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