mysql的壓縮特性-需求

玄慚發表於2016-04-01

需求:最近有個插入量比較大的應用需要上,每天的插入量在1億左右,同時會有較少的查詢,表的單行長度在0.5k,就資料而言每天有近50G資料,由於每天寫一張新表,保留30天的資料,一個月下來也要1.5T,所以考慮了mysql採用mysql innodb plugin的壓縮儲存,在官方文件中這樣描述了mysql的compress特性:

The ability to compress user data is an important new capability of the InnoDB Plugin. Compressed tables reduce the size of the database on disk, resulting in fewer reads and writes needed to access the user data. For many InnoDB workloads and many typical user tables (especially with read-intensive applications where sufficient memory is available to keep frequently-used data in memory), compression not only significantly reduces the storage required for the database, but also improves throughput by reducing the I/O workload, at a modest cost in processing overhead. The storage cost savings can be important, but the reduction in I/O costs can be even more valuable.

從文件中可以看到compress最大的優勢不僅在於空間的壓縮上,同時可以降低i/o的請求,特別在read-intensive的場景下[注意這裡是在記憶體能夠容納所讀取的資料]非常有效。

配置:

create table compress_test() ENGINE=InnoDB DEFAULT CHARSET=gbk ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8;

可以看到比我們平常的建表的引數中多加了ROW_FORMAT=COMPRESSED和 KEY_BLOCK_SIZE=8兩個引數,需要注意的是:Available only with innodb_file_format=Barracuda , Compresses the table and indexes using zlib to default compressed page size of 8K bytes;只有在檔案格式在Barracuda時候才能使用compress特性,那麼就需要配置innodb_file_per_table和innodb_file_format兩個引數;

預設情況下,mysql會將每個頁壓縮為8k,但也可以壓縮到1KB, 2KB, 4KB, 8KB, 16KB,普通innodb的page大小為16k,所以設定為key_block_size=16k不會收到很大的壓縮效果,但是對於一些blob,txt大欄位來說通常會很有用;

mysql的壓縮不僅對於表的資料有效,對索引頁(聚簇和非聚簇)同樣壓縮到key_block_size大小,但是我們卻不可以在建立索引的時候指定該索引的壓縮,而只能通過表的壓縮來實現索引的壓縮;

root@test 02:28:42>show variables like ‘%innodb_file_per_table%’;

Variable_name         | Value |

innodb_file_per_table | ON    |

root@test 02:30:43>show variables like ‘%innodb_file_format%’;

Variable_name            | Value     |

innodb_file_format       | Barracuda |

| innodb_file_format_check | Barracuda |


相關文章