简体   繁体   English

MySQL 8 MyISAM“ SHOW TABLE STATUS”行数

[英]mysql 8 MyISAM “SHOW TABLE STATUS” row count

i recently upgraded from mySQL 5.6.34 -> 8.0.16 (on macOS 10.14.5) and i am noticing very strange behavior with the row counts returned from "SHOW TABLE STATUS" as well as the row counts in the "information_schema" table. 我最近从mySQL 5.6.34-> 8.0.16(在macOS 10.14.5上)升级,并且注意到从“ SHOW TABLE STATUS”返回的行数以及“ information_schema”表中的行数非常奇怪的行为。 consider this simple schema: 考虑以下简单模式:

CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `test` (`id`, `name`) VALUES
(1, 'one'),
(2, 'two'),
(3, 'three'),
(4, 'four'),
(5, 'five');

when i then run the following query i see the expected output: 当我然后运行以下查询时,我看到预期的输出:

SELECT * FROM test;
+----+-------+
| id | name  |
+----+-------+
|  1 | one   |
|  2 | two   |
|  3 | three |
|  4 | four  |
|  5 | five  |
+----+-------+

likewise when i then run the following query i see the expected output: 同样,当我然后运行以下查询时,我看到预期的输出:

SELECT COUNT(*) FROM test;
+----------+
| COUNT(*) |
+----------+
|        5 |
+----------+

however when i then run the following query: 但是当我然后运行以下查询时:

    SHOW TABLE STATUS \G
*************************** 1. row ***************************
           Name: test
         Engine: MyISAM
        Version: 10
     Row_format: Dynamic
           Rows: 0
 Avg_row_length: 0
    Data_length: 0
Max_data_length: 281474976710655
   Index_length: 1024
      Data_free: 0
 Auto_increment: 1
    Create_time: 2019-05-30 13:56:46
    Update_time: 2019-05-30 16:02:24
     Check_time: NULL
      Collation: utf8_unicode_ci
       Checksum: NULL
 Create_options: 
        Comment: 

it appears that there are no rows (even though there are 5). 似乎没有行(即使有5行)。 likewise i see the same results when i run: 同样,当我运行时,我会看到相同的结果:

SELECT table_name, table_rows FROM information_schema.tables WHERE table_schema = 'test';
    +------------+------------+
    | TABLE_NAME | TABLE_ROWS |
    +------------+------------+
    | test       |          0 |
    +------------+------------+

no rows? 没有行? if i add/delete rows to the table the counts do not change. 如果我在表中添加/删除行,则计数不会改变。 only after i run: 只有在我跑步后:

ANALYZE TABLE `test`

...do i see all of the row counts as correct. ...我是否认为所有行计数都是正确的? i am only seeing this on mySQL 8. everything worked as expected on mySQL 5. i am aware of problems with accurate row counts using InnoDB tables, but these are all MyISAM tables, which should always show the correct row counts. 我仅在mySQL 8上看到此情况。一切都在mySQL 5上按预期工作。我知道使用InnoDB表的行数准确的问题,但这些都是MyISAM表,应始终显示正确的行数。 any help is appreciated. 任何帮助表示赞赏。 thanks. 谢谢。

The information schema tables underwent significant, incompatible changes in MySQL 8 with the introduction of the global data dictionary : 通过引入全局数据字典 ,信息模式表在MySQL 8中进行了重大的, 不兼容的更改

Previously, INFORMATION_SCHEMA queries for table statistics in the STATISTICS and TABLES tables retrieved statistics directly from storage engines. 以前,INFORMATION_SCHEMA查询STATISTICS和TABLES表中的表统计信息是直接从存储引擎检索统计信息。 As of MySQL 8.0, cached table statistics are used by default. 从MySQL 8.0开始,默认情况下使用缓存的表统计信息。

The cache is controlled by the system variable information_schema_stats_expiry : 缓存由系统变量information_schema_stats_expiry

Some INFORMATION_SCHEMA tables contain columns that provide table statistics: 一些INFORMATION_SCHEMA表包含提供表统计信息的列:

[...] TABLES.TABLE_ROWS [...] [...] TABLES.TABLE_ROWS [...]

Those columns represent dynamic table metadata; 这些列表示动态表元数据。 that is, information that changes as table contents change. 即,信息随着表内容的改变而改变。

By default, MySQL retrieves cached values for those columns from the mysql.index_stats and mysql.table_stats dictionary tables when the columns are queried, which is more efficient than retrieving statistics directly from the storage engine. 默认情况下,查询列时,MySQL从mysql.index_stats和mysql.table_stats字典表中检索这些列的缓存值,这比直接从存储引擎检索统计信息更有效。 If cached statistics are not available or have expired, MySQL retrieves the latest statistics from the storage engine and caches them in the mysql.index_stats and mysql.table_stats dictionary tables. 如果缓存的统计信息不可用或已过期,则MySQL从存储引擎检索最新的统计信息,并将其缓存在mysql.index_stats和mysql.table_stats字典表中。 Subsequent queries retrieve the cached statistics until the cached statistics expire. 后续查询将检索缓存的统计信息,直到缓存的统计信息到期为止。

[...] [...]

To update cached values at any time for a given table, use ANALYZE TABLE. 要随时更新给定表的缓存值,请使用ANALYZE TABLE。

To always retrieve the latest statistics directly from the storage engine and bypass cached values, set information_schema_stats_expiry to 0. 要始终直接从存储引擎检索最新统计信息并绕过缓存的值,请将information_schema_stats_expiry设置为0。

This is consistent with your behaviour. 这与您的行为一致。

You can set information_schema_stats_expiry globally to 0, or per session whenever you need accurate statistics. 您可以将information_schema_stats_expiry全局设置为0,也可以在需要准确统计信息时按会话设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM