导致统计信息不准确的原因是什么呢?其实是MySQL 8.0为了提高information_schema的查询效率,将视图tables和statistics里面的统计信息缓存起来,缓存过期时间由参数information_schema_stats_expiry决定,默认为86400s;如果想获取最新的统计信息,可以通过如下两种方式:
(1)analyze table进行表分析
(2)设置information_schema_stats_expiry=0
use mysql;
SET GLOBAL information_schema_stats_expiry=0;
SET @@GLOBAL.information_schema_stats_expiry=0;
SET SESSION information_schema_stats_expiry=0;
SET @@SESSION.information_schema_stats_expiry=0;
use information_schema;
-- select sum(table_rows) from tables where TABLE_SCHEMA = "limesurvey" order by table_rows asc;
select table_name,table_rows from tables
where TABLE_SCHEMA = 'limesurvey' and table_rows>0
order by table_name ;