萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> MySQL數據庫慢日志分析工具mysqlsla使用教程

MySQL數據庫慢日志分析工具mysqlsla使用教程

mysqlsla是一個慢日志分析工具了,我們可以把慢日志對過mysqlsla進行分析從而得出性能最差的sql語句了,下面我們一起來看看吧.


mysqlsla是hackmysql.com推出的一款mysql的日志分析工具,功能非常強大. 數據報表,非常有利於分析慢查詢的原因, 包括執行頻率, 數據量, 查詢消耗等。

一、使用

mysqlsla -lt slow mysql-slow.log

或者

mysqlsla -lt slow mysql-slow.log -sf “+SELECT” -db dbName -top 10 -sort t_sum

參數意義

lt:表示日志類型,有slow, general, binary, msl, udl。
sf:[+-][type],[TYPE]有SELECT, CREATE, DROP, UPDATE, INSERT,例如”+SELECT,INSERT”,不出現的默認是-,即不包括。
db:要處理哪個庫的日志。
top:表示取按規則排序的前多少條。
sort:按某種規則排序,t_sum按總時間排序, c_sum按總次數排序。

二、分析結果

總查詢次數 (queries total), 去重後的sql數量 (unique)

輸出報表的內容排序(sorted by)

最重大的慢sql統計信息, 包括 平均執行時間, 等待鎖時間, 結果行的總數, 掃描的行總數.

Count, sql的執行次數及占總的slow log數量的百分比.
time, 執行時間, 包括總時間, 平均時間, 最小, 最大時間, 時間占到總慢sql時間的百分比.
95% of Time, 去除最快和最慢的sql, 覆蓋率占95%的sql的執行時間.
Lock Time, 等待鎖的時間.
95% of Lock , 95%的慢sql等待鎖時間.
Rows sent, 結果行統計數量, 包括平均, 最小, 最大數量.
Rows examined, 掃描的行數量.
Database, 屬於哪個數據庫
users, 哪個用戶,ip, 占到所有用戶執行的sql百分比
Query abstract, 抽象後的sql語句
Query sample, sql語句

三、安裝

方法一

從http://hackmysql.com/mysqlsla下載mysqlsla,當前最新版為:2.03

tar zxvf mysqlsla-2.03.tar.gz
cp mysqlsla-2.03/bin/mysqlsla /usr/local/bin
如果提示“Can’t locate DBI.pm”,執行以下命令

yum install perl-DBI perl-DBD-MySQL -y
這時就可以使用mysqlsla了,用法如下

mysqlsla -lt slow slow.log

方法二

wgethttp://hackmysql.com/scripts/mysqlsla-2.03.tar.gz
# tar xzvf mysqlsla-2.03.tar.gz
mysqlsla-2.03/
mysqlsla-2.03/Changes
mysqlsla-2.03/INSTALL
mysqlsla-2.03/README
mysqlsla-2.03/Makefile.PL
mysqlsla-2.03/bin/
mysqlsla-2.03/bin/mysqlsla
mysqlsla-2.03/META.yml
mysqlsla-2.03/lib/
mysqlsla-2.03/lib/mysqlsla.pm
mysqlsla-2.03/MANIFEST
# cd mysqlsla-2.03
# ll
總計 68

drwxr-xr-x 2 1000 1000 4096 2008-11-11 bin
-rw-r–r– 1 1000 1000 5630 2008-11-11 Changes
-rw-r–r– 1 1000 1000 394 2008-07-13 INSTALL
drwxr-xr-x 2 1000 1000 4096 2008-11-11 lib
-rw-r–r– 1 1000 1000 389 2008-07-13 Makefile.PL
-rw-r–r– 1 1000 1000 152 2008-07-13 MANIFEST
-rw-r–r– 1 1000 1000 303 2008-11-11 META.yml
-rw-r–r– 1 1000 1000 1969 2008-11-11 README
# less INSTALL

Installing mysqlsla
===================

perl Makefile.PL
make
make install

Depending on your system, the mysqlsla script will be copied to some common bin
directory (/usr/local/bin/ for example). Then you should be able to run it from
the command line like: mysqlsla -lt slow LOG

For quick help, man mysqlsla

For all documentation and guides, visit http://hackmysql.com/mysqlsla

# less Makefile.PL
use 5.008004;
use ExtUtils::MakeMaker;

