使用 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 ;

     


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM