活久见,为什么SHOW TABLE STATUS总是不更新1

简介: 活久见,为什么SHOW TABLE STATUS总是不更新

1. 问题描述

2. 问题探究

3. 总结

4. 延伸阅读



1. 问题描述

前几天,QQ群里在讨论一个关于MySQL表统计信息迟迟不更新的问题。

这个问题我复现了,下面是详细过程:


# 创建一个空表
[root@yejr.run]>create table ttxx like t1;

# 第一次执行 show table status,看到 Rows = 0,没问题
[root@yejr.run] [test]>show table status like 'ttxx'\G
*************************** 1. row ***************************
           Name: ttxx
         Engine: InnoDB
        Version: 10
     Row_format: Dynamic
           Rows: 0
 Avg_row_length: 0
    Data_length: 16384
Max_data_length: 0
   Index_length: 16384
      Data_free: 0
 Auto_increment: 1
    Create_time: 2020-06-04 16:17:54
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options:
        Comment:
1 row in set (0.00 sec)

# 写入将近80万条数据
[root@yejr.run]>insert into ttxx select id,name,c1 from t1;
Query OK, 799994 rows affected (8.25 sec)
Records: 799994  Duplicates: 0  Warnings: 0

# 再次执行 show table status,发现 Rows 值还是 0,并且过了几秒钟后多执行几次,结果依然如此
[root@yejr.run] [test]>show table status like 'ttxx'\G
*************************** 1. row ***************************
           Name: ttxx
         Engine: InnoDB
        Version: 10
     Row_format: Dynamic
           Rows: 0
 Avg_row_length: 0
    Data_length: 16384
Max_data_length: 0
   Index_length: 16384
      Data_free: 0
 Auto_increment: 1
    Create_time: 2020-06-04 16:17:54
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options:
        Comment:



简言之,就是执行 SHOW TABLE STATUS无法及时查看到该表的最新统计信息

但与此同时,直接查看 mysql.innodb_table_statsmysql.innodb_index_stats 两个表,却又可以看到该表的统计信息已经更新了:

[root@yejr.run] [test]>select * from mysql.innodb_table_stats where database_name ='test' and table_name ='ttxx';

+---------------+------------+---------------------+--------+----------------------+--------------------------+
| database_name | table_name | last_update | n_rows | clustered_index_size | sum_of_other_index_sizes |
+---------------+------------+---------------------+--------+----------------------+--------------------------+
| test | ttxx | 2020-06-04 16:18:24 | 795064 | 2788 | 0 |
+---------------+------------+---------------------+--------+----------------------+--------------------------+

[root@yejr.run] [test]>select * from mysql.innodb_index_stats where database_name ='test' and table_name ='ttxx';
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| database_name | table_name | index_name | last_update | stat_name | stat_value | sample_size | stat_description |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+
| test | ttxx | PRIMARY | 2020-06-04 16:18:24 | n_diff_pfx01 | 795064 | 20 | aid |
| test | ttxx | PRIMARY | 2020-06-04 16:18:24 | n_leaf_pages | 2764 | NULL | Number of leaf pages in the index |
| test | ttxx | PRIMARY | 2020-06-04 16:18:24 | size | 2788 | NULL | Number of pages in the index |
+---------------+------------+------------+---------------------+--------------+------------+-------------+-----------------------------------+



尝试跑一个SQL观察执行计划,看起来也是正确的:

[root@yejr.run] [test]>desc select count(*) from ttxx;
+----+-------------+-------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+
| 1 | SIMPLE | ttxx | NULL | index | NULL | PRIMARY | 4 | NULL | 795064 | 100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+

此时再执行 SHOW TABLE STATUS 依然无法看到信息更新。

到底肿么回事呢?



2. 问题探究

作为老司机(踩坑大户),首先想到的就是检查官方手册。

MySQL官方手册的描述中,有这么一段内容:

• Rows

The number of rows. Some storage engines, such as MyISAM, store the exact count. For other storage engines, such as InnoDB, this value is an approximation, and may vary from the actual value by as much as 40% to 50%. In such cases, use SELECT COUNT(*) to obtain an accurate count.

The Rows value is NULL for INFORMATION_SCHEMA tables.

For InnoDB tables, the row count is only a rough estimate used in SQL optimization. (This is also true if the InnoDB table is partitioned.)



简言之,就是说MyISAM表的Rows是精确值,但InnoDB表则只是大概值,甚至有可能只是真实值的40% ~ 50% 之间。

另外,这个信息是从 INFORMATION_SCHEMA.TABLES (下面简称IFS.TABLES)里获取的:


Table information is also available from the INFORMATION_SCHEMA TABLES table. See Section 25.36, “The INFORMATION_SCHEMA TABLES Table”.


那我们再看看文档中关于 IFS.TABLES 的描述吧:


25.36 The INFORMATION_SCHEMA TABLES Table

The TABLES table provides information about tables in databases.

