[英]mysql analyze table results in odd behavior
我注意到運行時得到的表總計:
SELECT TABLE_NAME, TABLE_ROWS FROM `information_schema`.`tables` WHERE `table_schema` = 'my_database';
導致總數不正確。 我還讀到這樣做:
analyze table 'my_table';
將正確更新信息。 但似乎並非如此。 因此,要設置基准:
mysql> select count(*) from my_table;
+----------+
| count(*) |
+----------+
| 61782 |
+----------+
1 row in set (0.01 sec)
接下來,統計數據顯示:
mysql> SELECT TABLE_NAME, TABLE_ROWS FROM `information_schema`.`tables` WHERE `table_schema` = 'my_database' AND TABLE_NAME = 'my_table';
+--------------------+------------+
| TABLE_NAME | TABLE_ROWS |
+--------------------+------------+
| my_table | 58378 |
+--------------------+------------+
1 row in set (0.00 sec)
顯然,那是不對的,所以我進行分析並再次檢查:
mysql> analyze table my_table;
+------------------------------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------------------+---------+----------+----------+
| my_database.my_table | analyze | status | OK |
+------------------------------+---------+----------+----------+
1 row in set (0.02 sec)
mysql> SELECT TABLE_NAME, TABLE_ROWS FROM `information_schema`.`tables` WHERE `table_schema` = 'my_database' AND TABLE_NAME = 'my_table';
+--------------------+------------+
| TABLE_NAME | TABLE_ROWS |
+--------------------+------------+
| my_table | 56439 |
+--------------------+------------+
1 row in set (0.00 sec)
好的,它改變了,但是仍然是錯誤的。 所以我做了幾次:
mysql> analyze table my_table;
+------------------------------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------------------+---------+----------+----------+
| my_database.my_table | analyze | status | OK |
+------------------------------+---------+----------+----------+
1 row in set (0.02 sec)
mysql> SELECT TABLE_NAME, TABLE_ROWS FROM `information_schema`.`tables` WHERE `table_schema` = 'my_database' AND TABLE_NAME = 'my_table';
+--------------------+------------+
| TABLE_NAME | TABLE_ROWS |
+--------------------+------------+
| my_table | 58766 |
+--------------------+------------+
1 row in set (0.00 sec)
mysql> analyze table my_table;
+------------------------------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------------------+---------+----------+----------+
| my_database.my_table | analyze | status | OK |
+------------------------------+---------+----------+----------+
1 row in set (0.02 sec)
mysql> SELECT TABLE_NAME, TABLE_ROWS FROM `information_schema`.`tables` WHERE `table_schema` = 'my_database' AND TABLE_NAME = 'my_table';
+--------------------+------------+
| TABLE_NAME | TABLE_ROWS |
+--------------------+------------+
| my_table | 65749 |
+--------------------+------------+
1 row in set (0.00 sec)
mysql> analyze table my_table;
+------------------------------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------------------+---------+----------+----------+
| my_database.my_table | analyze | status | OK |
+------------------------------+---------+----------+----------+
1 row in set (0.02 sec)
mysql> SELECT TABLE_NAME, TABLE_ROWS FROM `information_schema`.`tables` WHERE `table_schema` = 'my_database' AND TABLE_NAME = 'my_table';
+--------------------+------------+
| TABLE_NAME | TABLE_ROWS |
+--------------------+------------+
| my_table | 61870 |
+--------------------+------------+
1 row in set (0.00 sec)
如您所見,我從來沒有得到相同的數字,而且它們都不是實際的行數。 我的問題是:這是否表明我的數據庫有問題(在所有表上都存在問題),或者這僅僅是MySQL中的錯誤?
我的環境的詳細信息:
服務器版本:5.6.43 MySQL Community Server(GPL)所有表均為INNODB,字符集為utf8
對於InnoDB, table_rows
統計信息是估計值 ,而不是精確的計數。 該值不能保證是確切的行數。
不,這不是MySQL中的錯誤。 InnoDB統計信息沒有錯。 行為記錄在《 MySQL參考手冊》中。
參考文獻:
https://dev.mysql.com/doc/refman/5.6/zh-CN/index-statistics.html
https://dev.mysql.com/doc/refman/5.6/zh-CN/innodb-persistent-stats.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.