MySQL :: MySQL 5.7 Reference Manual :: 14.9.1.2 Creating Compressed Tables (original) (raw)

14.9.1.2 Creating Compressed Tables

Compressed tables can be created infile-per-table tablespaces or ingeneral tablespaces. Table compression is not available for the InnoDB system tablespace. The system tablespace (space 0, the.ibdata files) can contain user-created tables, but it also contains internal system data, which is never compressed. Thus, compression applies only to tables (and indexes) stored in file-per-table or general tablespaces.

Creating a Compressed Table in File-Per-Table Tablespace

To create a compressed table in a file-per-table tablespace,innodb_file_per_table must be enabled (the default in MySQL 5.6.6) andinnodb_file_format must be set to Barracuda. You can set these parameters in the MySQL configuration file (my.cnf ormy.ini) or dynamically, using aSET statement.

After the innodb_file_per_table and innodb_file_format options are configured, specify theROW_FORMAT=COMPRESSED clause orKEY_BLOCK_SIZE clause, or both, in aCREATE TABLE orALTER TABLE statement to create a compressed table in a file-per-table tablespace.

For example, you might use the following statements:

SET GLOBAL innodb_file_per_table=1;
SET GLOBAL innodb_file_format=Barracuda;
CREATE TABLE t1
 (c1 INT PRIMARY KEY)
 ROW_FORMAT=COMPRESSED
 KEY_BLOCK_SIZE=8;
Creating a Compressed Table in a General Tablespace

To create a compressed table in a general tablespace,FILE_BLOCK_SIZE must be defined for the general tablespace, which is specified when the tablespace is created. The FILE_BLOCK_SIZE value must be a valid compressed page size in relation to theinnodb_page_size value, and the page size of the compressed table, defined by theCREATE TABLE orALTER TABLE KEY_BLOCK_SIZE clause, must be equal toFILE_BLOCK_SIZE/1024. For example, ifinnodb_page_size=16384 andFILE_BLOCK_SIZE=8192, theKEY_BLOCK_SIZE of the table must be 8. For more information, see Section 14.6.3.3, “General Tablespaces”.

The following example demonstrates creating a general tablespace and adding a compressed table. The example assumes a defaultinnodb_page_size of 16K. TheFILE_BLOCK_SIZE of 8192 requires that the compressed table have a KEY_BLOCK_SIZE of 8.

mysql> CREATE TABLESPACE `ts2` ADD DATAFILE 'ts2.ibd' FILE_BLOCK_SIZE = 8192 Engine=InnoDB;

mysql> CREATE TABLE t4 (c1 INT PRIMARY KEY) TABLESPACE ts2 ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8;
Notes
Restrictions on Compressed Tables