Columns in TABLES that represent table statistics hold cached values. The information_schema_stats_expiry system variable defines the period of time before cached table statistics expire. The default is 86400 seconds (24 hours). If there are no cached statistics or statistics have expired, statistics are retrieved from storage engines when querying table statistics columns. To update cached values at any time for a given table, use ANALYZE TABLE. To always retrieve the latest statistics directly from storage engines, set information_schema_stats_expiry to 0. For more information, see Section 8.2.3, “Optimizing INFORMATION_SCHEMA Queries”.


看到这里,真相基本上呼之欲出了。

IFS.TABLES表中看到的数据是有cache的,默认cache时长是 86400秒(即1天),修改参数 information_schema_stats_expiry 即可调整时长。也就是说,除非cache过期了,或者手动执行 ANALYZE TABLE 更新统计信息,否则不会主动更新。

这个参数(功能)是MySQL 8.0后新增的,所以这个问题在8.0之前的版本不存在。

参数 information_schema_stats_expiry 还影响其 IFS.STATISTICS 表。


            </div>
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
JSON TensorFlow 算法框架/工具
Windows下安装Anaconda5.3.1+Python3.8+TensorFlow2.13.0-CPU版本总结
Windows下安装Anaconda5.3.1+Python3.8+TensorFlow2.13.0-CPU版本总结
1202 0
|
消息中间件 存储
RabbitMQ的高可用机制
RabbitMQ 提供了多种高可用机制来确保消息队列的可靠性和稳定性。
1226 0
|
数据采集 机器学习/深度学习 算法
阿里音乐流行趋势预测—冠军答辩(二)|学习笔记
快速学习阿里音乐流行趋势预测—冠军答辩(二)
564 0
|
1月前
|
人工智能 自然语言处理 安全
直播带货效率提升300%:这个AI指令让技术人也能写出专业话术
技术人写直播话术太难?逻辑思维 vs 情感营销冲突大,学得慢、改得累。本文分享一套结构化AI指令,将专业带货方法论转化为可执行模板,支持多品类定制,30分钟生成高转化话术,助力开发者高效应对朋友求助或产品推广,轻松玩转直播文案。
455 5
|
9月前
|
前端开发 JavaScript 程序员
鸿蒙开发:console日志输出
针对初学者而言,大家只需要掌握住日志打印即可,等到了鸿蒙应用开发的时候,还有一个鸿蒙原生的打印工具HiLog,到时,我们也会详细的去讲述,也会针对HiLog,封装一个通用的工具类。
340 11
鸿蒙开发:console日志输出
|
10月前
|
机器学习/深度学习 存储 人工智能
《DeepSeek情感分析技术:突破与创新,精准判断情感倾向》
在数字化时代,文本数据激增,情感分析成为关键需求。DeepSeek作为AI领域的佼佼者,基于Transformer架构实现深度语义理解,通过多模态融合技术全面感知情感,结合领域自适应与迁移学习跨越不同场景,采用对抗训练提升鲁棒性,并融合情感词典与知识图谱增强理解。这些创新使DeepSeek能精准判断情感倾向,为企业和研究提供有力支持。
1234 20
|
11月前
|
Windows
Office Tool Plus 永恒经典,让每个人都能轻松使用上免费的办公神器!
本文介绍如何使用Office Tool Plus在Windows 11系统上快速、免费安装和激活Office。首先,下载并解压Office Tool Plus,启动后选择“Microsoft 365企业应用版”并设置为简体中文,点击“开始部署”。安装完成后,可通过两种方法激活Office:一是使用命令框输入特定指令,二是通过KMS激活。推荐使用KMS服务器(如kms.loli.beer)进行激活。此外,若之前安装过Office,需先清除激活信息和旧版本残留文件,以确保新安装顺利进行。
8680 9
|
人工智能 自然语言处理 前端开发
Lobe Vidol:AI数字人交互平台,可与虚拟人和3D模型聊天互动
Lobe Vidol是一款开源的AI数字人交互平台,允许用户创建和互动自己的虚拟偶像。该平台提供流畅的对话体验、丰富的动作姿势库、优雅的用户界面设计以及多种技术支持,如文本到语音和语音到文本技术。Lobe Vidol适用于娱乐互动、在线教育、客户服务、品牌营销和社交媒体等多个应用场景。
773 7
Lobe Vidol:AI数字人交互平台,可与虚拟人和3D模型聊天互动
|
安全 持续交付 Docker
微服务架构和 Docker 容器化部署的优点是什么?
微服务架构和 Docker 容器化部署的优点是什么?
|
数据采集 机器学习/深度学习 数据可视化
纵横小说网站数据采集与分析实现
本文介绍了一个基于Python的纵横中文网数据采集与分析项目,旨在通过技术手段深入分析网络小说市场,掌握读者需求,评估作品质量,并为网站运营提供策略支持。
691 0
纵横小说网站数据采集与分析实现