MariaDB Enterprise Server Configure InnoDB Page Compression
This page is part of MariaDB's Documentation.
The parent of this page is: MariaDB Enterprise Server InnoDB Operations
Topics on this page:
Overview
In MariaDB Enterprise Server, InnoDB supports table compression using InnoDB Page Compression. InnoDB Page Compression supports any InnoDB row format.
About InnoDB Page Compression
InnoDB Page Compression:
Is the recommended compression method for InnoDB tables:
Supports the zlib compression algorithm.
Compresses each page immediately before writing it to the tablespace file.
Creates variable-sized compressed pages.
Saves storage space and decreases I/O by treating tablespace files as sparse files.
Decompresses each page immediately after reading it from the tablespace file.
Only stores decompressed pages in the InnoDB buffer pool.
Basic Usage
CREATE DATABASE hq_sales;
CREATE TABLE hq_sales.invoices (
invoice_id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL,
branch_id INT NOT NULL,
customer_id INT,
invoice_date DATETIME(6),
invoice_total DECIMAL(13, 2),
payment_method ENUM('NONE', 'CASH', 'WIRE_TRANSFER', 'CREDIT_CARD', 'GIFT_CARD'),
PRIMARY KEY(invoice_id)
)
ENGINE = InnoDB
PAGE_COMPRESSED = 1;
SELECT NAME,
IF(
(FLAG & 128) = 128,
'Compressed',
'Not Compressed'
) AS Is_Compressed,
(FLAG >> 8) AS Compression_Level
FROM information_schema.INNODB_SYS_TABLES
WHERE NAME='hq_sales/invoices';
+-------------------+---------------+-------------------+
| NAME | Is_Compressed | Compression_Level |
+-------------------+---------------+-------------------+
| hq_sales/invoices | Compressed | 9 |
+-------------------+---------------+-------------------+
InnoDB Page Compression vs. InnoDB's Compressed Row Format
InnoDB Page Compression works a bit differently than the Compressed row format:
Feature | InnoDB Page Compression | |
---|---|---|
Recommended | Yes | No |
Efficiently utilizes buffer pool | Yes | No |
Efficiently minimizes compression operations | Yes | No |
Read/write | Yes | Not much longer* |
Algorithms | zlib | zlib |
Compressed page size | Variable (relies on sparse files) | Specified by the |
Caches in buffer pool | Only decompressed pages | Both compressed and decompressed pages |
Compression timing | Compresses each page immediately before writing it to the tablespace file | Compresses each page immediately after data on the page changes |
Decompression timing | Decompresses each page immediately after reading it from the tablespace file | Decompresses each page immediately after reading it from the tablespace file |
- *:
ROW_
FORMAT=COMPRESSED format tables are read-only by default in the latest releases, and the option to switch them back to read/write will be going away in a future release.
Create Compressed InnoDB Tables
Create Compressed InnoDB Tables using the innodb_compression_default System Variable
Compressed InnoDB tables can be created by setting the session value of the innodb_ON
:
Connect to the server using MariaDB Client:
$ mariadb --user=root
In your current session, set the session value of the innodb_
compression_ system variable todefault ON
using the SET SESSION statement.For example:
SET SESSION innodb_compression_default=ON;
If the database does not exist, then create the database for the table using the CREATE DATABASE statement:
CREATE DATABASE hq_sales;
Create the table using the CREATE TABLE statement:
CREATE TABLE hq_sales.invoices ( invoice_id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, branch_id INT NOT NULL, customer_id INT, invoice_date DATETIME(6), invoice_total DECIMAL(13, 2), payment_method ENUM('NONE', 'CASH', 'WIRE_TRANSFER', 'CREDIT_CARD', 'GIFT_CARD'), PRIMARY KEY(invoice_id) );
Confirm that the table uses InnoDB page compression by querying the information_
schema.INNODB_ table:SYS_ TABLES SELECT NAME, IF( (FLAG & 128) = 128, 'Compressed', 'Not Compressed' ) AS Is_Compressed FROM information_schema.INNODB_SYS_TABLES WHERE NAME='hq_sales/invoices';
+-------------------+---------------+ | NAME | Is_Compressed | +-------------------+---------------+ | hq_sales/invoices | Compressed | +-------------------+---------------+
Create Compressed InnoDB Tables using the PAGE_COMPRESSED Table Option
Compressed InnoDB tables can be created by providing the PAGE_COMPRESSED
table option to the CREATE TABLE statement:
Connect to the server using MariaDB Client:
$ mariadb --user=root
If the database does not exist, then create the database for the table using the CREATE DATABASE statement:
CREATE DATABASE hq_sales;
Create the table using the CREATE TABLE statement, and enable InnoDB page compression using the
PAGE_COMPRESSED
table option:CREATE TABLE hq_sales.invoices ( invoice_id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, branch_id INT NOT NULL, customer_id INT, invoice_date DATETIME(6), invoice_total DECIMAL(13, 2), payment_method ENUM('NONE', 'CASH', 'WIRE_TRANSFER', 'CREDIT_CARD', 'GIFT_CARD'), PRIMARY KEY(invoice_id) ) PAGE_COMPRESSED = 1;
Confirm that the table uses InnoDB page compression by querying the information_
schema.INNODB_ table:SYS_ TABLES SELECT NAME, IF( (FLAG & 128) = 128, 'Compressed', 'Not Compressed' ) AS Is_Compressed FROM information_schema.INNODB_SYS_TABLES WHERE NAME='hq_sales/invoices';
+-------------------+---------------+ | NAME | Is_Compressed | +-------------------+---------------+ | hq_sales/invoices | Compressed | +-------------------+---------------+
Compression Level
Some compression algorithms have the concept of a "compression level." The compression level determines how the algorithm balances 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.
Configure the Compression Level using the innodb_compression_level System Variable
The compression level can be configured by setting the innodb_
Connect to the server using MariaDB Client:
$ mariadb --user=root
Set the innodb_
compression_ system variable to the desired compression level using the SET GLOBAL statement.level For example:
SET GLOBAL innodb_compression_level=9;
Choose a configuration file for custom changes to system variables and options.
It is not recommended to make custom changes to Enterprise Server's default configuration files, because your custom changes can be overwritten by other default configuration files that are loaded after.
Ensure that your custom changes will be read last by creating a custom configuration file in one of the included directories. Configuration files in included directories are read in alphabetical order. Ensure that your custom configuration file is read last by using the
z-
prefix in the file name.Some example configuration file paths for different distributions are shown in the following table:
Distributions
Example configuration file path
CentOSRHELRocky LinuxSLES/etc/my.cnf.d/z-custom-mariadb.cnf
DebianUbuntu/etc/mysql/mariadb.conf.d/z-custom-mariadb.cnf
Set the innodb_
compression_ system variable in the configuration file.level It needs to be set in a group that will be read by MariaDB Server, such as
[mariadb]
or[server]
.For example:
[mariadb] ... innodb_compression_level=9
Configure the Compression Level using the PAGE_COMPRESSION_LEVEL Table Option
The compression level for an InnoDB table can be set by providing the PAGE_COMPRESSION_LEVEL
table option to the CREATE TABLE statement:
Connect to the server using MariaDB Client:
$ mariadb --user=root
If the database does not exist, then create the database for the table using the CREATE DATABASE statement:
CREATE DATABASE hq_sales;
Create the table using the CREATE TABLE statement, and set the compression level using the
PAGE_COMPRESSION_LEVEL
table option:CREATE TABLE hq_sales.invoices ( invoice_id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, branch_id INT NOT NULL, customer_id INT, invoice_date DATETIME(6), invoice_total DECIMAL(13, 2), payment_method ENUM('NONE', 'CASH', 'WIRE_TRANSFER', 'CREDIT_CARD', 'GIFT_CARD'), PRIMARY KEY(invoice_id) ) PAGE_COMPRESSED = 1 PAGE_COMPRESSION_LEVEL=9;
Query an InnoDB Table's Compression Level
Query the compression level for an InnoDB table by querying the information_
SELECT NAME,
IF(
(FLAG & 128) = 128,
'Compressed',
'Not Compressed'
) AS Is_Compressed,
(FLAG >> 8) AS Compression_Level
FROM information_schema.INNODB_SYS_TABLES
WHERE NAME='hq_sales/invoices';
+-------------------+---------------+-------------------+
| NAME | Is_Compressed | Compression_Level |
+-------------------+---------------+-------------------+
| hq_sales/invoices | Compressed | 9 |
+-------------------+---------------+-------------------+
Finding Compressed InnoDB Tables
Let's find compressed InnoDB tables by querying the information_
Connect to the server using MariaDB Client:
$ mariadb --user=root
Find tables that use InnoDB page compression by querying the information_
schema.INNODB_ table:SYS_ TABLES SELECT NAME, IF( (FLAG & 128) = 128, 'Compressed', 'Not Compressed' ) AS Is_Compressed, (FLAG >> 8) AS Compression_Level FROM information_schema.INNODB_SYS_TABLES WHERE NAME NOT LIKE 'SYS_%' AND NAME NOT LIKE 'mysql/%';
+-------------------+---------------+-------------------+ | NAME | Is_Compressed | Compression_Level | +-------------------+---------------+-------------------+ | hq_sales/invoices | Compressed | 9 | +-------------------+---------------+-------------------+