一、手动
执行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,将analyz