All pages
Powered by GitBook
1 of 1

Loading...

InnoDB Page Compression

This feature enables transparent page-level compression for tables using algorithms like LZ4 or Zlib, reducing storage requirements.

Overview

InnoDB page compression provides a way to compress InnoDB tables.

Use Cases

  • InnoDB page compression can be used on any storage device and any file system.

  • InnoDB page compression is most efficient on file systems that support sparse files. See for more information.

  • InnoDB page compression is most beneficial on solid state drives (SSDs) and other flash storage. See for more information.

  • InnoDB page compression performs best when your storage device and file system support atomic writes, since that allows the to be disabled. See for more information.

Comparison with the COMPRESSED Row Format

InnoDB page compression is a modern way to compress your InnoDB tables. It is similar to InnoDB's row format, but it has many advantages. Some of the differences are:

  • With InnoDB page compression, compressed pages are immediately decompressed after being read from the tablespace file, and only uncompressed pages are stored in the buffer pool. In contrast, with InnoDB's row format, compressed pages are decompressed immediately after they are read from the tablespace file, and both the uncompressed and compressed pages are stored in the buffer pool. This means that the row format uses more space in the buffer pool than InnoDB page compression does.

  • With InnoDB page compression, pages are compressed just before being written to the tablespace file. In contrast, with InnoDB's row format, pages are re-compressed immediately after any changes, and the compressed pages are stored in the buffer pool alongside the uncompressed pages. These changes are then occasionally flushed to disk. This means that the row format re-compresses data more frequently than InnoDB page compression does.

  • With InnoDB page compression, multiple compression algorithms are supported. In contrast, with InnoDB's

In general, InnoDB page compression is superior to the row format.

Comparison with Storage Engine-Independent Column Compression

  • See .

Configuring the InnoDB Page Compression Algorithm

There is not currently a table option to set different InnoDB page compression algorithms for individual tables.

However, the server-wide InnoDB page compression algorithm can be configured by setting the system variable.

When this system variable is changed, the InnoDB page compression algorithm does not change for existing pages that were already compressed with a different InnoDB page compression algorithm. InnoDB is able to handle this situation without issues, because every page in an InnoDB tablespace contains metadata about the InnoDB page compression algorithm in the page header. This means that InnoDB supports having uncompressed pages and pages compressed with different InnoDB page compression algorithms in the same InnoDB tablespace at the same time.

This system variable can be set to one of the following values:

System Variable Value
Description

However, on many distributions, the standard MariaDB builds do not support all InnoDB page compression algorithms by default. From , algorithms can be .

This system variable can be changed dynamically with :

This system variable can also be set in a server in an prior to starting up the server:

Checking Supported InnoDB Page Compression Algorithms

On many distributions, the standard MariaDB builds do not support all InnoDB page compression algorithms by default. Therefore, if you want to use a specific InnoDB page compression algorithm, then you should check whether your MariaDB build supports it.

The compression algorithm is always supported. From , algorithms can be .

A MariaDB build's support for other InnoDB page compression algorithms can be checked by querying the following status variables with :

Status Variable
Description

For example:

Adding Support for an InnoDB Page Compression Algorithm

On many distributions, the standard MariaDB builds do not support all InnoDB page compression algorithms by default. From , algorithms can be , but in earlier versions, if you want to use certain InnoDB page compression algorithms, then you may need to do the following:

  • Download the package for the desired compression library from the above links.

  • Install the package for the desired compression library.

  • Compile MariaDB from the source distribution.

The general steps for compiling MariaDB are:

  • Download and unpack the source code distribution:

  • Configure the build using :

  • Check to confirm that it has found the desired compression library on your system.

  • Compile the build:

  • Either install the build:

Or make a package to install:

See for more information.

Enabling InnoDB Page Compression

InnoDB page compression is not enabled by default. However, InnoDB page compression can be enabled for just individual InnoDB tables or it can be enabled for all new InnoDB tables by default.

InnoDB page compression is also only supported if the InnoDB table is in a tablespace. Therefore, the system variable must be set to ON to use InnoDB page compression.