WriteMakefile(
NAME => ‘mysqlsla’,
VERSION_FROM => ‘lib/mysqlsla.pm’,
PREREQ_PM => {},
($] >= 5.005 ?
(ABSTRACT_FROM => ‘lib/mysqlsla.pm’, # retrieve abstract from module
AUTHOR => ‘Daniel Nichter (http://hackmysql.com/contact)’) : ()),
EXE_FILES => [ 'bin/mysqlsla' ],
);
# perl Makefile.PL
Checking if your kit is complete…
Looks good
Writing Makefile for mysqlsla
# ll
總計 96
drwxr-xr-x 2 1000 1000 4096 2008-11-11 bin
-rw-r–r– 1 1000 1000 5630 2008-11-11 Changes
-rw-r–r– 1 1000 1000 394 2008-07-13 INSTALL
drwxr-xr-x 2 1000 1000 4096 2008-11-11 lib
-rw-r–r– 1 root root 22722 09-08 18:52 Makefile
-rw-r–r– 1 1000 1000 389 2008-07-13 Makefile.PL
-rw-r–r– 1 1000 1000 152 2008-07-13 MANIFEST
-rw-r–r– 1 1000 1000 303 2008-11-11 META.yml
-rw-r–r– 1 1000 1000 1969 2008-11-11 README
 

# make
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
# make install
Installing /usr/lib/perl5/site_perl/5.8.8/mysqlsla.pm
Installing /usr/share/man/man3/mysqlsla.3pm
Installing /usr/bin/mysqlsla
Writing /usr/lib/perl5/site_perl/5.8.8/i386-Linux-thread-multi/auto/mysqlsla/.packlist
Appending installation info to /usr/lib/perl5/5.8.8/i386-linux-thread-multi/perllocal.pod
默認將命令mysqlsla安裝到/usr/bin目錄,執行mysqlsla –help時提示沒有安裝DBI模塊,mysqlsla是用perl寫的。

第一次通過CPAN安裝perl模塊時,需要進行相關的配置,大部分配置采用默認值,一路回車即可。

#perl -MCPAN -e Shell

然後在下面的各個詢問中可以改已經配置好的選項,然後執行下面的命令保存

CPAN>reload index
CPAN>reload cpan

安裝DBI模塊

CPAN>install DBI
如果是第一次以後進入CPAN系統,想修改以前的配置,可以在CPAN命令行(即CPAN>)鍵入以下指令:

CPAN>o conf init
同時要執行下面的命令保存

CPAN>reload index
CPAN>reload cpan

man mysqlsla可以看到mysqlsla的相關選項

# man mysqlsla
XXX
XXX WARNING: old character encoding and/or character set
XXX
mysqlsla(3) User Contributed Perl Documentation mysqlsla(3)

NAME
mysqlsla – Parse, filter, analyze and sort MySQL slow, general and binary logs

SYNOPSIS
# Basic operation: parse a MySQL slow or general log
mysqlsla –log-type slow LOG
mysqlsla –log-type general LOG

# Parse output from mysqlbinlog
# mysqlsla cannot directly parse binary logs
mysqlbinlog LOG │ mysqlsla –log-type binary -

# Parse a microslow patched slow log
mysqlsla –log-type msl LOG

# Replay a replay file
mysqlsla –replay FILE

# Parse a user-defined log specify its format
mysqlsla –log-type udl –udl-format FILE

# Let mysqlsla automatically determine the log type
mysqlsla LOG

DESCRIPTION
mysqlsla parses, filters, analyzes and sorts MySQL slow, general, binary and microslow patched slow logs.
It also supports user-defined logs.

This POD/man page is only a very brief outline of usage and command line options. For the full library of
mysqlsla documentation visithttp://hackmysql.com/mysqlsla.

# mysqlsla /tmp/mysqlslow.log | less
Auto-detected logs as slow logs
Report for slow logs: /tmp/mysqlslow.log
6 queries total, 6 unique
Sorted by ‘t_sum’
Grand Totals: Time 0 s, Lock 0 s, Rows sent 13, Rows Examined 13
______________________________________________________________________ 001 ___
Count : 1 (16.67%)
Time : 2.321 ms total, 2.321 ms avg, 2.321 ms to 2.321 ms max (47.20%)
Lock Time (s) : 629 ?s total, 629 ?s avg, 629 ?s to 629 ?s max (77.75%)
Rows sent : 8 avg, 8 to 8 max (61.54%)
Rows examined : 8 avg, 8 to 8 max (61.54%)
Database :
Users :
root@ 192.168.1.100 : 100.00% (1) of query, 100.00% (6) of all users

Query abstract:
SET timestamp=N; SHOW variables LIKE ‘S’;

Query sample:
SET timestamp=1252395365;
show variables like ‘%character%’;
……
格式說明如下:

總查詢次數 (queries total), 去重後的sql數量 (unique)
輸出報表的內容排序(sorted by)
最重大的慢sql統計信息, 包括平均執行時間, 等待鎖時間, 結果行的總數, 掃描的行總數.

Count, sql的執行次數及占總的slow log數量的百分比.
Time, 執行時間, 包括總時間, 平均時間, 最小, 最大時間, 時間占到總慢sql時間的百分比.
95% of Time, 去除最快和最慢的sql, 覆蓋率占95%的sql的執行時間.
Lock Time, 等待鎖的時間.
95% of Lock , 95%的慢sql等待鎖時間.
Rows sent, 結果行統計數量, 包括平均, 最小, 最大數量.
Rows examined, 掃描的行數量.
Database, 屬於哪個數據庫
Users, 哪個用戶,IP, 占到所有用戶執行的sql百分比
Query abstract, 抽象後的sql語句
Query sample, sql語句
除了以上的輸出, 官方還提供了很多定制化參數, 是一款不可多得的好工具.

copyright © 萬盛學電腦網 all rights reserved