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.

相關文章
相關標籤/搜索