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

     

    發表評論
    所有評論
    還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
    相關文章