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) 统计信息和真实数据一致
统计信息查看
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: 物料**分配
--索引信息查看
select distinct s.*
from information_schema.statistics s
left outer join information_schema.table_constraints t
on t.table_schema = s.table_schema
and t.table_name = s.table_name
and s.index_name = t.constraint_name
where t.constraint_name is null and s.table_name='fa_ledger_b_asgmt' ORDER BY s.INDEX_NAME;