飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • UEditor使用文档
  • AngularJS教程
  • ThinkPHP5.0教程

mysql压缩解决方案

时间:2021-12-22  作者:q1359720840  

描述 MySQL 压缩的使用场景和解决方案,包括压缩传输协议、压缩列解决方案和压缩表解决方案。

提到 MySQL 压缩相关的内容,我们能想到的可能是如下几种和压缩相关的场景:

1、客户端和服务器之间传输的数据量太大,需要进行压缩,节约带宽

2、MySQL 某个列的数据量大,只针对某个列的数据压缩

3、MySQL 某个或者某几个表数据太多,需要将表数据压缩存放,减少磁盘空间的占用

这几个问题在 MySQL 侧都有很好的解决方案 ,针对第 1 个问题,可以使用 MySQL 的压缩协议解决;针对第 2 个问题,可以采用 MySQL 的压缩和解压函数完美解决;而针对最复杂的第 3 个问题,则可以在引擎层面进行解决,目前 myisam、innodb、tokudb、MyRocks 等引擎都支持表的压缩。本篇文章要详细讨论的就是此类关于 MySQL 压缩机制相关 的问题,下面是主要的内容:

一、MySQL 压缩协议介绍

1、适用场景

MySQL 压缩协议适合的场景是 MySQL 的服务器端和客户端之间传输的数据量很大,或者可用带宽不高的情况,典型的场景有如下两个:

a、查询大量的数据,带宽不够(比如导出数据的时候);

b、复制的时候 binlog 量太大,启用 slave_compressed_protocol 参数进行日志压缩复制。

2、压缩协议简介

压缩协议是 MySQL 通信协议的一部分,要启用压缩协议进行数据传输,需要 MySQL 服务器端和客户端都支持 zlib 算法。启动压缩协议会导致 CPU 负载略微上升。使用启用压缩协议使用-C 参数或者 --compress=true 参数启动客户端的压缩功能。如果启用了-C 或者 compress=true 选项,那么在连接到服务器段的时候,会发送 0x0020(CLIENT_COMPRESS)的服务器权能标志位,和服务器端协商通过后(3 次握手以后),就支持压缩协议了。由于采用压缩,数据包的格式会发生变化,具体的变化如下:

未压缩的数据包格式:

压缩后的数据包格式:

大家可能留意到压缩后的数据报格式有压缩和未压缩之分,这个是 MySQL 为了较少 CPU 开销而做的一个优化。如果内容小于 50 个字节的时候,就不对内容进行压缩,而大于 50 字节的时候,才会启用压缩功能。具体的规则如下:

当第三个字段的值等于 0x00 的时候,表示当前包没有压缩,因此 n*byte 的内容为 1*byte,n*byte,即请求类型和请求内容。

当第三个字段的值大于 0x00 的时候,表示当前包已采用 zlib 压缩,因此使用的时候需要对 n*byte 进行解压,解压后内容为 1*byte,n*byte,即请求类型和请求内容。

3、方案实践

在客户端连接的时候加上-C 或者--compress=true 参数。如果是对同步添加压缩协议支持的时候,则需要配置 slave_compressed_protocol=1。下面是采用压缩协议连接 MySQL 服务端的范例:

  • MySQL -h hostip -uroot -p password --compress
  • MySQLdump -h hostip -uroot -p password -default-character-set=utf8 --compress --single-transaction dbname tablename > 域名

如果需要在主从复制中启用压缩传输,则在从机开启 slave_compressed_protocol=1 参数就 OK。

4、压缩效果

可以通过在 MySQLdump 中使用--compress 选项来观察压缩传输的效果,也可以通过主从复制中已用 slave_compressed_protocol 参数来观察压缩传输的效果,很容易看出效果,这里不再截图说明。

二、MySQL 列压缩解决方案

2、压缩函数简介

MySQL 的压缩函数 COMPRESS 压缩一个字符串,然后返回一个二进制串。使用该函数需要 MySQL 服务端支持压缩,否则会返回 NULL,压缩字段最好采用 varbinary 或者 blob 字段类型保存。使用 UNCOMPRESSED 函数对压缩过的数据进行解压。注意,采用这种方式需要在业务侧做少量改造。压缩后的内容存储方式如下:

a、空字符串就以空字符串存储

b、非空字符串存储方式为前 4 个 bype 保存未压缩的字符串,紧接着保存压缩的字符串

3、方案实践

字段压缩方案涉及到的几个相关的函数如下:

压缩函数

  • COMPRESS()

解压缩函数

  • UNCOMPRESS()

字符串长度函数

  • LENGTH()

未解压字符串长度函数

  • UNCOMPRESSED_LENGTH()

实践步骤:

1. 创建数据库表

CREATE TABLE  IF NOT EXISTS `test_compress` (

`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT \'ID\',

`content` blob NOT NULL COMMENT \'内容列\',
`name` varchar(200) NOT NULL COMMENT \'name\',

PRIMARY KEY (`id`)

 ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT=\'压缩测试表\';

  2.插入一条数据

insert into `test_compress`(content) values(COMPRESS(\'"<div class="w-e-content-container"> <p> <img src="https://域名/chic/jfs/t1/213416/13/1536/273782/617372daE5e6b3a33/域名" alt="" data-href="https://域名/chic/jfs/t1/213416/13/1536/273782/617372daE5e6b3a33/域名" style=""/> <img src="https://域名/chic/jfs/t1/220138/15/1882/93119/61777d3dE61db5ddb/域名" alt="" data-href="https://域名/chic/jfs/t1/220138/15/1882/93119/61777d3dE61db5ddb/域名" style=""/> <img src="https://域名/chic/jfs/t1/218219/26/1554/286582/617372daE80c0bdce/域名" alt="" data-href="https://域名/chic/jfs/t1/218219/26/1554/286582/617372daE80c0bdce/域名" style=""/> <img src="https://域名/chic/jfs/t1/207798/25/6476/328695/617372daE4f770dd4/域名" alt="" data-href="https://域名/chic/jfs/t1/207798/25/6476/328695/617372daE4f770dd4/域名" style=""/> <img src="https://域名/chic/jfs/t1/207293/28/6562/355923/617372daEad2418ed/域名" alt="" data-href="https://域名/chic/jfs/t1/207293/28/6562/355923/617372daEad2418ed/域名" style=""/> <img src="https://域名/chic/jfs/t1/198472/7/14147/353512/617372daE475bfd78/域名" alt="" data-href="https://域名/chic/jfs/t1/198472/7/14147/353512/617372daE475bfd78/域名" style=""/> <img src="https://域名/chic/jfs/t1/208709/11/6409/385534/617372daE546bf141/域名" alt="" data-href="https://域名/chic/jfs/t1/208709/11/6409/385534/617372daE546bf141/域名" style=""/> <img src="https://域名/chic/jfs/t1/141557/22/23755/397718/617372daE0b82f73d/域名" alt="" data-href="https://域名/chic/jfs/t1/141557/22/23755/397718/617372daE0b82f73d/域名" style=""/> <img src="https://域名/chic/jfs/t1/169270/29/21329/416165/617372daE94e8b257/域名" alt="" data-href="https://域名/chic/jfs/t1/169270/29/21329/416165/617372daE94e8b257/域名" style=""/> <img src="https://域名/chic/jfs/t1/140728/1/25666/344248/617372dbE535662cb/域名" alt="" data-href="https://域名/chic/jfs/t1/140728/1/25666/344248/617372dbE535662cb/域名" style=""/> <img src="https://域名/chic/jfs/t1/171559/6/21235/423277/617372dbE9eb46235/域名" alt="" data-href="https://域名/chic/jfs/t1/171559/6/21235/423277/617372dbE9eb46235/域名" style=""/> <img src="https://域名/chic/jfs/t1/168858/37/22897/439146/617372dbEd82f4e2a/域名" alt="" data-href="https://域名/chic/jfs/t1/168858/37/22897/439146/617372dbEd82f4e2a/域名" style=""/> <img src="https://域名/chic/jfs/t1/205152/24/12749/493690/617372dbEf73dbe18/域名" alt="" data-href="https://域名/chic/jfs/t1/205152/24/12749/493690/617372dbEf73dbe18/域名" style=""/> <img src="https://域名/chic/jfs/t1/161214/15/21277/441293/617372dbE8e533234/域名" alt="" data-href="https://域名/chic/jfs/t1/161214/15/21277/441293/617372dbE8e533234/域名" style=""/> <img src="https://域名/chic/jfs/t1/218750/6/1519/383308/617372dbEa6274296/域名" alt="" data-href="https://域名/chic/jfs/t1/218750/6/1519/383308/617372dbEa6274296/域名" style=""/> <img src="https://域名/chic/jfs/t1/156858/19/21427/418698/617372dbEeca2b01f/域名" alt="" data-href="https://域名/chic/jfs/t1/156858/19/21427/418698/617372dbEeca2b01f/域名" style=""/> </p> </div><div class="w-e-content-container"> <p> <img src="https://域名/chic/jfs/t1/213416/13/1536/273782/617372daE5e6b3a33/域名" alt="" data-href="https://域名/chic/jfs/t1/213416/13/1536/273782/617372daE5e6b3a33/域名" style=""/> <img src="https://域名/chic/jfs/t1/220138/15/1882/93119/61777d3dE61db5ddb/域名" alt="" data-href="https://域名/chic/jfs/t1/220138/15/1882/93119/61777d3dE61db5ddb/域名" style=""/> <img src="https://域名/chic/jfs/t1/218219/26/1554/286582/617372daE80c0bdce/域名" alt="" data-href="https://域名/chic/jfs/t1/218219/26/1554/286582/617372daE80c0bdce/域名" style=""/> <img src="https://域名/chic/jfs/t1/207798/25/6476/328695/617372daE4f770dd4/域名" alt="" data-href="https://域名/chic/jfs/t1/207798/25/6476/328695/617372daE4f770dd4/域名" style=""/> <img src="https://域名/chic/jfs/t1/207293/28/6562/355923/617372daEad2418ed/域名" alt="" data-href="https://域名/chic/jfs/t1/207293/28/6562/355923/617372daEad2418ed/域名" style=""/> <img src="https://域名/chic/jfs/t1/198472/7/14147/353512/617372daE475bfd78/域名" alt="" data-href="https://域名/chic/jfs/t1/198472/7/14147/353512/617372daE475bfd78/域名" style=""/> <img src="https://域名/chic/jfs/t1/208709/11/6409/385534/617372daE546bf141/域名" alt="" data-href="https://域名/chic/jfs/t1/208709/11/6409/385534/617372daE546bf141/域名" style=""/> <img src="https://域名/chic/jfs/t1/141557/22/23755/397718/617372daE0b82f73d/域名" alt="" data-href="https://域名/chic/jfs/t1/141557/22/23755/397718/617372daE0b82f73d/域名" style=""/> <img src="https://域名/chic/jfs/t1/169270/29/21329/416165/617372daE94e8b257/域名" alt="" data-href="https://域名/chic/jfs/t1/169270/29/21329/416165/617372daE94e8b257/域名" style=""/> <img src="https://域名/chic/jfs/t1/140728/1/25666/344248/617372dbE535662cb/域名" alt="" data-href="https://域名/chic/jfs/t1/140728/1/25666/344248/617372dbE535662cb/域名" style=""/> <img src="https://域名/chic/jfs/t1/171559/6/21235/423277/617372dbE9eb46235/域名" alt="" data-href="https://域名/chic/jfs/t1/171559/6/21235/423277/617372dbE9eb46235/域名" style=""/> <img src="https://域名/chic/jfs/t1/168858/37/22897/439146/617372dbEd82f4e2a/域名" alt="" data-href="https://域名/chic/jfs/t1/168858/37/22897/439146/617372dbEd82f4e2a/域名" style=""/> <img src="https://域名/chic/jfs/t1/205152/24/12749/493690/617372dbEf73dbe18/域名" alt="" data-href="https://域名/chic/jfs/t1/205152/24/12749/493690/617372dbEf73dbe18/域名" style=""/> <img src="https://域名/chic/jfs/t1/161214/15/21277/441293/617372dbE8e533234/域名" alt="" data-href="https://域名/chic/jfs/t1/161214/15/21277/441293/617372dbE8e533234/域名" style=""/> <img src="https://域名/chic/jfs/t1/218750/6/1519/383308/617372dbEa6274296/域名" alt="" data-href="https://域名/chic/jfs/t1/218750/6/1519/383308/617372dbEa6274296/域名" style=""/> <img src="https://域名/chic/jfs/t1/156858/19/21427/418698/617372dbEeca2b01f/域名" alt="" data-href="https://域名/chic/jfs/t1/156858/19/21427/418698/617372dbEeca2b01f/域名" style=""/> </p> </div><div class="w-e-content-container"> <p> <img src="https://域名/chic/jfs/t1/213416/13/1536/273782/617372daE5e6b3a33/域名" alt="" data-href="https://域名/chic/jfs/t1/213416/13/1536/273782/617372daE5e6b3a33/域名" style=""/> <img src="https://域名/chic/jfs/t1/220138/15/1882/93119/61777d3dE61db5ddb/域名" alt="" data-href="https://域名/chic/jfs/t1/220138/15/1882/93119/61777d3dE61db5ddb/域名" style=""/> <img src="https://域名/chic/jfs/t1/218219/26/1554/286582/617372daE80c0bdce/域名" alt="" data-href="https://域名/chic/jfs/t1/218219/26/1554/286582/617372daE80c0bdce/域名" style=""/> <img src="https://域名/chic/jfs/t1/207798/25/6476/328695/617372daE4f770dd4/域名" alt="" data-href="https://域名/chic/jfs/t1/207798/25/6476/328695/617372daE4f770dd4/域名" style=""/> <img src="https://域名/chic/jfs/t1/207293/28/6562/355923/617372daEad2418ed/域名" alt="" data-href="https://域名/chic/jfs/t1/207293/28/6562/355923/617372daEad2418ed/域名" style=""/> <img src="https://域名/chic/jfs/t1/198472/7/14147/353512/617372daE475bfd78/域名" alt="" data-href="https://域名/chic/jfs/t1/198472/7/14147/353512/617372daE475bfd78/域名" style=""/> <img src="https://域名/chic/jfs/t1/208709/11/6409/385534/617372daE546bf141/域名" alt="" data-href="https://域名/chic/jfs/t1/208709/11/6409/385534/617372daE546bf141/域名" style=""/> <img src="https://域名/chic/jfs/t1/141557/22/23755/397718/617372daE0b82f73d/域名" alt="" data-href="https://域名/chic/jfs/t1/141557/22/23755/397718/617372daE0b82f73d/域名" style=""/> <img src="https://域名/chic/jfs/t1/169270/29/21329/416165/617372daE94e8b257/域名" alt="" data-href="https://域名/chic/jfs/t1/169270/29/21329/416165/617372daE94e8b257/域名" style=""/> <img src="https://域名/chic/jfs/t1/140728/1/25666/344248/617372dbE535662cb/域名" alt="" data-href="https://域名/chic/jfs/t1/140728/1/25666/344248/617372dbE535662cb/域名" style=""/> <img src="https://域名/chic/jfs/t1/171559/6/21235/423277/617372dbE9eb46235/域名" alt="" data-href="https://域名/chic/jfs/t1/171559/6/21235/423277/617372dbE9eb46235/域名" style=""/> <img src="https://域名/chic/jfs/t1/168858/37/22897/439146/617372dbEd82f4e2a/域名" alt="" data-href="https://域名/chic/jfs/t1/168858/37/22897/439146/617372dbEd82f4e2a/域名" style=""/> <img src="https://域名/chic/jfs/t1/205152/24/12749/493690/617372dbEf73dbe18/域名" alt="" data-href="https://域名/chic/jfs/t1/205152/24/12749/493690/617372dbEf73dbe18/域名" style=""/> <img src="https://域名/chic/jfs/t1/161214/15/21277/441293/617372dbE8e533234/域名" alt="" data-href="https://域名/chic/jfs/t1/161214/15/21277/441293/617372dbE8e533234/域名" style=""/> <img src="https://域名/chic/jfs/t1/218750/6/1519/383308/617372dbEa6274296/域名" alt="" data-href="https://域名/chic/jfs/t1/218750/6/1519/383308/617372dbEa6274296/域名" style=""/> <img src="https://域名/chic/jfs/t1/156858/19/21427/418698/617372dbEeca2b01f/域名" alt="" data-href="https://域名/chic/jfs/t1/156858/19/21427/418698/617372dbEeca2b01f/域名" style=""/> </p> </div><div class="w-e-content-container"> <p> <img src="https://域名/chic/jfs/t1/213416/13/1536/273782/617372daE5e6b3a33/域名" alt="" data-href="https://域名/chic/jfs/t1/213416/13/1536/273782/617372daE5e6b3a33/域名" style=""/> <img src="https://域名/chic/jfs/t1/220138/15/1882/93119/61777d3dE61db5ddb/域名" alt="" data-href="https://域名/chic/jfs/t1/220138/15/1882/93119/61777d3dE61db5ddb/域名" style=""/> <img src="https://域名/chic/jfs/t1/218219/26/1554/286582/617372daE80c0bdce/域名" alt="" data-href="https://域名/chic/jfs/t1/218219/26/1554/286582/617372daE80c0bdce/域名" style=""/> <img src="https://域名/chic/jfs/t1/207798/25/6476/328695/617372daE4f770dd4/域名" alt="" data-href="https://域名/chic/jfs/t1/207798/25/6476/328695/617372daE4f770dd4/域名" style=""/> <img src="https://域名/chic/jfs/t1/207293/28/6562/355923/617372daEad2418ed/域名" alt="" data-href="https://域名/chic/jfs/t1/207293/28/6562/355923/617372daEad2418ed/域名" style=""/> <img src="https://域名/chic/jfs/t1/198472/7/14147/353512/617372daE475bfd78/域名" alt="" data-href="https://域名/chic/jfs/t1/198472/7/14147/353512/617372daE475bfd78/域名" style=""/> <img src="https://域名/chic/jfs/t1/208709/11/6409/385534/617372daE546bf141/域名" alt="" data-href="https://域名/chic/jfs/t1/208709/11/6409/385534/617372daE546bf141/域名" style=""/> <img src="https://域名/chic/jfs/t1/141557/22/23755/397718/617372daE0b82f73d/域名" alt="" data-href="https://域名/chic/jfs/t1/141557/22/23755/397718/617372daE0b82f73d/域名" style=""/> <img src="https://域名/chic/jfs/t1/169270/29/21329/416165/617372daE94e8b257/域名" alt="" data-href="https://域名/chic/jfs/t1/169270/29/21329/416165/617372daE94e8b257/域名" style=""/> <img src="https://域名/chic/jfs/t1/140728/1/25666/344248/617372dbE535662cb/域名" alt="" data-href="https://域名/chic/jfs/t1/140728/1/25666/344248/617372dbE535662cb/域名" style=""/> <img src="https://域名/chic/jfs/t1/171559/6/21235/423277/617372dbE9eb46235/域名" alt="" data-href="https://域名/chic/jfs/t1/171559/6/21235/423277/617372dbE9eb46235/域名" style=""/> <img src="https://域名/chic/jfs/t1/168858/37/22897/439146/617372dbEd82f4e2a/域名" alt="" data-href="https://域名/chic/jfs/t1/168858/37/22897/439146/617372dbEd82f4e2a/域名" style=""/> <img src="https://域名/chic/jfs/t1/205152/24/12749/493690/617372dbEf73dbe18/域名" alt="" data-href="https://域名/chic/jfs/t1/205152/24/12749/493690/617372dbEf73dbe18/域名" style=""/> <img src="https://域名/chic/jfs/t1/161214/15/21277/441293/617372dbE8e533234/域名" alt="" data-href="https://域名/chic/jfs/t1/161214/15/21277/441293/617372dbE8e533234/域名" style=""/> <img src="https://域名/chic/jfs/t1/218750/6/1519/383308/617372dbEa6274296/域名" alt="" data-href="https://域名/chic/jfs/t1/218750/6/1519/383308/617372dbEa6274296/域名" style=""/> <img src="https://域名/chic/jfs/t1/156858/19/21427/418698/617372dbEeca2b01f/域名" alt="" data-href="https://域名/chic/jfs/t1/156858/19/21427/418698/617372dbEeca2b01f/域名" style=""/> </p> </div><div class="w-e-content-container"> <p> <img src="https://域名/chic/jfs/t1/213416/13/1536/273782/617372daE5e6b3a33/域名" alt="" data-href="https://域名/chic/jfs/t1/213416/13/1536/273782/617372daE5e6b3a33/域名" style=""/> <img src="https://域名/chic/jfs/t1/220138/15/1882/93119/61777d3dE61db5ddb/域名" alt="" data-href="https://域名/chic/jfs/t1/220138/15/1882/93119/61777d3dE61db5ddb/域名" style=""/> <img src="https://域名/chic/jfs/t1/218219/26/1554/286582/617372daE80c0bdce/域名" alt="" data-href="https://域名/chic/jfs/t1/218219/26/1554/286582/617372daE80c0bdce/域名" style=""/> <img src="https://域名/chic/jfs/t1/207798/25/6476/328695/617372daE4f770dd4/域名" alt="" data-href="https://域名/chic/jfs/t1/207798/25/6476/328695/617372daE4f770dd4/域名" style=""/> <img src="https://域名/chic/jfs/t1/207293/28/6562/355923/617372daEad2418ed/域名" alt="" data-href="https://域名/chic/jfs/t1/207293/28/6562/355923/617372daEad2418ed/域名" style=""/> <img src="https://域名/chic/jfs/t1/198472/7/14147/353512/617372daE475bfd78/域名" alt="" data-href="https://域名/chic/jfs/t1/198472/7/14147/353512/617372daE475bfd78/域名" style=""/> <img src="https://域名/chic/jfs/t1/208709/11/6409/385534/617372daE546bf141/域名" alt="" data-href="https://域名/chic/jfs/t1/208709/11/6409/385534/617372daE546bf141/域名" style=""/> <img src="https://域名/chic/jfs/t1/141557/22/23755/397718/617372daE0b82f73d/域名" alt="" data-href="https://域名/chic/jfs/t1/141557/22/23755/397718/617372daE0b82f73d/域名" style=""/> <img src="https://域名/chic/jfs/t1/169270/29/21329/416165/617372daE94e8b257/域名" alt="" data-href="https://域名/chic/jfs/t1/169270/29/21329/416165/617372daE94e8b257/域名" style=""/> <img src="https://域名/chic/jfs/t1/140728/1/25666/344248/617372dbE535662cb/域名" alt="" data-href="https://域名/chic/jfs/t1/140728/1/25666/344248/617372dbE535662cb/域名" style=""/> <img src="https://域名/chic/jfs/t1/171559/6/21235/423277/617372dbE9eb46235/域名" alt="" data-href="https://域名/chic/jfs/t1/171559/6/21235/423277/617372dbE9eb46235/域名" style=""/> <img src="https://域名/chic/jfs/t1/168858/37/22897/439146/617372dbEd82f4e2a/域名" alt="" data-href="https://域名/chic/jfs/t1/168858/37/22897/439146/617372dbEd82f4e2a/域名" style=""/> <img src="https://域名/chic/jfs/t1/205152/24/12749/493690/617372dbEf73dbe18/域名" alt="" data-href="https://域名/chic/jfs/t1/205152/24/12749/493690/617372dbEf73dbe18/域名" style=""/> <img src="https://域名/chic/jfs/t1/161214/15/21277/441293/617372dbE8e533234/域名" alt="" data-href="https://域名/chic/jfs/t1/161214/15/21277/441293/617372dbE8e533234/域名" style=""/> <img src="https://域名/chic/jfs/t1/218750/6/1519/383308/617372dbEa6274296/域名" alt="" data-href="https://域名/chic/jfs/t1/218750/6/1519/383308/617372dbEa6274296/域名" style=""/> <img src="https://域名/chic/jfs/t1/156858/19/21427/418698/617372dbEeca2b01f/域名" alt="" data-href="https://域名/chic/jfs/t1/156858/19/21427/418698/617372dbEeca2b01f/域名" style=""/> </p> </div><div class="w-e-content-container"> <p> <img src="https://域名/chic/jfs/t1/213416/13/1536/273782/617372daE5e6b3a33/域名" alt="" data-href="https://域名/chic/jfs/t1/213416/13/1536/273782/617372daE5e6b3a33/域名" style=""/> <img src="https://域名/chic/jfs/t1/220138/15/1882/93119/61777d3dE61db5ddb/域名" alt="" data-href="https://域名/chic/jfs/t1/220138/15/1882/93119/61777d3dE61db5ddb/域名" style=""/> <img src="https://域名/chic/jfs/t1/218219/26/1554/286582/617372daE80c0bdce/域名" alt="" data-href="https://域名/chic/jfs/t1/218219/26/1554/286582/617372daE80c0bdce/域名" style=""/> <img src="https://域名/chic/jfs/t1/207798/25/6476/328695/617372daE4f770dd4/域名" alt="" data-href="https://域名/chic/jfs/t1/207798/25/6476/328695/617372daE4f770dd4/域名" style=""/> <img src="https://域名/chic/jfs/t1/207293/28/6562/355923/617372daEad2418ed/域名" alt="" data-href="https://域名/chic/jfs/t1/207293/28/6562/355923/617372daEad2418ed/域名" style=""/> <img src="https://域名/chic/jfs/t1/198472/7/14147/353512/617372daE475bfd78/域名" alt="" data-href="https://域名/chic/jfs/t1/198472/7/14147/353512/617372daE475bfd78/域名" style=""/> <img src="https://域名/chic/jfs/t1/208709/11/6409/385534/617372daE546bf141/域名" alt="" data-href="https://域名/chic/jfs/t1/208709/11/6409/385534/617372daE546bf141/域名" style=""/> <img src="https://域名/chic/jfs/t1/141557/22/23755/397718/617372daE0b82f73d/域名" alt="" data-href="https://域名/chic/jfs/t1/141557/22/23755/397718/617372daE0b82f73d/域名" style=""/> <img src="https://域名/chic/jfs/t1/169270/29/21329/416165/617372daE94e8b257/域名" alt="" data-href="https://域名/chic/jfs/t1/169270/29/21329/416165/617372daE94e8b257/域名" style=""/> <img src="https://域名/chic/jfs/t1/140728/1/25666/344248/617372dbE535662cb/域名" alt="" data-href="https://域名/chic/jfs/t1/140728/1/25666/344248/617372dbE535662cb/域名" style=""/> <img src="https://域名/chic/jfs/t1/171559/6/21235/423277/617372dbE9eb46235/域名" alt="" data-href="https://域名/chic/jfs/t1/171559/6/21235/423277/617372dbE9eb46235/域名" style=""/> <img src="https://域名/chic/jfs/t1/168858/37/22897/439146/617372dbEd82f4e2a/域名" alt="" data-href="https://域名/chic/jfs/t1/168858/37/22897/439146/617372dbEd82f4e2a/域名" style=""/> <img src="https://域名/chic/jfs/t1/205152/24/12749/493690/617372dbEf73dbe18/域名" alt="" data-href="https://域名/chic/jfs/t1/205152/24/12749/493690/617372dbEf73dbe18/域名" style=""/> <img src="https://域名/chic/jfs/t1/161214/15/21277/441293/617372dbE8e533234/域名" alt="" data-href="https://域名/chic/jfs/t1/161214/15/21277/441293/617372dbE8e533234/域名" style=""/> <img src="https://域名/chic/jfs/t1/218750/6/1519/383308/617372dbEa6274296/域名" alt="" data-href="https://域名/chic/jfs/t1/218750/6/1519/383308/617372dbEa6274296/域名" style=""/> <img src="https://域名/chic/jfs/t1/156858/19/21427/418698/617372dbEeca2b01f/域名" alt="" data-href="https://域名/chic/jfs/t1/156858/19/21427/418698/617372dbEeca2b01f/域名" style=""/> </p> </div><div class="w-e-content-container"> <p> <img src="https://域名/chic/jfs/t1/213416/13/1536/273782/617372daE5e6b3a33/域名" alt="" data-href="https://域名/chic/jfs/t1/213416/13/1536/273782/617372daE5e6b3a33/域名" style=""/> <img src="https://域名/chic/jfs/t1/220138/15/1882/93119/61777d3dE61db5ddb/域名" alt="" data-href="https://域名/chic/jfs/t1/220138/15/1882/93119/61777d3dE61db5ddb/域名" style=""/> <img src="https://域名/chic/jfs/t1/218219/26/1554/286582/617372daE80c0bdce/域名" alt="" data-href="https://域名/chic/jfs/t1/218219/26/1554/286582/617372daE80c0bdce/域名" style=""/> <img src="https://域名/chic/jfs/t1/207798/25/6476/328695/617372daE4f770dd4/域名" alt="" data-href="https://域名/chic/jfs/t1/207798/25/6476/328695/617372daE4f770dd4/域名" style=""/> <img src="https://域名/chic/jfs/t1/207293/28/6562/355923/617372daEad2418ed/域名" alt="" data-href="https://域名/chic/jfs/t1/207293/28/6562/355923/617372daEad2418ed/域名" style=""/> <img src="https://域名/chic/jfs/t1/198472/7/14147/353512/617372daE475bfd78/域名" alt="" data-href="https://域名/chic/jfs/t1/198472/7/14147/353512/617372daE475bfd78/域名" style=""/> <img src="https://域名/chic/jfs/t1/208709/11/6409/385534/617372daE546bf141/域名" alt="" data-href="https://域名/chic/jfs/t1/208709/11/6409/385534/617372daE546bf141/域名" style=""/> <img src="https://域名/chic/jfs/t1/141557/22/23755/397718/617372daE0b82f73d/域名" alt="" data-href="https://域名/chic/jfs/t1/141557/22/23755/397718/617372daE0b82f73d/域名" style=""/> <img src="https://域名/chic/jfs/t1/169270/29/21329/416165/617372daE94e8b257/域名" alt="" data-href="https://域名/chic/jfs/t1/169270/29/21329/416165/617372daE94e8b257/域名" style=""/> <img src="https://域名/chic/jfs/t1/140728/1/25666/344248/617372dbE535662cb/域名" alt="" data-href="https://域名/chic/jfs/t1/140728/1/25666/344248/617372dbE535662cb/域名" style=""/> <img src="https://域名/chic/jfs/t1/171559/6/21235/423277/617372dbE9eb46235/域名" alt="" data-href="https://域名/chic/jfs/t1/171559/6/21235/423277/617372dbE9eb46235/域名" style=""/> <img src="https://域名/chic/jfs/t1/168858/37/22897/439146/617372dbEd82f4e2a/域名" alt="" data-href="https://域名/chic/jfs/t1/168858/37/22897/439146/617372dbEd82f4e2a/域名" style=""/> <img src="https://域名/chic/jfs/t1/205152/24/12749/493690/617372dbEf73dbe18/域名" alt="" data-href="https://域名/chic/jfs/t1/205152/24/12749/493690/617372dbEf73dbe18/域名" style=""/> <img src="https://域名/chic/jfs/t1/161214/15/21277/441293/617372dbE8e533234/域名" alt="" data-href="https://域名/chic/jfs/t1/161214/15/21277/441293/617372dbE8e533234/域名" style=""/> <img src="https://域名/chic/jfs/t1/218750/6/1519/383308/617372dbEa6274296/域名" alt="" data-href="https://域名/chic/jfs/t1/218750/6/1519/383308/617372dbEa6274296/域名" style=""/> <img src="https://域名/chic/jfs/t1/156858/19/21427/418698/617372dbEeca2b01f/域名" alt="" data-href="https://域名/chic/jfs/t1/156858/19/21427/418698/617372dbEeca2b01f/域名" style=""/> </p> </div>"\'));
3.查询结果
select UNCOMPRESS(content) from  `test_compress`;
SELECT UNCOMPRESSED_LENGTH(content) AS length, LENGTH(content) AS compress_length, UNCOMPRESS(content), content FROM `test_compress`

 

 对比一下压缩前后的数据长度:2万多的字符对应varchar的长度达到911

从上面截图可以看出压缩效果比较好,针对 text、char、varchr、blob 等,如果里面重复的数据越多压缩效果就越好。

三、InnoDB 表压缩方案解决方案

1、适用场景

采用压缩表一般都用在由于数据量太大,磁盘空间不足,负载主要体现在 IO 上,而服务器的 CPU 又有比较多的余量的场景。

2、表压缩简介

a、为什么需要压缩

目前很多表都支持压缩,比如 Myisam、InnoDB、TokuDB、MyRocks 。由于使用 InnoDB 主要是不需要做什么改动,对线上完全透明,压缩方案也非常成熟,因此这里只对 InnoDB 做详细说明。

innodb 的压缩介绍

使用 innodb 压缩的前提条件是,innodb_file_per_table 这个参数要启用,innodb_file_format 这个参数设置成 Barracuda。

你可以使用 ROW_FORMAT=COMPRESSED 来 create 或者 alter 表来开启 innodb 的压缩功能,如果没有指定 KEY_BLOCK_SIZE 的大小,默认 KEY_BLOCK_SIZE 为 innodb_page_size 大小的一半,也可以通过指定 KEY_BLOCK_SIZE=n 参数来开启 innodb 的压缩功能,n 可以为 1、2、4、8、16,单位是 K。n 的值越小,压缩比越高,消耗的 CPU 资源也越多。注意 32K 或者 64K 的页不支持压缩。启用压缩后,索引数据也同样会被压缩。

你也可以通过调整 innodb_compression_level 来设置压缩的级别,级别从 1~9,默认是 6。级别越低,意味着压缩比越高,同时也意味着需要更多的 CPU 资源。

c、压缩算法

innodb 压缩借助的是著名的 zlib 库,采用 L777 压缩算法,这种算法在减少数据大小和 CPU 利用方面很成熟高效。同时这种算法是无损的,因此原生的未压缩的数据总是能够从压缩文件中重构,LZ777 实现原理是查找重复数据的序列号然后进行压缩,所以数据模式决定了压缩效率,一般而言,用户的数据能够被压缩 50%以上。

d、压缩表在 buffer_pool 中如何处理

在 buffer_pool 缓冲池中,压缩的数据通过 KEY_BLOCK_SIZE 的大小的页来保存,如果要提取压缩的数据或者要更新压缩数据对应的列,则会创建一个未压缩页来解压缩数据,然后在数据更新完成后,会将为压缩页的数据重新写入到压缩页中。内存不足的时候,MySQL 会讲对应的未压缩页踢出去。因此如果你启用了压缩功能,你的 buffer_pool 缓冲池中可能会存在压缩页和未压缩页,也可能只存在压缩页。不过可能仍然需要将你的 buffer_pool 缓冲池调大,以便能同时能保存压缩页和未压缩页。

MySQL 采用最少使用(LRU)算法来确定将哪些页保留在内存中,哪些页剔除出去,因此热数据会更多地保留在内存中。当压缩表被访问的时候,MySQL 使用自适应的 LRU 算法来维持内存中压缩页和非压缩页的平衡。当系统 IO 负载比较高的时候,这种算法倾向于讲未压缩的页剔除,一面腾出更多的空间来存放更多的压缩页。当系统 CPU 负载比较高的时候,MySQL 倾向于将压缩页和未压缩页都剔除出去,这个时候更多的内存用来保留热的数据,从而减少解压的操作。

e、如何评估 KEY_BLOCK_SIZE 是否合适

为了更深入地了解压缩表对性能的影响,在 Information Schema 库中有对应的表可以用来评估内存的使用和压缩率等指标。INNODB_CMP 是收集的是某一类的 KEY_BLOCK_SIZE 压缩表的整体状况的信息,汇总的是所有 KEY_BLOCK_SIZE 压缩表的统计。而 INNODB_CMP_PER_INDEX 表则是收集各个表和索引的压缩情况信息,这些信息对于在某个时间评估某个表的压缩效率或者诊断性能问题很有帮助。INNODB_CMP_PER_INDEX 表的收集会导致系统性能受到影响,必须 innodb_cmp_per_index_enabled 选项才会记录,生产环境最好不要开启。

我们可以通过观察 INNODB_CMP 表的压缩失败情况,如果失败比较多,则需要调大 KEY_BLOCK_SIZE。一般建议 KEY_BLOCK_SIZE 设置为 8。

3、方案实践

a、设置好 innodb_file_per_table 和 innodb_file_format 参数

SET GLOBAL innodb_file_per_table=1;SET GLOBAL innodb_file_format=Barracuda;

b、创建对应的压缩表

CREATE TABLE compress_test (c1 INT PRIMARY KEY,content varchar(255)) ROW_FORMAT=COMPRESSEDKEY_BLOCK_SIZE=8;

如果是已经存在的表,则通过 alter 来修改,SQL 如下:

ALTER TABLE compress_test ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8;

4、压缩效果

压缩效果通过线上的一个监控的表修改为压缩后的文件大小来说明,压缩前后对比如下:

标签:编程
湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。