目的:
统计慢日志,优化SQL;
工具:
MySQLdumpslow,MySQLdumpslow是mysql自带的用来分析慢查询的工具;
Option Name | Description | |
---|---|---|
-a | Do not abstract all numbers to N and strings to 'S' | 不要将所有数字都抽象为N,将字符串抽象为“ S” |
-n | Abstract numbers with at least the specified digits | 后面跟数字,在名称中至少有n个数字抽象为数字 |
--debug | Write debugging information | |
-g | Only consider statements that match the pattern | 正则后边可以写一个正则匹配模式,大小写不敏感的 |
--help | Display help message and exit | |
-h | Host name of the server in the log file name | 日志文件名中服务器的主机名 |
-i | Name of the server instance | 服务器实例的名称 |
-l | Do not subtract lock time from total time | 锁定时间 |
-r | Reverse the sort order | 返回记录 |
-s | How to sort output | 排序 |
-t | Display only first num queries | 返回多少条数据 |
--verbose | Verbose mode | 详细模式 |
默认情况下,mysqldumpslow按平均查询时间(等于-s at)排序。
-s 后面可以跟参数来选择排序方式
c:访问计数
l:锁定时间
r:返回记录
al:平均锁定时间
ar:平均访问记录数
at:平均查询时间
案例:
平均访问次数最多的20条sql
mysqldumpslow -s ar -t 20 sqldlow.log
平均查询时间最长倒序的前50条,并输出到日志中
mysqldumpslow -s at -t 50 /var/lib/mysql/slow.log > /tmp/slowsql.log
查询结果如下: Count:查询次数; Time:执行时间; Lock:锁定时间; Rows:扫描行数; 这里未使用-a所以参数已经抽象,被N代替;
Count: 2 Time=3.00s (6s) Lock=0.00s (0s) Rows=4103.0 (8206), root[root]@2hosts
SELECT * FROM `user` LIMIT N OFFSET N