InnoDB page compression is only supported if the InnoDB table uses the Barracuda .Therefore, in and before, the system variable must be set to Barracuda to use InnoDB page compression.

InnoDB page compression is also only supported if the InnoDB table's is or .

Enabling InnoDB Page Compression by Default

In and later, InnoDB page compression can be enabled for all new InnoDB tables by default by setting the system variable to ON.

This system variable can be set to one of the following values:

System Variable Value
Description

This system variable can be changed dynamically with :

This system variable's session value can be changed dynamically with :

This system variable can also be set in a server in an prior to starting up the server:

Enabling InnoDB Page Compression for Individual Tables

InnoDB page compression can be enabled for individual tables by setting the table option to 1:

Configuring the Compression Level

Some InnoDB page compression algorithms support a compression level option, which configures how the InnoDB page compression algorithm will balance speed and compression.

The compression level's supported values range from 1 to 9. The range goes from the fastest to the most compact, which means that 1 is the fastest and 9 is the most compact.

Only the following InnoDB page compression algorithms currently support compression levels:

If an InnoDB page compression algorithm does not support compression levels, then it ignores any provided compression level value.

Configuring the Default Compression Level

The default compression level can be configured by setting the system variable.

This system variable's default value is 6.

This system variable can be changed dynamically with :

This system variable can also be set in a server in an prior to starting up the server:

Configuring the Compression Level for Individual Tables

The compression level for individual tables can also be configured by setting the table option for the table:

Configuring the Failure Threshold and Maximum Padding

InnoDB page compression can encounter compression failures.

InnoDB page compression's failure threshold can be configured. If InnoDB encounters more compression failures than the failure threshold, then it pads pages with zeroed out bytes before attempting to compress them as a way to reduce failures. If the failure rate stays above the failure threshold, then InnoDB pads pages with more zeroed out bytes in 128 byte increments.

InnoDB page compression's maximum padding can also be configured.

Configuring the Failure Threshold

The failure threshold can be configured by setting the system variable.

This system variable's supported values range from 0 to 100.

This system variable's default value is 5.

This system variable can be changed dynamically with :

This system variable can also be set in a server in an prior to starting up the server:

Configuring the Maximum Padding

The maximum padding can be configured by setting the system variable.

This system variable's supported values range from 0 to 75.

This system variable's default value is 50.

This system variable can be changed dynamically with :

This system variable can also be set in a server in an prior to starting up the server:

Saving Storage Space with Sparse Files

When InnoDB page compression is used, InnoDB may still write the compressed page to the tablespace file with the original size of the uncompressed page, which would be equivalent to the value of the system variable. This is done by design, because when InnoDB's I/O code needs to read the page from disk, it can only read the full page size. However, this is obviously not optimal.

On file systems that support sparse files, this problem is solved by writing the tablespace file as a sparse file using the punch hole technique. With the punch hole technique, InnoDB will only write the actual compressed page size to the tablespace file, aligned to sector size. The rest of the page is trimmed.

This punch hole technique allows InnoDB to read the compressed page from disk as the full page size, even though the compressed page really takes up less space on the file system.

There are some potential disadvantages to using sparse files:

  • Some utilities may require special options in order to handle sparse files in an efficient manner.

  • Most existing file systems are slow to sparse files. As a consequence, if a tablespace file is a sparse file, then dropping the table can be very slow.

Sparse File Support on Linux

On Linux, the following file systems support sparse files:

  • ext3

  • ext4

  • xfs

  • btrfs

On Linux, file systems need to support the system call with the FALLOC_FL_PUNCH_HOLE and FALLOC_FL_KEEP_SIZE flags:

Some Linux utilities may require special options in order to work with sparse files efficiently:

  • The utility will report the non-sparse size of the tablespace file when executed with default behavior, but will report the actual amount of storage allocated for the tablespace file.

  • The utility is pretty good at auto-detecting sparse files, but it also provides the and options, if the auto-detection is not desired.

  • The utility will archive sparse files with their non-sparse size when executed with default behavior, but will auto-detect sparse files, and archive them with their sparse size.

