Mysql 慢查询日志不遵守 long_query_time 变量,记录真正快速的查询

Mysql slow query log not honoring long_query_time variable, logs really quick queries

我是 运行ning mysql 5.5,正在调试性能问题。我启用了 mysql 慢速日志记录。这些是我在 my.cnf:

中的相关设置
log_slow_queries    = /var/log/mysql/mysql-slow.log
long_query_time = 10
log-queries-not-using-indexes

Mysql 确实在创建和写入慢速查询日志。但是,它会记录大量 运行 比 long_query_time 参数指定的查询快得多的查询。

这里有几个例子:

# Query_time: 0.000142  Lock_time: 0.000039 Rows_sent: 1  Rows_examined: 1 
# Query_time: 0.000081  Lock_time: 0.000024 Rows_sent: 1  Rows_examined: 1
# Query_time: 0.000116  Lock_time: 0.000034 Rows_sent: 1  Rows_examined: 1

实际上没有查询或锁定时间加起来大于 0.1 的查询实例。根据我对 long_query_time 参数应该做什么的阅读,我不希望在慢速查询日志中找到这些查询。

编辑 my.cnf 不会将更改应用到 运行 实例。您必须重新启动 MySQL 服务才能使 my.cnf 生效。

或者,您可以使用 SET GLOBAL long_query_time = 10 在当前 运行 实例中应用等效更改。

我的习惯是both: edit my.cnf 这样下次重启MySQL 服务就生效了,然后我也使用 SET GLOBAL 将相同的更改应用于 运行 实例。了解并非每个 MySQL 服务器变量都可以像那样动态更改。

另请注意,只有 new 客户端连接将获得该全局值。现有客户端连接将继续使用其连接开始时有效的先前全局值。


回复您的评论:

请注意,您设置了不使用索引的日志查询。这意味着记录 所有 不使用索引的查询,即使查询比 long_query_time.

https://dev.mysql.com/doc/refman/5.5/en/slow-query-log.html 说:

The server uses the controlling parameters in the following order to determine whether to write a query to the slow query log:

  • The query must either not be an administrative statement, or --log-slow-admin-statements must have been specified.

  • The query must have taken at least long_query_time seconds, or log_queries_not_using_indexes must be enabled and the query used no indexes for row lookups.

  • The query must have examined at least min_examined_row_limit rows.

请注意 "not using indexes" 有点违反直觉。 https://dev.mysql.com/doc/refman/5.5/en/server-options.html#option_mysqld_log-queries-not-using-indexes 说:

[The log-queries-not-using-indexes] option does not necessarily mean that no index is used. For example, a query that uses a full index scan uses an index but would be logged because the index would not limit the number of rows.