mysqlsla 是一款由hackmysql.com推出的一款日志分析工具,功能非常强大. 图形输出数据报表,非常有利于分析慢查询的原因, 包括执行频率, 数据量, 查询消耗等!
1 安装
[root@rac3 software]# wget http://hackmysql.com/scripts/mysqlsla-2.03.tar.gz
[root@rac3 software]# tar zvxf mysqlsla-2.03.tar.gz
[root@rac3 software]# mv mysqlsla-2.03 mysqlsla
[root@rac3 software]# cd mysqlsla
[root@rac3 mysqlsla]# ls
bin Changes INSTALL lib Makefile.PL MANIFEST META.yml README
Makefile.PL MANIFEST META.yml
[root@rac3 mysqlsla]# perl Makefile.PL
Checking if your kit is complete...
Looks good
Writing Makefile for mysqlsla
[root@rac3 mysqlsla]#
[root@rac3 mysqlsla]# make && make install
cp lib/mysqlsla.pm blib/lib/mysqlsla.pm
cp bin/mysqlsla blib/script/mysqlsla
/usr/bin/perl "-MExtUtils::MY" -e "MY->fixin(shift)" blib/script/mysqlsla
Manifying blib/man3/mysqlsla.3pm
Installing /usr/lib/perl5/site_perl/5.8.8/mysqlsla.pm
Installing /usr/share/man/man3/mysqlsla.3pm
Installing /usr/bin/mysqlsla
Writing /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/mysqlsla/.packlist
Appending installation info to /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/perllocal.pod
2 使用
在mysql中执行查询语句,之后使用mysqlsla 工具查询
[root@rac3 bin]# mysqlsla /opt/mysql/data/slowquery.log
Auto-detected logs as slow logs
Report for slow logs: /opt/mysql/data/slowquery.log
3 queries total, 3 unique
Sorted by 't_sum'
Grand Totals: Time 1 s, Lock 0 s, Rows sent 2, Rows Examined 1.00M
______________________________________________________________________ 001 ___
Count : 1 (50.00%)
Time : 634.32 ms total, 634.32 ms avg, 634.32 ms to 634.32 ms max (98.09%)
Lock Time (s) : 107.011 ms total, 107.011 ms avg, 107.011 ms to 107.011 ms max (98.71%)
Rows sent : 1 avg, 1 to 1 max (50.00%)
Rows examined : 1.00M avg, 1.00M to 1.00M max (99.80%)
Database : test
Users :
root@localhost : 100.00% (1) of query, 100.00% (2) of all users
Query abstract:
SET timestamp=N; SELECT COUNT(N) FROM sbtest;
Query sample:
SET timestamp=1329572756;
select count(1) from sbtest;
______________________________________________________________________ 002 ___
Count : 1 (50.00%)
Time : 12.356 ms total, 12.356 ms avg, 12.356 ms to 12.356 ms max (1.91%)
Lock Time (s) : 1.403 ms total, 1.403 ms avg, 1.403 ms to 1.403 ms max (1.29%)
Rows sent : 1 avg, 1 to 1 max (50.00%)
Rows examined : 2.00k avg, 2.00k to 2.00k max (0.20%)
Database :
Users :
root@localhost : 100.00% (1) of query, 100.00% (2) of all users
Query abstract:
SET timestamp=N; SELECT COUNT(N) FROM sbtest ,t1 WHERE t1.id=sbtest.id;
Query sample:
SET timestamp=1329572815;
select count(1) from sbtest ,t1 where t1.id=sbtest.id;
______________________________________________________________________ 003 ___
Count : 1 (33.33%)
Time : 12.356 ms total, 12.356 ms avg, 12.356 ms to 12.356 ms max (0.00%)
Lock Time (s) : 1.403 ms total, 1.403 ms avg, 1.403 ms to 1.403 ms max (1.29%)
Rows sent : 1 avg, 1 to 1 max (33.33%)
Rows examined : 2.00k avg, 2.00k to 2.00k max (0.20%)
Database :
Users :
root@localhost : 100.00% (1) of query, 100.00% (3) of all users
Query abstract:
SET timestamp=N; SELECT COUNT(N) FROM sbtest ,t1 WHERE t1.id=sbtest.id;
Query sample:
SET timestamp=1329572815;
select count(1) from sbtest ,t1 where t1.id=sbtest.id;
输出解释:
queries total 总查询次数
unique 去除重复后的 sql 数量.
Sorted by 输出报表的内容排序.
Grand Totals slow sql 统计信息包括: 总执行时间, 等待锁时间, 结果行总数, 扫描行总数.
Count slow sql 的执行次数及占总的 slow log 数量的百分比.
Time 执行时间, 包括总时间, 平均时间, 最小, 最大时间, 时间占到总 slow sql 时间的百分比.
Lock Time 等待锁的时间.
Rows sent 结果行统计数量, 包括平均, 最小, 最大数量.
Rows examined 扫描的行数量.
Database 属于哪个数据库
Users username@hostname 执行sql的用户
Query abstract 精简后的sql语句
几个常用OPTION, 其他可查看官网文档
--log-type=type logs or -lt type logs
其中type logs 可为 slow, general, binary, msl or udl. 官方文档说是没有指定时, 会根据给定的日志文件自动检测, 但测试时失败, so, 最好指定该参数.
msl 是指 microslow patched 的慢日志.
udl 用户自定义的日志.
binary 因为 mysqlsla 不能直接解析 MySQL 的 binary log, 所以需先用 mysqlbinlog命令将其解析为文本. mysqlbinlog带有--short-form参数时, 则 LOG TYPE 需指定为 udl. 命令可类似于如下:
mysqlbinlog /opt/mysql/data/mysql-bin.000001 | mysqlsla -lt binary -
mysqlbinlog --short-form. /opt/mysql/data/mysql-bin.000001 | mysqlsla -lt udl -
--explain or -ex explain 每句 SQL, 默认没有启用
--databases=dbname1,dbnam2[,...] or -db dbname1,dbnam2[,...] or -D dbname1,dbnam2[,...]用于 --explain
--flush-qc 强制刷新查询缓存, 默认没有启用
--grep="PATTERN" 只解析满足条件的 SQL
--meta-filter="CONDTIONS" or -mf "CONDTIONS" CONDITIONS 格式为[meta][op][value],多个条件时, 中间以,分割.
[meta] 查看这里
[op] >, < r =. 当 [meta] 是基于字符串时, [op] 只能是 =.
[value] 数字 or 字符串.
--reports=REPORTS or -R REPORTS, 默认是 standard, REPORTS 可以是以,为分割的列表. 可选选项:standard, time-all, print-unique, print-all, dump