Sparse File Support on Windows

On Windows, the following file systems support sparse files:

  • NTFS

On Windows, file systems need to support the function with the and control codes:

Configuring InnoDB to use Sparse Files

In and later, InnoDB uses the punch hole technique to create sparse files used automatically when the underlying file system supports sparse files.

In and before, InnoDB can be configured to use the punch hole technique to create sparse files by configuring the and system variables. These system variables can be set in a server in an prior to starting up the server:

Optimized for Flash Storage

InnoDB page compression was designed to be optimized on solid state drives (SSDs) and other flash storage.

InnoDB page compression was originally developed by collaborating with . As a consequence, it was originally designed to work best on using . has since been acquired by , and they have decided not to continue supporting .

However, InnoDB page compression is still likely to be most optimized on solid state drives (SSDs) and other flash storage.

InnoDB page compression works without any issues on hard disk drives (HDDs). However, since its compression relies on the use of sparse files, the data may be somewhat fragmented on disk. This fragmentation may hurt performance on HDDs, since they handle random reads and writes much more slowly than flash storage.

Configuring InnoDB Page Flushing

With InnoDB page compression, pages are compressed when they are flushed to disk. Therefore, it can be helpful to optimize the configuration of InnoDB's page flushing. See for more information.

Monitoring InnoDB Page Compression

InnoDB page compression can be monitored by querying the following status variables with :

Status Variable
Description

With InnoDB page compression, a page is only compressed when it is flushed to disk. This means that if you are monitoring InnoDB page compression via these status variables, then the status variables values will only get incremented when the dirty pages are flushed to disk, which does not necessarily happen immediately:

Compatibility with Backup Tools

supports InnoDB page compression.

does not support InnoDB page compression.

Acknowledgements

  • InnoDB page compression was developed by collaborating with . Special thanks especially to Dhananjoy Das and Torben Mathiasen.

See Also

This page is licensed: CC BY-SA / Gnu FDL

row format,
is the only supported compression algorithm. This means that the
row format has less compression options than InnoDB page compression does.

snappy

