MySQL 与主流分支版本上执行 ANALYZE TABLE 安全么?

有时,需要使用 ANALYZE TABLE 命令手动更新表和索引统计信息。在不进一步探讨这种需求的原因的情况下,我想就与在生产系统上运行命令相关的开销来聊聊这个话题。然而,这里讨论的开销与深入表行收集统计信息的通常成本无关,我们可以通过设置样本页数 来控制。

五年前,我的同事 Sveta 发布了一篇不错的博客文章,介绍了 Percona Server for MySQL 中引入的一项改进,以解一些不必要的等待:

《ANALYZE TABLE 不再是阻塞操作》

从历史上看,在 MySQL 中运行 ANALYZE TABLE 命令的问题是查询需要在表的表定义缓存条目上使用排他锁。这使得查询等待任何长时间运行的查询完成,但也可能触发级联等待其他传入请求。简而言之,ANALYZE 可能会导致高负载生产环境中出现较高得延时。

从那时起,MySQL/Percona/MariaDB 都发生了一些变化,但今天仍然存在许多生产系统会受影响的版本。让我们回顾一下这些年来情况的演变。

MySQL

该问题适用于 MySQL 8.0.23 之前的所有版本。5.7 系列没有任何改进(顺便说一句,本月将达到 EOL!),这意味着即使是最新的 5.7.43 也会受到影响。以下是您可能会遇到的示例场景:

sql 复制代码
mysql > select @@version,@@version_comment;
+-----------+------------------------------+
| @@version | @@version_comment            |
+-----------+------------------------------+
| 5.7.43    | MySQL Community Server (GPL) |
+-----------+------------------------------+
1 row in set (0.00 sec)

mysql > show processlist;
+----+----------+-----------+------+---------+------+-------------------------+----------------------------------------------------------------+
| Id | User     | Host      | db   | Command | Time | State                   | Info                                                           |
+----+----------+-----------+------+---------+------+-------------------------+----------------------------------------------------------------+
|  4 | msandbox | localhost | db1  | Query   |   54 | Sending data            | select avg(k) from sbtest1 where pad not like '%f%' group by c |
| 13 | msandbox | localhost | db1  | Query   |   29 | Waiting for table flush | analyze table sbtest1                                          |
| 17 | msandbox | localhost | db1  | Query   |    0 | starting                | show processlist                                               |
| 18 | msandbox | localhost | db1  | Query   |   15 | Waiting for table flush | select * from sbtest1 where id=100                             |
+----+----------+-----------+------+---------+------+-------------------------+----------------------------------------------------------------+
4 rows in set (0.00 sec)

一个慢查询导致 ANALYZE 在等待,且另一个通常非常快的查询现在也在等待。

同样的情况也可能发生在 MySQL 8.0 系列,包括 8.0.23。幸运的是,8.0.24 版本中修复了这个问题 。我们只能在发行说明 中读到一些关于此问题解决得评论:

事实上,从版本 8.0.24 开始,运行慢查询期间的类似测试会导致即时查询执行:

sql 复制代码
mysql > select @@version,@@version_comment;
+-----------+------------------------------+
| @@version | @@version_comment            |
+-----------+------------------------------+
| 8.0.24    | MySQL Community Server - GPL |
+-----------+------------------------------+
1 row in set (0.00 sec)

mysql > analyze table sbtest1;
+-------------+---------+----------+----------+
| Table       | Op      | Msg_type | Msg_text |
+-------------+---------+----------+----------+
| db1.sbtest1 | analyze | status   | OK       |
+-------------+---------+----------+----------+
1 row in set (0.00 sec)

不过,即使是 8.1 版本,我们仍然可以在官方文档中找到警告,如下所示:

ANALYZE TABLE removes the table from the table definition cache, which requires a flush lock. If there are long running statements or transactions still using the table, subsequent statements and transactions must wait for those operations to finish before the flush lock is released. Because ANALYZE TABLE itself typically finishes quickly, it may not be apparent that delayed transactions or statements involving the same table are due to the remaining flush lock.

Percona

如上所述,为了解决此错误报告,Percona 引入了修复程序并删除了不必要的表定义缓存锁。

使用 Percona 时,从版本 5.6.38 和 5.7.20 开始运行 ANALYZE TABLE 已经是安全的,因为这些是当时活跃的版本。

第一个版本(包括第一个 GA 版本 8.0.13-3 )以来,Percona Server for MySQL 版本 8.0 就没有这个问题,因为改进是从 Percona Server for MySQL 5.7 系列合并而来的。

MariaDB

ANALYZE TABLE 的问题发生于 10.5.3 之前的所有 MariaDB 版本。在版本 10.5.4 中,Percona 得以解决。

因此,当您在 10.5.3 或更低版本以及任何以前的系列(甚至是最新的 10.4.31)中运行查询时,可能会出现类似的情况:

