红联Linux门户
Linux帮助

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

发布时间:2016-11-26 09:24:58来源:linux网站作者:YALY有鸭梨
开启慢日志的方法:
编辑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
然后重启MySQL。
2.登录MySQL,设置log_queries_not_using_indexes的全局状态为0:
set global log_queries_not_using_indexes = 0;
 
本文永久更新地址:http://www.linuxdiyf.com/linux/26330.html