Pages are compressed using the algorithm.

  • nvmfs

  • Number of 16384 sectors trimmed

    Number of 32768 sectors trimmed

    Number of pages compressed

    Number of trim operations

    Number of trim operations saved

    Number of pages decompressed

    Number of compression errors

    InnoDB holepunch compression vs the filesystem in MariaDB 10.1
  • Significant performance boost with new MariaDB page compression on FusionIO

  • INFLOW '14: NVM Compression—Hybrid Flash-Aware Application Level Compression

  • none

    Pages are not compressed. This is the default value in and before, and and before.

    zlib

    Pages are compressed using the bundled zlib compression algorithm. This is the default value in and later, and and later.

    lz4

    Pages are compressed using the lz4 compression algorithm.

    lzo

    Pages are compressed using the lzo compression algorithm.

    lzma

    Pages are compressed using the lzma compression algorithm.

    bzip2

    Pages are compressed using the bzip2 compression algorithm.

    Innodb_have_lz4

    Whether InnoDB supports the lz4 compression algorithm.

    Innodb_have_lzo

    Whether InnoDB supports the lzo compression algorithm.

    Innodb_have_lzma

    Whether InnoDB supports the lzma compression algorithm.

    Innodb_have_bzip2

    Whether InnoDB supports the bzip2 compression algorithm.

    Innodb_have_snappy

    Whether InnoDB supports the snappy compression algorithm.

    OFF

    New InnoDB tables do not use InnoDB page compression. This is the default value.

    ON

    New InnoDB tables use InnoDB page compression.

    Innodb_page_compression_saved

    Bytes saved by compression

    Innodb_page_compression_trim_sect512

    Number of 512 sectors trimmed

    Innodb_page_compression_trim_sect1024

    Number of 1024 sectors trimmed

    Innodb_page_compression_trim_sect2048

    Number of 2048 sectors trimmed

    Innodb_page_compression_trim_sect4096

    Number of 4096 sectors trimmed

    Innodb_page_compression_trim_sect8192

    Number of 8192 sectors trimmed

    Saving Storage Space with Sparse Files
    Optimized for Flash Storage
    InnoDB doublewrite buffer
    Atomic Write Support
    COMPRESSED
    COMPRESSED
    COMPRESSED
    COMPRESSED
    COMPRESSED
    COMPRESSED
    COMPRESSED
    Storage Engine-Independent Column Compression - Comparison with InnoDB Page Compression
    innodb_compression_algorithm
    installed as a plugin
    SET GLOBAL
    option group
    option file
    zlib
    installed as a plugin
    SHOW GLOBAL STATUS
    installed as a plugin
    cmake
    CMakeCache.txt
    Compiling MariaDB From Source
    file per-table
    innodb_file_per_table
    file format
    innodb_file_format
    row format
    COMPACT
    DYNAMIC
    innodb_compression_default
    SET GLOBAL
    SET SESSION
    option group
    option file
    PAGE_COMPRESSED
    zlib
    lzma
    innodb_compression_level
    SET GLOBAL
    option group
    option file
    PAGE_COMPRESSION_LEVEL
    innodb_compression_failure_threshold_pct
    SET GLOBAL
    option group
    option file
    innodb_compression_pad_pct_max
    SET GLOBAL
    option group
    option file
    innodb_page_size
    unlink()
    fallocate()
    ls
    ls -s
    cp
    cp --sparse=always
    cp --sparse=never
    tar
    tar --sparse
    DeviceIoControl()
    FSCTL_SET_SPARSE
    FSCTL_SET_ZERO_DATA
    innodb_use_trim
    innodb_use_fallocate
    option group
    option file
    Fusion-io
    FusionIO devices
    NVMFS
    Fusion-io
    Western Digital
    NVMFS
    InnoDB Page Flushing
    SHOW GLOBAL STATUS
    mariadb-backup
    Percona XtraBackup
    Fusion-io
    Storage-Engine Independent Column Compression
    Atomic Write Support
    MariaDB Introduces Atomic Writes
    Small Datum: Third day with InnoDB transparent page compression
    zlib
    COMPRESSED
    SET GLOBAL innodb_compression_algorithm='lzma';
    [mariadb]
    ...
    innodb_compression_algorithm=lzma
    SHOW GLOBAL STATUS WHERE Variable_name IN (
       'Innodb_have_lz4', 
       'Innodb_have_lzo', 
       'Innodb_have_lzma', 
       'Innodb_have_bzip2', 
       'Innodb_have_snappy'
    );
    +--------------------+-------+
    | Variable_name      | Value |
    +--------------------+-------+
    | Innodb_have_lz4    | OFF   |
    | Innodb_have_lzo    | OFF   |
    | Innodb_have_lzma   | ON    |
    | Innodb_have_bzip2  | OFF   |
    | Innodb_have_snappy | OFF   |
    +--------------------+-------+
    wget https://downloads.mariadb.com/MariaDB/mariadb-10.4.8/source/mariadb-10.4.8.tar.gz
    tar -xvzf mariadb-10.4.8.tar.gz
    cd mariadb-10.4.8/
    cmake .
    make
    make install
    make package
    SET GLOBAL innodb_compression_default=ON;
    SET GLOBAL innodb_file_per_table=ON;
    
    SET GLOBAL innodb_file_format='Barracuda';
    
    SET GLOBAL innodb_default_row_format='dynamic';
    
    SET GLOBAL innodb_compression_algorithm='lzma';
    
    SET SESSION  innodb_compression_default=ON;
    
    CREATE TABLE users (
       user_id INT NOT NULL, 
       b VARCHAR(200), 
       PRIMARY KEY(user_id)
    ) 
       ENGINE=InnoDB;
    [mariadb]
    ...
    innodb_compression_default=ON
    SET GLOBAL innodb_file_per_table=ON;
    
    SET GLOBAL innodb_file_format='Barracuda';
    
    SET GLOBAL innodb_default_row_format='dynamic';
    
    SET GLOBAL innodb_compression_algorithm='lzma';
    
    CREATE TABLE users (
       user_id INT NOT NULL, 
       b VARCHAR(200), 
       PRIMARY KEY(user_id)
    ) 
       ENGINE=InnoDB
       PAGE_COMPRESSED=1;
    SET GLOBAL innodb_compression_level=9;
    [mariadb]
    ...
    innodb_compression_level=9
    SET GLOBAL innodb_file_per_table=ON;
    
    SET GLOBAL innodb_file_format='Barracuda';
    
    SET GLOBAL innodb_default_row_format='dynamic';
    
    SET GLOBAL innodb_compression_algorithm='lzma';
    
    CREATE TABLE users (
       user_id INT NOT NULL, 
       b VARCHAR(200), 
       PRIMARY KEY(user_id)
    ) 
       ENGINE=InnoDB
       PAGE_COMPRESSED=1
       PAGE_COMPRESSION_LEVEL=9;
    SET GLOBAL innodb_compression_failure_threshold_pct=10;
    [mariadb]
    ...
    innodb_compression_failure_threshold_pct=10
    SET GLOBAL innodb_compression_pad_pct_max=75;
    [mariadb]
    ...
    innodb_compression_pad_pct_max=75
    fallocate(file_handle, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, file_offset, remainder_len);
    DeviceIoControl(file_handle, FSCTL_SET_SPARSE, inbuf, inbuf_size, 
       outbuf, outbuf_size,  NULL, &overlapped)
    ...
    DeviceIoControl(file_handle, FSCTL_SET_ZERO_DATA, inbuf, inbuf_size, 
       outbuf, outbuf_size,  NULL, &overlapped)
    [mariadb]
    ...
    innodb_use_trim=ON
    innodb_use_fallocate=ON
    CREATE TABLE `tab` (
         `id` INT(11) NOT NULL,
         `str` VARCHAR(50) DEFAULT NULL,
         PRIMARY KEY (`id`)
       ) ENGINE=InnoDB;
     
    INSERT INTO tab VALUES (1, 'str1');
    
    SHOW GLOBAL STATUS LIKE 'Innodb_num_pages_page_compressed';
    +----------------------------------+-------+
    | Variable_name                    | Value |
    +----------------------------------+-------+
    | Innodb_num_pages_page_compressed | 0     |
    +----------------------------------+-------+
     
    SET GLOBAL innodb_file_per_table=ON;
    
    SET GLOBAL innodb_file_format='Barracuda';
    
    SET GLOBAL innodb_default_row_format='dynamic';
    
    SET GLOBAL innodb_compression_algorithm='lzma';
     
    ALTER TABLE tab PAGE_COMPRESSED=1;
    
    SHOW GLOBAL STATUS LIKE 'Innodb_num_pages_page_compressed';
    +----------------------------------+-------+
    | Variable_name                    | Value |
    +----------------------------------+-------+
    | Innodb_num_pages_page_compressed | 0     |
    +----------------------------------+-------+
     
    SELECT SLEEP(10);
    +-----------+
    | SLEEP(10) |
    +-----------+
    |         0 |
    +-----------+
     
    SHOW GLOBAL STATUS LIKE 'Innodb_num_pages_page_compressed';
    +----------------------------------+-------+
    | Variable_name                    | Value |
    +----------------------------------+-------+
    | Innodb_num_pages_page_compressed | 3     |
    +----------------------------------+-------+
    snappy
    Innodb_page_compression_trim_sect16384
    Innodb_page_compression_trim_sect32768
    Innodb_num_pages_page_compressed
    Innodb_num_page_compressed_trim_op
    Innodb_num_page_compressed_trim_op_saved
    Innodb_num_pages_page_decompressed
    Innodb_num_pages_page_compression_error
    MariaDB 10.7
    MariaDB 10.7
    MariaDB 10.7
    MariaDB 10.1
    MariaDB 10.2.3
    MariaDB 10.3
    MariaDB 10.2
    MariaDB 10.2.3
    MariaDB 10.1.21
    MariaDB 10.2.4
    MariaDB 10.1.22