mysql的压缩特性-需求

时间:2023-03-09 00:28:17
mysql的压缩特性-需求

需求:最近有个插入量比较大的应用需要上,每天的插入量在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 |