1. 程式人生 > >pt-query-digest

pt-query-digest

pt-query-digest

pt-query-digest

可以用來分析binlog、General log、slowlog,也可以通過show processlist或者通過tcpdump抓取的MySQL協議數據來進行分析


用法:


(1)直接分析slow日誌:

pt-query-digest INDEX01184W-slow.log > slow_report1.log


(2)分析最近12小時內的查詢:

pt-query-digest --since=12h INDEX01184W-slow.log > slow_report2.log


(3)分析指定時間範圍內的查詢:

#pt-query-digest --since ‘2017-02-01 09:30:00‘ --until ‘2017-03-01 10:00:00‘ INDEX01184W-slow.log >> slow_report3.log


(4)分析指含有select語句的慢查詢

pt-query-digest --filter ‘$event->{fingerprint} =~ m/^select/i‘ INDEX01184W-slow.log> slow_report4.log


(5) 針對某個用戶的慢查詢

pt-query-digest --filter ‘($event->{user} || "") =~ m/^root/i‘ INDEX01184W-slow.log> slow_report5.log


(6) 查詢所有所有的全表掃描或full join的慢查詢

pt-query-digest --filter ‘(($event->{Full_scan} || "") eq "yes") ||(($event->{Full_join} || "") eq "yes")‘ INDEX01184W-slow.log> slow_report6.log


(7)把查詢保存到query_review表(#默認會創建percona_schema庫和query_review表 )

pt-query-digest --user=root --password=147258 --review h=localhost INDEX01184W-slow.log


(8)把查詢保存到query_history表

pt-query-digest --user=root –password=abc123 --review h=localhost INDEX01184W-slow.log


(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.000003 > mysql-bin000003.sql

pt-query-digest --type=binlog mysql-bin000003.sql > slow_report10.log


(11)分析general log

pt-query-digest --type=genlog general.log > slow_report11.log


例:

#查詢兩條慢SQL:

[email protected] [(none)]>select sleep(3);

+----------+

| sleep(3) |

+----------+

| 0 |

+----------+

1 row in set (3.70 sec)

[email protected] [(none)]>select sleep(4);

+----------+

| sleep(4) |

+----------+

| 0 |

+----------+

1 row in set (4.02 sec)

[email protected] [(none)]>select sleep(8);

+----------+

| sleep(8) |

+----------+

| 0 |

+----------+

1 row in set (8.07 sec)


#查看slow日誌,可以發現會記錄上面兩條SQL:

[[email protected] data]# cat slow.log

# Time: 2017-06-02T05:06:04.452125Z

# [email protected]: root[root] @ localhost [] Id: 5565

# Query_time: 3.665139 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0

SET timestamp=1496379964;

select sleep(3);

# Time: 2017-06-02T05:35:42.145231Z

# [email protected]: root[root] @ localhost [] Id: 6454

# Query_time: 4.013508 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0

SET timestamp=1496381742;

select sleep(4);

# Time: 2017-06-02T07:29:33.820712Z

# [email protected]: root[root] @ localhost [] Id: 9867

# Query_time: 8.032160 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0

SET timestamp=1496388573;

select sleep(8);


#使用pt-query-digest分析slow日誌文件:

[[email protected] data]# pt-query-digest slow.log

# 170ms user time, 70ms system time, 24.36M rss, 204.71M vsz

# Current date: Fri Jun 2 15:30:17 2017

# Hostname: Darren1

# Files: slow.log

# Overall: 3 total, 1 unique, 0.00 QPS, 0.00x concurrency ________________

# Time range: 2017-06-02T05:06:04 to 2017-06-02T07:29:33

# Attribute total min max avg 95% stddev median

# ============ ======= ======= ======= ======= ======= ======= =======

# Exec time 16s 4s 8s 5s 8s 2s 4s

# Lock time 0 0 0 0 0 0 0

# Rows sent 3 1 1 1 1 0 1

# Rows examine 0 0 0 0 0 0 0

# Query size 45 15 15 15 15 0 15


第一部分:

Overall: 總共有多少條查詢,上例為總共3個查詢

unique: 對SQL進行分類,總的SQL種類,上例為1種

Time range: 查詢執行的時間範圍

total: 總計 min:最小 max: 最大 avg:平均

95%: 把所有值從小到大排列,位置位於95%的那個數,這個數一般最具有參考價值。

median: 中位數,把所有值從小到大排列,位置位於中間那個數。

# Profile

# Rank Query ID Response time Calls R/Call V/M Item

# ==== ================== ============== ===== ====== ===== ======

# 1 0xF9A57DD5A41825CA 15.7108 100.0% 3 5.2369 0.68 SELECT


第二部分:

對SQL進行分組,然後對各類查詢的執行情況進行分析,結果按總執行時長,從大到小排序。

Response: 總的響應時間。

time: 該查詢在本次分析中總的時間占比。

calls: 執行次數,即本次分析總共有多少條這種類型的查詢語句。

R/Call: 平均每次執行的響應時間。

Item : 查詢對象

# Query 1: 0.00 QPS, 0.00x concurrency, ID 0xF9A57DD5A41825CA at byte 409

# This item is included in the report because it matches --limit.

# Scores: V/M = 0.68

# Time range: 2017-06-02T05:06:04 to 2017-06-02T07:29:33

# Attribute pct total min max avg 95% stddev median

# ============ === ======= ======= ======= ======= ======= ======= =======

# Count 100 3

# Exec time 100 16s 4s 8s 5s 8s 2s 4s

# Lock time 0 0 0 0 0 0 0 0

# Rows sent 100 3 1 1 1 1 0 1

# Rows examine 0 0 0 0 0 0 0 0

# Query size 100 45 15 15 15 15 0 15

# String:

# Hosts localhost

# Users root

# Query_time distribution

# 1us

# 10us

# 100us

# 1ms

# 10ms

# 100ms

# 1s ################################################################

# 10s+

# EXPLAIN /*!50100 PARTITIONS*/

select sleep(8)\G


第三部分:

Databases: 庫名

Users: 各個用戶執行的次數(占比)

Query_time distribution : 查詢時間分布圖, 長短體現區間占比,本例中SQL處於1s-10s。

Tables: 查詢中涉及到的表

Explain: 示例

#把分析結果記錄到表中DSN

[[email protected] data]# pt-query-digest --user=root --password=147258 --review h=localhost slow.log

[email protected] [percona_schema]>select * from percona_schema.query_review\G

......

*************************** 2. row ***************************

checksum: 17988922643135866314

fingerprint: select sleep(?)

sample: select sleep(8)

first_seen: 2017-06-02 05:06:04

last_seen: 2017-06-02 07:29:33

reviewed_by: NULL

reviewed_on: NULL

comments: NULL

2 rows in set (0.00 sec)


本文出自 “10979687” 博客,請務必保留此出處http://10989687.blog.51cto.com/10979687/1931782

pt-query-digest