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