Innodb表压缩过程当中遇到的坑(innodb_file_format) - billy鹏

原文地址:http://www.cnblogs.com/billyxp/p/3342969.htmlhtml

对于愈来愈多的数据,数据库的容量愈来愈大,压缩也就愈来愈常见了。在个人实际工做中进行过屡次压缩工做,也遇到屡次问题,在此和你们分享一下。mysql

首先,咱们先说说怎么使用innodb的压缩.sql

第一,mysql的版本须要大于5.5第二,设置innodb_file_format=barracuda
第三,create table或者alter talble 增长 ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8;(默认的key_block_size=16)

其实很简单,根据经验,通常压缩比例能够达到30%-40%数据库

而后,咱们说说我在压缩过程当中遇到的坑和发现的关联,固然有些比较二。session

No1:app

问题:使用脚本批量alter操做,只动态修改了实例的innodb_file_format=barracuda,而后alter全部数据库中的表。并无修改配置文件中的设置。ide

结果:表中已有数据被压缩,可是在重启以后,因为innodb_file_format参数被从新修改为antelope,致使后续写入的数据没有被压缩(虽然表结构中有row_format=compressed,可是不会起做用),最终表体积仍然很大。测试

教训:实例和配置文件要同步修改。(这个错误最二,过低级 T_T,不解释了。)ui

No2:this

问题:在innodb_file_format=antelope的状况下,创建压缩表(表结构中带有row_format=compressed),而后在设置innodb_file_format=barracuda。

结果:表结构中的row_format=compressed被忽略,后续写入表的数据并无被压缩,最终致使表体积大。

教训:先修改innodb_file_format(session和global都须要修改),在create table或者alter table。

可是以上这点有个坑人的地方,在错误的顺序下,表是能够被成功创建了,只是会有warning,可是表结构中会有row_format=compressed,在后期排查的时候很是误导人!

+--------------------------+----------+| Variable_name            | Value    |+--------------------------+----------+| innodb_file_format       | Antelope || innodb_file_format_check | ON       || innodb_file_format_max   | Antelope |+--------------------------+----------+3 rows in set (0.00 sec)

test> create table test_1 (x int) ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8; 
Query OK, 0 rows affected, 4 warnings (0.07 sec)

 

test> show warnings; 
+---------+------+-----------------------------------------------------------------------+ 
| Level | Code | Message | 
+---------+------+-----------------------------------------------------------------------+ 
| Warning | 1478 | InnoDB: KEY_BLOCK_SIZE requires innodb_file_format > Antelope . | 
| Warning | 1478 | InnoDB: ignoring KEY_BLOCK_SIZE=8 . | 
| Warning | 1478 | InnoDB: ROW_FORMAT=COMPRESSED requires innodb_file_format > Antelope . | 
| Warning | 1478 | InnoDB: assuming ROW_FORMAT=COMPACT . | 
+---------+------+-----------------------------------------------------------------------+ 
4 rows in set (0.00 sec)

咱们能够从warnings中看见,压缩设置被忽略了。可是最坑爹的一点是,若是咱们show create table会有以下结果:

