mysql-慢查询-mysqldumpslow简介

来源:互联网 发布:淘宝店铺0信誉的多少钱 编辑:程序博客网 时间:2024/05/30 05:41

mysqldumpslow是mysql server自带的用来分析慢日志的工具,主要作用是分析慢查询日志文件,将日志中的内容进行摘要后输出显示。

~# mysqldumpslow --helpUsage: mysqldumpslow [ OPTS... ] [ LOGS... ]Parse and summarize the MySQL slow query log. Options are  --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

比较常用的参数有这么几个:

-s 指定输出的排序方式   t  : 根据query time(执行时间)进行排序;   at : 根据average query time(平均执行时间)进行排序;(默认使用的方式)   l  : 根据lock time(锁定时间)进行排序;   al : 根据average lock time(平均锁定时间)进行排序;   r  : 根据rows(扫描的行数)进行排序;   ar : 根据average rows(扫描的平均行数)进行排序;   c  : 根据日志中出现的总次数进行排序;-t 指定输出的sql语句条数;-a 不进行抽象显示(默认会将数字抽象为N,字符串抽象为S);-g 满足指定条件,与grep相似;-h 用来指定主机名(指定打开文件,通常慢查询日志名称为“主机名-slow.log”,用-h exp则表示打开exp-slow.log文件);

实际操作:

root@yunzongjitest1:~# mysqldumpslow -s t -t 3Reading mysql slow query log from /var/lib/mysql/exp-slow.log /var/lib/mysql/yunzongjitest1-slow.logCount: 464  Time=18.35s (8515s)  Lock=0.01s (3s)  Rows=90884.0 (42170176), root[root]@localhost  select ************Count: 38  Time=11.22s (426s)  Lock=0.00s (0s)  Rows=1.0 (38), root[root]@localhost  select *********** not like 'S'Count: 48  Time=5.07s (243s)  Lock=0.02s (1s)  Rows=1.0 (48), root[root]@localhost  select ********='S'

这其中的sql语句因为涉及某些信息,所以我都用*号将主体替换了,如果希望得到具体的值,使用-a参数。

使用mysqldumpslow查询出来的摘要信息,包含了这些内容:
Count: 464 : 表示慢查询日志总共记录到这条sql语句执行的次数;
Time=18.35s (8515s) : 18.35s表示平均执行时间(-s at),8515s表示总的执行时间(-s t);
Lock=0.01s (3s) : 与上面的Time相同,第一个表示平均锁定时间(-s al),括号内的表示总的锁定时间(-s l)(也有另一种说法,说是表示的等待锁释放的时间);
Rows=90884.0 (42170176) : 第一个值表示扫描的平均行数(-s ar),括号内的值表示扫描的总行数(-s r)。