mysql慢查询日志

mysql慢查询日志可以记录查询时间过长的sql,对于性能问题定位十分重要,本文旨在介绍慢查询日志管理使用。

参数

1、slow_query_log:ON表示慢查询日志开启,OFF表示慢查询日志关闭;

2、slow_query_log_file:慢查询日志文件;

3、long_query_time:表示执行时间超过多少秒的sql记录到慢查询日志;

4、log_queries_not_using_indexes:ON表示慢查询日志会记录执行过的没有索引的sql,OFF表示不会记录;

5、log_throttle_queries_not_using_indexes:限制每分钟记录的无索引sql数量;

6、log_output:慢查询日志记录方式,FILE表示记录文件,TABLE表示记录表,FILE/TABLE表示同时记录文件,表。

实战

set global log_output='FILE,TABLE';
set global long_query_time=2;
create database coshaho003;
use coshaho003;
show variables like 'long_query_time';
select sleep(3);
select * from mysql.slow_log;

这里需要注意的是,执行了set global long_query_time=2;后,需要新建数据库才能生效。

慢查询日志信息如下:

Time                 Id Command    Argument
# Time: 2017-08-13T04:48:11.591942Z
# User@Host: root[root] @ localhost [127.0.0.1]  Id:     4
# Query_time: 3.015729  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
use coshaho001;
SET timestamp=1502599691;
select sleep(3)
LIMIT 0, 1000;
# Time: 2017-08-13T04:51:41.790726Z
# User@Host: root[root] @ localhost [127.0.0.1]  Id:     4
# Query_time: 3.000108  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
use coshaho003;
SET timestamp=1502599901;
select sleep(3)
LIMIT 0, 1000;
原文地址:https://www.cnblogs.com/coshaho/p/7353086.html