mysqlsla mysql慢查询日志归纳工具
当前mysqlsla的最新版本为2.03,可以下拉到官网下载,地址如下:
http://hackmysql.com/scripts/mysqlsla-2.03.tar.gz
mysqlsla是perl编写的脚本,运行mysqlsla需要perl-DBI和per-DBD-Mysql两模块的支持,因此在运行mysqlsla前需要首先安装DBI模块和相应的数据库DBD驱动,而默认情况下linux不安装这两个模块,需要自行下载安装,下载地址如下:
http://www.cpan.org/modules/by-module/DBI/DBI-1.608.tar.gz
http://www.cpan.org/modules/by-module/DBD/DBD-mysql-4.011.tar.gz
DBI的编译安装步骤如下:
# tar xvfz DBI-1.608.tar.gz
# cd DBI-1.608
# perl Makefile.PL
# make
# make test
# make install
DBD-mysql驱动模块的编译安装步骤如下:
# tar xvfz DBD-mysql-4.011.tar.gz
# cd DBD-mysql-4.011
# perl Makefile.PL
# make
# make install
需要注意,在安装DBD-mysql时需要用到mysql_config,该命令包含在MySQL-devel安装包中,如果当前系统中没有安装该软件,需要首先安装MySQL-devel,否则DBD-mysql在编译过程中会出现错误。
准备工作完全,就可以安装mysqlsla了,编译安装步骤如下:
# tar xvfz mysqlsla-2.03.tar.gz
# cd mysqlsla-2.03
# perl Makefile.PL
# make
# make install
mysqlsla命令默认会保存在/usr/bin路径下,通常可在任意路径下直接执行。对慢查询日志文件的分析,最简化的调用方式如下:
# mysqlsla -lt slow [SlowLogFilePath] > [ResultFilePath]
比如说,原始慢日志中有一堆的下列语句:
# Time: 130605 10:43:18
# User@Host: twitter[twitter] @ [192.10.8.106]
# Query_time: 19.012133 Lock_time: 0.000040 Rows_sent: 1 Rows_examined: 4213235
SET timestamp=1370400198;
select * from twitter_users where 1=1 and nick_name like '%<B3><C2><C0><EF>%' order by user_id desc limit 0, 50;
# Time: 130605 10:43:20
....................
直接阅读的操作体验很不好,使用mysqlsla处理后,结果呈现如下:
Report for slow logs: 0605-slow.log
20 queries total, 6 unique
Sorted by 't_sum'
Grand Totals: Time 383 s, Lock 0 s, Rows sent 369, Rows Examined 105.09M
______________________________________________________________________ 001 ___
Count : 9 (45.00%)
Time : 132.918433 s total, 14.768715 s avg, 11.551593 s to 27.165757 s max (34.74%)
Lock Time (s) : 835 μs total, 93 μs avg, 58 μs to 184 μs max (51.99%)
Rows sent : 30 avg, 30 to 30 max (73.17%)
Rows examined : 5.79M avg, 4.90M to 5.90M max (49.54%)
Database :
Users :
twitter@ 192.10.8.106 : 88.89% (8) of query, 75.00% (15) of all users
twitter@ 192.10.8.105 : 11.11% (1) of query, 25.00% (5) of all users
Query abstract:
SET timestamp=N; SELECT u.user_id,u.nick_name,u.home_city_name,u.sex,u.is_validate,u.user_head_middle,u.person_url, us.follower_num, us.focuson_num, us.msg_num, f.sign_time FROM twitter_users u,twitter_followers f, twitter_user_statistics us WHERE u.user_id=f.follower_id AND u.user_id=us.user_id AND u.isclose=N AND f.user_id=N ORDER BY f.sign_time DESC, u.user_id ASC LIMIT N,N;
Query sample:
SET timestamp=1370409152;
SELECT u.user_id,u.nick_name,u.home_city_name,u.sex,u.is_validate,u.user_head_middle,u.person_url, us.follower_num, us.focuson_num, us.msg_num, f.sign_time FROM twitter_users u,twitter_followers f, twitter_user_statistics us WHERE u.user_id=f.follower_id AND u.user_id=us.user_id AND u.isclose=0 and f.user_id=3016 order by f.sign_time desc, u.user_id asc limit 0,30;