test_1 | CREATE TABLE `test_1` (  `x` int(11) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8

在这种状况下,咱们吸收教训,不能使用show create table看压缩状态,而是应该用show table status;

show table status like 'test_1'\G;*************************** 1. row ***************************
           Name: test_1
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 0
 Avg_row_length: 0
    Data_length: 16384Max_data_length: 0
   Index_length: 0
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2013-09-27 15:59:13
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: row_format=COMPRESSED KEY_BLOCK_SIZE=8
        Comment:1 row in set (0.00 sec)

坑爹啊,不说了。正常应该这个样子

show table status like 'test_2'\G;*************************** 1. row ***************************
           Name: test_2
         Engine: InnoDB
        Version: 10
     Row_format: Compressed
           Rows: 0
 Avg_row_length: 0
    Data_length: 8192Max_data_length: 0
   Index_length: 0
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2013-09-27 16:09:51
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: row_format=COMPRESSED KEY_BLOCK_SIZE=8
        Comment:1 row in set (0.00 sec)

No3:

发现和innodb_file_format相关的2个参数:

+--------------------------+-----------+| Variable_name            | Value     |+--------------------------+-----------+| innodb_file_format       | Barracuda || innodb_file_format_check | ON        || innodb_file_format_max   | Barracuda |+--------------------------+-----------+3 rows in set (0.00 sec)

官方的解释能够参考以下的连接:http://dev.mysql.com/doc/refman/5.5/en/innodb-parameters.html#sysvar_innodb_file_format

测试过程当中发现,若是是innodb_file_format=barracuda而innodb_file_format_max=antelop,那么在创建压缩表的时候,max会自动变成barracuda。

localhost.test>show global variables like 'innodb_file_format%';+--------------------------+-----------+| Variable_name            | Value     |+--------------------------+-----------+| innodb_file_format       | Barracuda || innodb_file_format_check | ON        || innodb_file_format_max   | Antelope  |+--------------------------+-----------+3 rows in set (0.00 sec)

localhost.test>create table test_4(x int) ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8;Query OK, 0 rows affected (0.01 sec)

localhost.test>show global variables like 'innodb_file_format%';+--------------------------+-----------+| Variable_name            | Value     |+--------------------------+-----------+| innodb_file_format       | Barracuda || innodb_file_format_check | ON        || innodb_file_format_max   | Barracuda |+--------------------------+-----------+3 rows in set (0.00 sec)

若是innodb_file_format_check这参数解释的,决定innodb是否会检查共享表空间中的表格式的tag,若是检查开启,那么当标记的表格式的tag高于innodb能够支撑的表格式,那么innodb会报错,并中止启动。若是支持,那么会将innodb_file_format_max的值改成这个tag的值。

如下是参考材料


    • innodb_file_format  

  • Command-Line Format

  • --innodb_file_format=#

  • Option-File Format

  • innodb_file_format

  • System Variable Name

  • Variable Scope

  • Global

  • Dynamic Variable

  • Yes

  •  

  • Permitted Values (<= 5.5.6)

  • Type

  • string

  • Default

  • Barracuda

  • Valid Values

  • Antelope

  • Barracuda

  •  

  • Permitted Values (>= 5.5.7)

  • Type

  • string

  • Default

  • Antelope

  • Valid Values

  • Antelope

  • Barracuda


    • The file format to use for new InnoDB tables. Currently, Antelope andBarracuda are supported. This applies only for tables that have their own tablespace, so for it to have an effect, innodb_file_per_table must be enabled. The Barracuda file format is required for certain InnoDB features such as table compression. 

    • innodb_file_format_check  

  • Command-Line Format

  • --innodb_file_format_check=#

  • Option-File Format

  • innodb_file_format_check

  • System Variable Name

  • Variable Scope

  • Global

  • Dynamic Variable

  • No

  •  

  • Permitted Values

  • Type

  • string

  • Default

  • Antelope

  •  

  • Permitted Values (>= 5.5.1)

  • Type

  • string

  • Default

  • Barracuda

  •  

  • Permitted Values (>= 5.5.5)

  • Type

  • boolean

  • Default

  • ON


    • As of MySQL 5.5.5, this variable can be set to 1 or 0 at server startup to enable or disable whether InnoDB checks the file format tag in the shared tablespace (for example, Antelope or Barracuda ). If the tag is checked and is higher than that supported by the current version of InnoDB , an error occurs and InnoDBdoes not start. If the tag is not higher, InnoDB sets the value ofinnodb_file_format_max to the file format tag. 
      Before MySQL 5.5.5, this variable can be set to 1 or 0 at server startup to enable or disable whether InnoDB checks the file format tag in the shared tablespace. If the tag is checked and is higher than that supported by the current version ofInnoDB , an error occurs and InnoDB does not start. If the tag is not higher,InnoDB sets the value of innodb_file_format_check to the file format tag, which is the value seen at runtime. 
      Note 
      Despite the default value sometimes being displayed as ON or OFF , always use the numeric values 1 or 0 to turn this option on or off in your configuration file or command line. 

    • innodb_file_format_max  

  • Introduced

  • 5.5.5

  • Command-Line Format

  • --innodb_file_format_max=#

  • Option-File Format

  • innodb_file_format_max

  • System Variable Name

  • Variable Scope

  • Global

  • Dynamic Variable

  • Yes

  •  

  • Permitted Values

  • Type

  • string

  • Default

  • Antelope

  • Valid Values

  • Antelope

  • Barracuda


    • At server startup, InnoDB sets the value of innodb_file_format_max to the file format tag in the shared tablespace (for example, Antelope or Barracuda ). If the server creates or opens a table with a “higher” file format, it sets the value ofinnodb_file_format_max to that format. This variable was added in MySQL 5.5.5.

相关文章
相关标签/搜索