数据库
首页 > 数据库> > mysql – 使用主键偶尔缓慢更新

mysql – 使用主键偶尔缓慢更新

作者:互联网

我在MySQL 5.5.53中有一个InnoDB表,其中有简单的更新

UPDATE mytable SET acol = 'value' WHERE id = 42;

挂几秒钟. id是表的主键.

如果我使用启用查询分析

SET profiling = 1;

然后运行查询并查看配置文件,我看到类似的东西:

show profile;
+------------------------------+----------+
| Status                       | Duration |
+------------------------------+----------+
| starting                     | 0.000077 |
| checking permissions         | 0.000008 |
| Opening tables               | 0.000024 |
| System lock                  | 0.000008 |
| init                         | 0.000346 |
| Updating                     | 0.000108 |
| end                          | 0.000004 |
| Waiting for query cache lock | 0.000002 |
| end                          | 3.616845 |
| query end                    | 0.000016 |
| closing tables               | 0.000015 |
| freeing items                | 0.000023 |
| logging slow query           | 0.000003 |
| logging slow query           | 0.000048 |
| cleaning up                  | 0.000004 |
+------------------------------+----------+

也就是说,所有的时间都花在了最后.

The documentation说:

  • end

    This occurs at the end but before the cleanup of ALTER TABLE, CREATE VIEW, DELETE, INSERT, SELECT, or UPDATE statements.

这样一个简单的陈述如何在这种状态下度过这么长时间?

解决方法:

事实证明问题是查询缓存.

如果我禁用它

SET GLOBAL query_cache_size = 0;
SET GLOBAL query_cache_type = 0;

问题消失了.

它必须使查询缓存条目无效,导致查询挂起这么长时间.

标签:mysql,query-cache
来源: https://codeday.me/bug/20190828/1752030.html