mysql 使用 table_rows 统计表格行数不准确

使用 table_rows 统计表格行数不准确

 
  • 首先生产环境不建议这样做,只是为了测试
  • 导致统计信息不准确的原因是什么呢?其实是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 ;

posted @ 2021-10-29 09:24  yongqi-911  阅读(1609)  评论(0编辑  收藏  举报