MySQL分析表结果奇怪的行为

时间:2019-02-01 17:48:48

标签: mysql

我注意到运行时得到的表总计:

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社区服务器(GPL) 所有表都是INNODB,字符集是utf8

1 个答案:

答案 0 :(得分:2)

对于InnoDB,table_rows统计信息是估计,而不是确切的计数。该值不能保证准确的行计数。

不,这不是MySQL中的错误。 InnoDB统计信息没有错。行为记录在《 MySQL参考手册》中。

参考文献:

https://dev.mysql.com/doc/refman/5.6/en/index-statistics.html

https://dev.mysql.com/doc/refman/5.6/en/innodb-persistent-stats.html