mysql收集統計信息


一、手動  
執行Analyze table 
innodb和myisam存儲引擎都可以通過執行“Analyze table tablename”來收集表的統計信息,除非執行計划不准確,否則不要輕易執行該操作,如果是很大的表該操作會影響表的性能。

二、自動觸發
以下行為會自動觸發統計信息的收集

1.第一次打開表的時候
2.表修改的行超過1/6或者20億條時
3.當有新的記錄插入時
4.執行show index from tablename或者執行show table stauts、查詢information_schema.tables\statistics 時


三、開啟參數innodb_stats_on_metadata
當開啟參數innodb_stats_on_metadata后訪問以下表也會觸發統計信息的收集
在訪問以下表時,innodb表的統計信息可自動收集
information_schema.TABLES
information_schema.STATISTICS
information_schema.PARTITIONS
information_schema.KEY_COLUMN_USAGE
information_schema.TABLE_CONSTRAINTS
information_schema.REFERENTIAL_CONSTRAINTS
information_schema.table_constraints


參數說明:
Innodb_stats_sample_pages:每次收集統計信息時采樣的頁數,默認為20
innodb_stats_persistent:默認on,將analyze table產生的統計信息保存於磁盤,直至下次analyze table為止,此舉避免了統計信息動態更新,保證了執行計划的穩定,對於大表也節省了收集統計信息的所需資源;

查詢表的統計信息

mysql> select  TABLE_SCHEMA,table_name,column_name,CARDINALITY from information_schema.STATISTICS  b where  b.table_name='salaries';
+--------------+------------+-------------+-------------+
| TABLE_SCHEMA | table_name | column_name | CARDINALITY |
+--------------+------------+-------------+-------------+
| employees    | salaries   | emp_no      |     2843953 |
| employees    | salaries   | from_date   |     2843953 |
+--------------+------------+-------------+-------------+
2 rows in set (0.00 sec)
mysql> show index from employees.salaries;
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table    | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| salaries |          0 | PRIMARY  |            1 | emp_no      | A         |     2843953 |     NULL | NULL   |      | BTREE      |         |               |
| salaries |          0 | PRIMARY  |            2 | from_date   | A         |     2843953 |     NULL | NULL   |      | BTREE      |         |               |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> select table_rows from information_schema.tables where table_name='salaries';
+------------+
| table_rows |
+------------+
|    2843953 |
+------------+
1 row in set (0.00 sec)
mysql> select count(1)  from  employees.salaries;
+----------+
| count(1) |
+----------+
|  2844047 |
+----------+
1 row in set (0.52 sec)
表的真實數據為 2844047
手動分析下表在看統計信息
mysql> analyze table employees.salaries;
+--------------------+---------+----------+----------+
| Table              | Op      | Msg_type | Msg_text |
+--------------------+---------+----------+----------+
| employees.salaries | analyze | status   | OK       |
+--------------------+---------+----------+----------+
1 row in set (0.50 sec)

mysql> select table_rows from information_schema.tables where table_name='salaries';
+------------+
| table_rows |
+------------+
|    2838489 |
+------------+
1 row in set (0.01 sec)

mysql> select  TABLE_SCHEMA,table_name,column_name,CARDINALITY from information_schema.STATISTICS  b where  b.table_name='salaries';
+--------------+------------+-------------+-------------+
| TABLE_SCHEMA | table_name | column_name | CARDINALITY |
+--------------+------------+-------------+-------------+
| employees    | salaries   | emp_no      |     2838489 |
| employees    | salaries   | from_date   |     2838489 |
+--------------+------------+-------------+-------------+
2 rows in set (0.00 sec)

mysql> show index from employees.salaries;
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table    | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| salaries |          0 | PRIMARY  |            1 | emp_no      | A         |     2838489 |     NULL | NULL   |      | BTREE      |         |               |
| salaries |          0 | PRIMARY  |            2 | from_date   | A         |     2838489 |     NULL | NULL   |      | BTREE      |         |               |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
統計信息和真實數據一致

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM