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为止,此举避免了统计信息动态更新,保证了执行计划的稳定,对于大表也节省了收集统计信息的所需资源;

查询表的统计信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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)
统计信息和真实数据一致

  

统计信息查看

select * from mysql.innodb_index_stats   where table_name ='productapplyrange';

select * from mysql.innodb_table_stats    where table_name ='productapplyrange';

 

show table status from iuap_apdoc_coredoc like 'productapplyrange'\G
*************************** 1. row ***************************
Name: productapplyrange
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 282588981
Avg_row_length: 168
Data_length: 47507832832
Max_data_length: 0
Index_length: 146502991872
Data_free: 7340032
Auto_increment: 2053328773908006371
Create_time: 2028-02-28 20:43:46
Update_time: 2028-07-29 15:42:31
Check_time: NULL
Collation: utf8mb4_general_ci
Checksum: NULL
Create_options: row_format=DYNAMIC
Comment: 物料**分配

 

posted @   刚好遇见Mysql  阅读(11319)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
点击右上角即可分享
微信分享提示