2020年6月22日 作者 zeroheart

慢日志查询统计

mysqldumpslow 慢日志分析工具
命令:

-s 按照那种方式排序
c:访问计数
l:锁定时间
r:返回记录
al:平均锁定时间
ar:平均访问记录数
at:平均查询时间
-t 是top n的意思,返回多少条数据。
-g 可以跟上正则匹配模式,大小写不敏感。

完整的参数
--verbose    verbose
--debug      debug
--help       write this text to standard output


-v           verbose
-d           debug
-s ORDER     what to sort by (al, at, ar, c, l, r, t), 'at' is default
                al: average lock time
                ar: average rows sent
                at: average query time
                 c: count
                 l: lock time
                 r: rows sent
                 t: query time  
-r           reverse the sort order (largest last instead of first)
-t NUM       just show the top n queries
-a           don't abstract all numbers to N and strings to 'S'
-n NUM       abstract numbers with at least n digits within names
-g PATTERN   grep: only consider stmts that include this string
-h HOSTNAME  hostname of db server for *-slow.log filename (can be wildcard),
               default is '*', i.e. match all
-i NAME      name of server instance (if using mysql.server startup script)
-l           don't subtract lock time from total time
得到返回记录最多的20个sql
mysqldumpslow -s r -t 20 /usr/local/mysql/data/slowSql.log
平均访问时间最长的20个sql
mysqldumpslow -s at -t 20 /usr/local/mysql/data/slowSql.log
得到平均访问次数最多,并且里面含有select字符的20条sql
mysqldumpslow -s ar -t 20 -g "select" /usr/local/mysql/data/slowSql.log
如果出现 -bash: mysqldumpslow: command not found 错误,请执行
ln -s /usr/local/mysql/bin/mysqldumpslow /usr/bin
如果出现如下错误,Died at /usr/bin/mysqldumpslow line 161, <> chunk 405659.说明你要分析的sql日志太大了,请拆分后再分析

拆分的命令为:tail -100000 mysql-slow.log > mysql-slow.20180725.log
关闭mysql查询缓存
set global query_cache_size=0
set global query_cache_type=0

参考:https://blog.csdn.net/sunyuhua_keyboard/article/details/81204020