sql 复制代码
mysql > select @@version,@@version_comment;
+----------------+-------------------+
| @@version      | @@version_comment |
+----------------+-------------------+
| 10.5.3-MariaDB | MariaDB Server    |
+----------------+-------------------+
1 row in set (0.000 sec)

mysql > show processlist;
+----+----------+-----------+------+---------+------+-------------------------+----------------------------------------------------------------+----------+
| Id | User     | Host      | db   | Command | Time | State                   | Info                                                           | Progress |
+----+----------+-----------+------+---------+------+-------------------------+----------------------------------------------------------------+----------+
|  4 | msandbox | localhost | db1  | Query   |   18 | Sending data            | select avg(k) from sbtest1 where pad not like '%f%' group by c |    0.000 |
| 13 | msandbox | localhost | db1  | Query   |   16 | Waiting for table flush | analyze table sbtest1                                          |    0.000 |
| 14 | msandbox | localhost | db1  | Query   |   14 | Waiting for table flush | select * from sbtest1 where id=100                             |    0.000 |
| 15 | msandbox | localhost | NULL | Query   |    0 | starting                | show processlist                                               |    0.000 |
+----+----------+-----------+------+---------+------+-------------------------+----------------------------------------------------------------+----------+
4 rows in set (0.000 sec)

mysql > select @@version,@@version_comment;
+-----------------+-------------------+
| @@version       | @@version_comment |
+-----------------+-------------------+
| 10.4.31-MariaDB | MariaDB Server    |
+-----------------+-------------------+
1 row in set (0.000 sec)

mysql > show processlist;
+----+-------------+-----------+------+---------+------+--------------------------+----------------------------------------------------------------+----------+
| Id | User        | Host      | db   | Command | Time | State                    | Info                                                           | Progress |
+----+-------------+-----------+------+---------+------+--------------------------+----------------------------------------------------------------+----------+
|  1 | system user |           | NULL | Daemon  | NULL | InnoDB purge coordinator | NULL                                                           |    0.000 |
|  2 | system user |           | NULL | Daemon  | NULL | InnoDB purge worker      | NULL                                                           |    0.000 |
|  3 | system user |           | NULL | Daemon  | NULL | InnoDB purge worker      | NULL                                                           |    0.000 |
|  4 | system user |           | NULL | Daemon  | NULL | InnoDB purge worker      | NULL                                                           |    0.000 |
|  5 | system user |           | NULL | Daemon  | NULL | InnoDB shutdown handler  | NULL                                                           |    0.000 |
|  9 | msandbox    | localhost | db1  | Query   |   18 | Sending data             | select avg(k) from sbtest1 where pad not like '%f%' group by c |    0.000 |
| 18 | msandbox    | localhost | db1  | Query   |   16 | Waiting for table flush  | analyze table sbtest1                                          |    0.000 |
| 19 | msandbox    | localhost | db1  | Query   |   12 | Waiting for table flush  | select * from sbtest1 where id=100                             |    0.000 |
| 22 | msandbox    | localhost | NULL | Query   |    0 | Init                     | show processlist                                               |    0.000 |
+----+-------------+-----------+------+---------+------+--------------------------+----------------------------------------------------------------+----------+
9 rows in set (0.000 sec)

总结

只要您的数据库在最新版本的 MySQL 或 MariaDB 上运行,运行 ANALYZE TABLE 就应该绝对安全,不会导致任何意外的停顿。

Percona Server for MySQL 系列:5.6.38+、5.7.20+ 和 8.0.x 的用户都是安全的。

MariaDB 用户必须升级到 10.5.4 或更高版本以避免锁定问题。

更多技术文章,请访问:opensource.actionsky.com/

关于 SQLE

SQLE 是一款全方位的 SQL 质量管理平台,覆盖开发至生产环境的 SQL 审核和管理。支持主流的开源、商业、国产数据库,为开发和运维提供流程自动化能力,提升上线效率,提高数据质量。

相关推荐
寒秋丶15 分钟前
Milvus:Json字段详解(十)
数据库·人工智能·python·ai·milvus·向量数据库·rag
ManageEngineITSM4 小时前
技术的秩序:IT资产与配置管理的现代重构
大数据·运维·数据库·重构·工单系统
档案宝档案管理6 小时前
档案宝:企业合同档案管理的“安全保险箱”与“效率加速器”
大数据·数据库·人工智能·安全·档案·档案管理
wangjialelele6 小时前
mysql库操作二
数据库·mysql
Chloeis Syntax7 小时前
MySQL初阶学习日记(1)--- 数据库的基本操作
数据库·学习·mysql
workflower7 小时前
FDD(Feature Driven Development)特征驱动开发
大数据·数据库·驱动开发·需求分析·个人开发
韩立学长8 小时前
基于Springboot的旧物公益捐赠管理系统3726v22v(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
苦学编程的谢9 小时前
Redis_4_常见命令(完)+认识数据类型和编码方式
数据库·redis·缓存
小光学长9 小时前
基于Vue的儿童手工创意店管理系统as8celp7(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
前端·数据库·vue.js