转 mysql 慢查询

来源:互联网 发布:中山大学附属医院 知乎 编辑:程序博客网 时间:2024/05/23 11:26

最近和开发、DBA一起对MYSQL作性能调优,发现几个点对抓取MYSQL慢语句很有帮助

在/etc/my.cnf 文件加入

log_slow_queries
log-queries-not-using-indexes
log_long_format
long_query_time=1


将在mysql数据目录下( datadir = /home/mysql ) 生成一个如test-slow.log的文件,内容有:

# Time: 080430 20:06:53
#: match_account[match_account] @ [10.0.4.114]
# Query_time: 0 Lock_time: 0 Rows_sent: 9 Rows_examined: 18
SELECT * FROM `tree_menus`   WHERE (parentid = 0) ORDER BY sequence desc, id desc;
# Time: 080430 20:09:04


查询用了0秒,返回9行,一共查了18行


如何利用呢,有2 种方法:

1) [root@test mysql]# grep Rows_examined   test-slow.log | sort -g -k9 -r|uniq -u |head -100
# Query_time: 0 Lock_time: 0 Rows_sent: 30 Rows_examined: 24460
# Query_time: 0 Lock_time: 0 Rows_sent: 10 Rows_examined: 24440
# Query_time: 0 Lock_time: 0 Rows_sent: 9 Rows_examined: 24439
# Query_time: 0 Lock_time: 0 Rows_sent: 2 Rows_examined: 24432
# Query_time: 0 Lock_time: 0 Rows_sent: 1 Rows_examined: 24430
# Query_time: 0 Lock_time: 0 Rows_sent: 0 Rows_examined: 12043
# Query_time: 0 Lock_time: 0 Rows_sent: 34 Rows_examined: 2441
# Query_time: 0 Lock_time: 0 Rows_sent: 12 Rows_examined: 1449
# Query_time: 0 Lock_time: 0 Rows_sent: 15 Rows_examined: 30
# Query_time: 0 Lock_time: 0 Rows_sent: 5 Rows_examined: 23
# Query_time: 0 Lock_time: 0 Rows_sent: 6 Rows_examined: 22
# Query_time: 0 Lock_time: 0 Rows_sent: 9 Rows_examined: 18
# Query_time: 0 Lock_time: 0 Rows_sent: 1 Rows_examined: 6
# Query_time: 0 Lock_time: 0 Rows_sent: 1 Rows_examined: 5
# Query_time: 0 Lock_time: 0 Rows_sent: 2 Rows_examined: 2
[root@test mysql]# grep -A 1 -B 2 "Rows_examined: 24460" test-slow.lo
grep: test-slow.lo: No such file or directory
[root@test mysql]# grep -A 1 -B 2 "Rows_examined: 24460" test-slow.log
# Time: 080430 20:02:29
#: match_account[match_account] @ [10.0.4.114]
# Query_time: 0 Lock_time: 0 Rows_sent: 30 Rows_examined: 24460
SELECT * FROM `profiles`    ORDER BY position, created_at DESC LIMIT 0, 30;


2)方法2:

[root@test mysql]# mysqldumpslow   -s t   -t 20

Reading mysql slow query log from /home/mysql/test-slow.log
Count: 1 Time=0.00s (0s) Lock=0.00s (0s) Rows=6.0 (6), match_account[match_account]@[10.0.4.114]
SELECT * FROM `reports`    ORDER BY sequence desc LIMIT N

Count: 1 Time=0.00s (0s) Lock=0.00s (0s) Rows=15.0 (15), match_account[match_account]@[10.0.4.114]
SELECT * FROM `experts`   WHERE (email IS NULL) ORDER BY sequence desc LIMIT N


一般我们关注的3类,参见

A) "Rows_examined" is more than 2000
B) "Rows_examined" is less than 2000 but that query is being run 20 times a second.
C) "Rows_examined" is three times bigger than "Rows_sent"



一直以来积累了很多对MySQL优化的心得和经验,计划通过写日志的方式一步一步写出来分享给大家。

MySQL优化的第一步应该做的就是排查问题,找出瓶颈,而通常情况下的瓶颈和问题都需要通过观察MySQL的运行情况来进行分析,《查看MySQL运行状况》这篇文章罗列了一些常用的查看 MySQL运行信息的方式。

而对于大多数的程序员来说,最容易发现并解决的问题就是MySQL的慢查询或者没有利用索引的查询,所以这里主要给大家介绍如何利用官方的 mysqldumpslow工具方便的查看这些信息。如何打开MySQL的慢查询,请点击《打开MySQL的慢查询记录》。

mysqldumpslow命令
/path/mysqldumpslow -s c -t 10 /database/mysql/slow-log
这会输出记录次数最多的10条SQL语句,其中:

  • -s, 是表示按照何种方式排序,c、t、l、r分别是按照记录次数、时间、查询时间、返回的记录数来排序,ac、at、al、ar,表示相应的倒叙;

  • -t, 是top n的意思,即为返回前面多少条的数据;

  • -g, 后边可以写一个正则匹配模式,大小写不敏感的;

比如
/path/mysqldumpslow -s r -t 10 /database/mysql/slow-log
得到返回记录集最多的10个查询。
/path/mysqldumpslow -s t -t 10 -g “left join” /database/mysql/slow-log
得到按照时间排序的前10条里面含有左连接的查询语句。

小结
使用mysqldumpslow命令可以非常明确的得到各种我们需要的查询语句,对MySQL查询语句的监控、分析、优化是MySQL优化的第一步,也是 非常重要的一步


0 0
原创粉丝点击