pt-query-digest [OPTIONS] [FILES] [DSN]


--create-review-table 当使用--review参数把分析结果输出到表中时,如果没有表就自动创建。


--create-history-table 当使用--history参数把分析结果输出到表中时,如果没有表就自动创建。


--filter 对输入的慢查询按指定的字符串进行匹配过滤后再进行分析


--limit 限制输出结果百分比或数量,默认值是20,即将最慢的20条语句输出,如果是50%则按总响应时间占比从大到小排序,输出到总和达到50%位置截止。


--host mysql服务器地址


--user mysql用户名


--password mysql用户密码


--history 将分析结果保存到表中,分析结果比较详细,下次再使用--history时,如果存在相同的语句,且查询所在的时间区间和历史表中的不同,则会记录到数据表中,可以通过查询同一CHECKSUM来比较某类型查询的历史变化。


--review 将分析结果保存到表中,这个分析只是对查询条件进行参数化,一个类型的查询一条记录,比较简单。当下次使用--review时,如果存在相同的语句分析,就不会记录到数据表中。


--output 分析结果输出类型,值可以是report(标准分析报告)、slowlog(Mysql slow log)、json、json-anon,一般使用report,以便于阅读。


--since 从什么时间开始分析,值为字符串,可以是指定的某个”yyyy-mm-dd [hh:mm:ss]”格式的时间点,也可以是简单的一个时间值:s(秒)、h(小时)、m(分钟)、d(天),如12h就表示从12小时前开始统计。


--until 截止时间,配合—since可以分析一段时间内的慢查询。


 


 


用法示例


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 /data/mysql/mysql_slow_query.log --since '2020-03-20 09:30:00' --until '2020-03-27 13:00:00'  > /tmp/mysql_slow_query.log_report.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