MySQL 5.7 ANALYZE TABLE分析索引的統計資訊

feelpurple發表於2016-06-06
在生產環境中,索引的更新操作可能非常頻繁。如果再每次索引發生更新操作時就對其進行Cardinality的統計,那麼將會對資料庫帶來很大的負擔。另外需要考慮的是,如果一張表的資料量非常大,比如一張表有50GB的資料,
那麼統計一次Cardinality資訊所需要的時間可能非常長。這在生產環境的應用中也是不能接受的。因此,資料庫對於Cardinality的統計都是透過取樣(sample)的方法來完成的。
在InnoDB儲存引擎中,Cardinality統計資訊的更新發生在兩個操作中:INSERT和UPDATE。根據前面的敘述,不可能在每次發生INSERT和UPDATE時都去更新Cardinality的資訊,
這會增加資料庫系統的負荷,同事對大表進行統計時,時間上也不允許。
因此InnoDB儲存引擎對於更新Cardinality資訊的策略為:
表中1/16的資料已發生變化
stat_modified_counter > 2 000 000 000

ANALYZE TABLE語句分析並儲存表中索引的分佈情況。對於InnoDB、MyISAM儲存引擎,在分析中,表會被加只讀鎖。這個語句適用於InnoDB、NDB和MyISAM表。對於MyISAM表,這個語句等同於myisamchk --analyze。這個語句對

檢視無效。

在執行非constant型別的表連線時,MySQL使用儲存的索引分佈資訊來決定表的關聯順序。另外,索引的分佈情況也可以用來決定在查詢中使用哪個表的特定索引。

執行這個語句需要對指定表的SELECT和INSERT許可權。

對於分割槽表,需要執行ALTER TABLE ... ANALYZE PARTITION語句。

在MySQL 5.7.1,在執行ANALYZE TABLE語句之前,需要將gtid_next引數設為AUTOMATIC。這個限制在MySQL 5.7.2版本以後被修復。

可以使用SHOW INDEX語句來顯示索引的分佈情況。

預設,MySQL服務會將ANALYZE TABLE語句寫到二進位制日誌中,並傳輸到複製環境中的slave節點。想關閉這個日誌記錄,可以在語句中加上NO_WRITE_TO_BINLOG或LOCAL。

--分析非分割槽表的索引分佈情況

mysql> show index from dept;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| dept  |          0 | PRIMARY  |            1 | DEPTNO      | A         |           4 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

mysql> show keys from dept;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| dept  |          0 | PRIMARY  |            1 | DEPTNO      | A         |           4 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

mysql> analyze table  dept;
+-----------+---------+----------+----------+
| Table     | Op      | Msg_type | Msg_text |
+-----------+---------+----------+----------+
| fire.dept | analyze | status   | OK       |
+-----------+---------+----------+----------+
1 row in set (0.03 sec)

mysql> show index from dept;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| dept  |          0 | PRIMARY  |            1 | DEPTNO      | A         |           4 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

--分析分割槽表的索引分佈情況

mysql> select table_schema,table_name,partition_name from  information_schema.partitions where table_name = 'trb1';
+--------------+------------+----------------+
| table_schema | table_name | partition_name |
+--------------+------------+----------------+
| fire         | trb1       | p0             |
| fire         | trb1       | p1             |
| fire         | trb1       | p2             |
| fire         | trb1       | p3             |
+--------------+------------+----------------+
4 rows in set (0.00 sec)

mysql> show index from trb1;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| trb1  |          0 | PRIMARY  |            1 | id          | A         |          10 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

mysql> alter table trb1 analyze partition p0;
+-----------+---------+----------+----------+
| Table     | Op      | Msg_type | Msg_text |
+-----------+---------+----------+----------+
| fire.trb1 | analyze | status   | OK       |
+-----------+---------+----------+----------+
1 row in set (0.03 sec)

mysql> alter table trb1 analyze partition p1;
+-----------+---------+----------+----------+
| Table     | Op      | Msg_type | Msg_text |
+-----------+---------+----------+----------+
| fire.trb1 | analyze | status   | OK       |
+-----------+---------+----------+----------+
1 row in set (0.00 sec)

mysql> alter table trb1 analyze partition p2;
+-----------+---------+----------+----------+
| Table     | Op      | Msg_type | Msg_text |
+-----------+---------+----------+----------+
| fire.trb1 | analyze | status   | OK       |
+-----------+---------+----------+----------+
1 row in set (0.01 sec)

mysql> alter table trb1 analyze partition p3;
+-----------+---------+----------+----------+
| Table     | Op      | Msg_type | Msg_text |
+-----------+---------+----------+----------+
| fire.trb1 | analyze | status   | OK       |
+-----------+---------+----------+----------+
1 row in set (0.00 sec)

mysql> show index from trb1;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| trb1  |          0 | PRIMARY  |            1 | id          | A         |          10 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2115026/,如需轉載,請註明出處,否則將追究法律責任。

相關文章