一、开启慢日志参数
#指定是否开启慢查询日志
slow_query_log = 1
#指定慢日志文件存放位置(默认在data)
slow_query_log_file=/application/mysql/data/slow.log
#设定慢查询的阀值(默认10s)
long_query_time=0.05
#不使用索引的慢查询日志是否记录到日志
log_queries_not_using_indexes
二、Mysqldumpslow
参数说明:
-s:
是表示按照何种方式排序,c、t、l、r分别是按照记录次数、时间、查询时间、返回的记录数来排序,ac、at、al、ar,表示相应的倒叙;
-t:
是top n的意思,即为返回前面多少条的数据;
-g:
后边可以写一个正则匹配模式,大小写不敏感的;
[root@db ~]# mysqldumpslow -s c -t 10 /application/mysql/data/slow.log
Reading mysql slow query log from /application/mysql/data/slow.log
Count: 8 Time=0.69s (5s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@localhost
insert into city_new select * from city_new
Count: 2 Time=0.24s (0s) Lock=0.00s (0s) Rows=275000.0 (550000), root[root]@localhost
select * from city_new limit N
Count: 1 Time=0.04s (0s) Lock=0.01s (0s) Rows=0.0 (0), root[root]@localhost
create table city_new select * from city
Count: 1 Time=0.66s (0s) Lock=0.00s (0s) Rows=1.0 (1), root[root]@localhost
select count(*) from city_new
Died at /application/mysql/bin/mysqldumpslow line 161, <> chunk 10.
三、pt-query-digest日志分析工具
下载
wget https://downloads.percona.com/downloads/percona-toolkit/3.3.1/binary/redhat/7/x86_64/percona-toolkit-3.3.1-1.el7.x86_64.rpm
yum安装
yum install -y percona-toolkit-3.3.1-1.el7.x86_64.rpm
分析日志
pt-query-digest /application/mysql/data/slow.log