MYSQL慢查询日志存储在表中时查询出TOPSQL的脚本

按执行时间使用倒序的方式排序找出 TOP 10 的慢查询SQL
t_slow.sql

use MySQL;
select * from
(select * from slow_log  where start_time >= '2016-08-01 00:00:01'  order by query_time desc) a
limit 10;

mysql -u root -p -e "source t_slow.sql" > t_slow.txt

按锁定时间使用倒序的方式排序找出 TOP 10 的慢查询SQL
l_slow.sql

use mysql;
select * from
(select * from slow_log  where start_time >= '2016-08-01 00:00:01'  order by lock_time desc) a
limit 10;


mysql -u root -p -e "source l_slow.sql" > l_slow.txt


按输出的行数使用倒序的方式排序找出 TOP 10 的慢查询SQL
r_slow.sql

use mysql;
select * from
(select * from slow_log  where start_time >= '2016-08-01 00:00:01'  order by rows_sent desc) a
limit 10;

mysql -u root -p -e "source r_slow.sql" > r_slow.txt


按执行次数使用倒序的方式排序找出 TOP 10 的慢查询SQL
c_slow.sql

use mysql;
select * from
(select sum(query_time),avg(query_time),sum(lock_time),avg(lock_time),
sum(rows_sent),avg(rows_sent),sum(rows_examined),avg(rows_examined),
count(1),sql_text from slow_log  where start_time >= '2016-08-01 00:00:01'
group by sql_text  order by count(1) desc) a
limit 10;

mysql -u root -p -e "source c_slow.sql" > c_slow.txt
名称栏目:MYSQL慢查询日志存储在表中时查询出TOPSQL的脚本
分享URL:http://csdahua.cn/article/ieicgs.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流