MySQL 5.6+版本设置long_query_time的值无效的原因及解决方法

MySQL 5.6+版本设置long_query_time的值无效的原因及解决方法

开启慢日志的方法:

编辑my.cnf文件:

# vim /etc/my.cnf
long_query_time = 1
slow_query_log = 1

然后再重启MySQL。

在实际生产环境中测试,配置确实生效,但是查看MySQL慢日志发现,慢日志中出现了很多小于1秒的查询。

原因:
在MySQL官方手册5.4.5 The Slow Query Log中的描述:

  1. The query must either not be an administrative statement, or log_slow_admin_statements must be enabled.
  2. 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.
  3. The query must have examined at least min_examined_row_limit rows.
  4. The query must not be suppressed according to the log_throttle_queries_not_using_indexes setting.

从第二条中可以看到,当log_queries_not_using_indexes为enable状态时,没有使用所有的查询将被记录到慢查询日志中。经测试得知,log_queries_using_indexes默认状态为enable(MySQL5.7.12)。

解决方法:

  1. 修改my.cnf配置文件,添加如下行或修改为:

    log_queries_not_using_indexes = 0
    
    • 1
    • 2

    然后重启MySQL。

  2. 登录MySQL,设置log_queries_not_using_indexes的全局状态为0:

    set global log_queries_not_using_indexes = 0;

猜你喜欢

转载自blog.csdn.net/u010735147/article/details/81359162