pt-query-digest是用于分析mysql慢查询的一个工具,它能够分析binlog、General log、slowlog,也能够经过SHOW PROCESSLIST或者经过tcpdump抓取的MySQL协议数据来进行分析。能够把分析结果输出到文件中,分析过程是先对查询语句的条件进行参数化,而后对参数化之后的查询进行分组统计,统计出各查询的执行时间、次数、占比等,能够借助分析结果找出问题进行优化。mysql
--until 截止时间,配合—since能够分析一段时间内的慢查询。sql
第一部分:整体统计结果数据库
Overall:总共有多少条查询json
Time range:查询执行的时间范围服务器
unique:惟一查询数量,即对查询条件进行参数化之后,总共有多少个不一样的查询tcp
total:总计 min:最小 max:最大 avg:平均工具
95%:把全部值从小到大排列,位置位于95%的那个数,这个数通常最具备参考价值优化
median:中位数,把全部值从小到大排列,位置位于中间那个数spa
第二部分:查询分组统计结果
.net
Rank:全部语句的排名,默认按查询时间降序排列,经过--order-by指定
Query ID:语句的ID,(去掉多余空格和文本字符,计算hash值)
Response:总的响应时间
time:该查询在本次分析中总的时间占比
calls:执行次数,即本次分析总共有多少条这种类型的查询语句
R/Call:平均每次执行的响应时间
V/M:响应时间Variance-to-mean的比率
Item:查询对象
第三部分:每一种查询的详细统计结果
由下面查询的详细统计结果,最上面的表格列出了执行次数、最大、最小、平均、95%等各项目的统计。
ID:查询的ID号,和上图的Query ID对应
Databases:数据库名
Users:各个用户执行的次数(占比)
Query_time distribution :查询时间分布, 长短体现区间占比,本例中1s-10s之间查询数量是10s以上的两倍。
Tables:查询中涉及到的表
Explain:SQL语句
用法示例
1.直接分析慢查询文件:
pt-query-digest slow.log > slow_report.log
2.分析最近12小时内的查询:
pt-query-digest --since=12h slow.log > slow_report2.log
3.分析指定时间范围内的查询:
pt-query-digest slow.log --since '2017-01-07 09:30:00' --until '2017-01-07 10:00:00'> > slow_report3.log
4.分析指含有select语句的慢查询
pt-query-digest --filter '$event->{fingerprint} =~ m/^select/i' slow.log> slow_report4.log
5.针对某个用户的慢查询
pt-query-digest --filter '($event->{user} || "") =~ m/^root/i' slow.log> slow_report5.log
6.查询全部的全表扫描或full join的慢查询
pt-query-digest --filter '(($event->{Full_scan} || "") eq "yes") ||(($event->{Full_join} || "") eq "yes")' slow.log> slow_report6.log
7.把查询保存到query_review表
pt-query-digest --user=root –password=abc123 --review h=localhost,D=test,t=query_review--create-review-table slow.log
8.把查询保存到query_history表
pt-query-digest --user=root –password=abc123 --review h=localhost,D=test,t=query_history--create-review-table slow.log_0001 pt-query-digest --user=root –password=abc123 --review h=localhost,D=test,t=query_history--create-review-table slow.log_0002
9.经过tcpdump抓取mysql的tcp协议数据,而后再分析
tcpdump -s 65535 -x -nn -q -tttt -i any -c 1000 port 3306 > mysql.tcp.txt pt-query-digest --type tcpdump mysql.tcp.txt> slow_report9.log
10.分析binlog
mysqlbinlog mysql-bin.000093 > mysql-bin000093.sql pt-query-digest --type=binlog mysql-bin000093.sql > slow_report10.log
11.分析general log
pt-query-digest --type=genlog localhost.log > slow_report11.log
参考:http://www.jb51.net/article/107698.htm