pt-query-digest
pt-query-digest 簡介
pt-query-digest是用於分析mysql慢查詢的一個工具,它可以分析binlog、General log、slowlog,也可以通過SHOWPROCESSLIST或者通過tcpdump抓取的MySQL協議數據來進行分析。可以把分析結果輸出到文件中,分析過程是先對查詢語句的條件進行參數化,然後對參數化以後的查詢進行分組統計,統計出各查詢的執行時間、次數、佔比等,可以藉助分析結果找出問題進行優化。
安裝pt-query-digest
在官網下載pt安裝包,然後直接rpm安裝
rpm -ivh percona-toolkit-3.1.0-2.el7.x86_64.rpm
pt-query-digest語法及重要選項
使用pt-query-digest --help查看全部選項,下面選取一些重要選項
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可以分析一段時間內的慢查詢。
分析pt-query-digest輸出結果
第一部分:總體統計結果
Overall:總共有多少條查詢
Time range:查詢執行的時間範圍
unique:唯一查詢數量,即對查詢條件進行參數化以後,總共有多少個不同的查詢
total:總計 min:最小 max:最大 avg:平均
95%:把所有值從小到大排列,位置位於95%的那個數,這個數一般最具有參考價值
median:中位數,把所有值從小到大排列,位置位於中間那個數
# 該工具執行日誌分析的用戶時間,系統時間,物理內存佔用大小,虛擬內存佔用大小
# 570ms user time, 40ms system time, 32.85M rss, 309.68M vsz
# 工具執行時間
# Current date: Mon May 25 00:00:05 2020
# 運行分析工具的主機名
# Hostname: localhost.localdomain
# 被分析的文件名
# Files: /mysql/log/mysql-slow.log
# 語句總數量,唯一的語句數量,QPS,併發數
# Overall: 73 total, 2 unique, 0.00 QPS, 0.00x concurrency _______________
# 日誌記錄的時間範圍
# Time range: 2020-05-18 00:31:22 to 2020-05-21 18:00:14
# 屬性 總計 最小 最大 平均 95% 標準 中等
# Attribute total min max avg 95% stddev median
# ============ ======= ======= ======= ======= ======= ======= =======
# 語句執行時間
# Exec time 1516s 11s 62s 21s 24s 6s 20s
# 鎖佔用時間
# Lock time 5ms 52us 124us 66us 93us 14us 60us
# 發送到客戶端的行數
# Rows sent 2 0 1 0.03 0 0.16 0
# select語句掃描行數
# Rows examine 29.73M 99.08k 13.13M 416.98k 174.27k 1.66M 143.37k
# 查詢的字符數
# Query size 17.16k 231 241 240.73 234.30 1.82 234.30
第二部分:查詢分組統計結果
Rank:所有語句的排名,默認按查詢時間降序排列,通過–order-by指定
Query ID:語句的ID,(去掉多餘空格和文本字符,計算hash值)
Response:總的響應時間
time:該查詢在本次分析中總的時間佔比
calls:執行次數,即本次分析總共有多少條這種類型的查詢語句
R/Call:平均每次執行的響應時間
V/M:響應時間Variance-to-mean的比率
Item:查詢對象
# Profile
# Rank Query ID Response time Calls R/Call V/
# ==== ================================== =============== ===== ======= ==
# 1 0x13AA2790C74007F7D46C64DC823C9123 1433.0437 94.5% 71 20.1837 0.64 DELETE SELECT events problem
# 2 0x5AC1605D997951F8B5F6CD8D47517C90 82.9604 5.5% 2 41.4802 19.94 SELECT events problem
第三部分:每一種查詢的詳細統計結果
由下面查詢的詳細統計結果,最上面的表格列出了執行次數、最大、最小、平均、95%等各項目的統計。
ID:查詢的ID號,和上圖的Query ID對應
Databases:數據庫名
Users:各個用戶執行的次數(佔比)
Query_time distribution :查詢時間分佈, 長短體現區間佔比,本例中1s-10s之間查詢數量是10s以上的兩倍。
Tables:查詢中涉及到的表
Explain:SQL語句
# Query 1: 0 QPS, 0x concurrency, ID 0xF9A57DD5A41825CA at byte 802 ______
# This item is included in the report because it matches --limit.
# Scores: V/M = 0.00
# Time range: all events occurred at 2016-11-22 06:11:40
# Attribute pct total min max avg 95% stddev median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count 50 1
# Exec time 76 2s 2s 2s 2s 2s 0 2s
# Lock time 0 0 0 0 0 0 0 0
# Rows sent 20 1 1 1 1 1 0 1
# Rows examine 0 0 0 0 0 0 0 0
# Query size 3 15 15 15 15 15 0 15
# String:
# Databases test
# Hosts 192.168.2.1
# Users mysql
# Query_time distribution
# 1us
# 10us
# 100us
# 1ms
# 10ms
# 100ms
# 1s ################################################################
# 10s+
# EXPLAIN /*!50100 PARTITIONS*/
select sleep(2)\G
自定義創建query_review表和query_history表
備註:默認會生成query_review表和query_history表,因爲一些字段不存在需要自己添加,一些字段你們可以刪掉不使用。
CREATE TABLE `mysql_slow_query_review` (
`checksum` char(32) NOT NULL,
`fingerprint` text NOT NULL,
`sample` text NOT NULL,
`first_seen` datetime DEFAULT NULL,
`last_seen` datetime DEFAULT NULL,
`reviewed_by` varchar(20) DEFAULT NULL,
`reviewed_on` datetime DEFAULT NULL,
`comments` text,
`reviewed_status` varchar(24) DEFAULT NULL,
PRIMARY KEY (`checksum`),
KEY `idx_last_seen` (`last_seen`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `mysql_slow_query_review_history` (
`host_max` varchar(64) DEFAULT NULL,
`user_max` varchar(64) DEFAULT NULL,
`db_max` varchar(64) DEFAULT NULL,
`checksum` char(32) NOT NULL,
`sample` text NOT NULL,
`ts_min` datetime NOT NULL,
`ts_max` datetime NOT NULL,
`ts_cnt` float DEFAULT NULL,
`Query_time_sum` float DEFAULT NULL,
`Query_time_min` float DEFAULT NULL,
`Query_time_max` float DEFAULT NULL,
`Query_time_pct_95` float DEFAULT NULL,
`Query_time_stddev` float DEFAULT NULL,
`Query_time_median` float DEFAULT NULL,
`Lock_time_sum` float DEFAULT NULL,
`Lock_time_min` float DEFAULT NULL,
`Lock_time_max` float DEFAULT NULL,
`Lock_time_pct_95` float DEFAULT NULL,
`Lock_time_stddev` float DEFAULT NULL,
`Lock_time_median` float DEFAULT NULL,
`Rows_sent_sum` float DEFAULT NULL,
`Rows_sent_min` float DEFAULT NULL,
`Rows_sent_max` float DEFAULT NULL,
`Rows_sent_pct_95` float DEFAULT NULL,
`Rows_sent_stddev` float DEFAULT NULL,
`Rows_sent_median` float DEFAULT NULL,
`Rows_examined_sum` float DEFAULT NULL,
`Rows_examined_min` float DEFAULT NULL,
`Rows_examined_max` float DEFAULT NULL,
`Rows_examined_pct_95` float DEFAULT NULL,
`Rows_examined_stddev` float DEFAULT NULL,
`Rows_examined_median` float DEFAULT NULL,
`Rows_affected_sum` float DEFAULT NULL,
`Rows_affected_min` float DEFAULT NULL,
`Rows_affected_max` float DEFAULT NULL,
`Rows_affected_pct_95` float DEFAULT NULL,
`Rows_affected_stddev` float DEFAULT NULL,
`Rows_affected_median` float DEFAULT NULL,
`Rows_read_sum` float DEFAULT NULL,
`Rows_read_min` float DEFAULT NULL,
`Rows_read_max` float DEFAULT NULL,
`Rows_read_pct_95` float DEFAULT NULL,
`Rows_read_stddev` float DEFAULT NULL,
`Rows_read_median` float DEFAULT NULL,
`Merge_passes_sum` float DEFAULT NULL,
`Merge_passes_min` float DEFAULT NULL,
`Merge_passes_max` float DEFAULT NULL,
`Merge_passes_pct_95` float DEFAULT NULL,
`Merge_passes_stddev` float DEFAULT NULL,
`Merge_passes_median` float DEFAULT NULL,
`InnoDB_IO_r_ops_min` float DEFAULT NULL,
`InnoDB_IO_r_ops_max` float DEFAULT NULL,
`InnoDB_IO_r_ops_pct_95` float DEFAULT NULL,
`InnoDB_IO_r_ops_stddev` float DEFAULT NULL,
`InnoDB_IO_r_ops_median` float DEFAULT NULL,
`InnoDB_IO_r_bytes_min` float DEFAULT NULL,
`InnoDB_IO_r_bytes_max` float DEFAULT NULL,
`InnoDB_IO_r_bytes_pct_95` float DEFAULT NULL,
`InnoDB_IO_r_bytes_stddev` float DEFAULT NULL,
`InnoDB_IO_r_bytes_median` float DEFAULT NULL,
`InnoDB_IO_r_wait_min` float DEFAULT NULL,
`InnoDB_IO_r_wait_max` float DEFAULT NULL,
`InnoDB_IO_r_wait_pct_95` float DEFAULT NULL,
`InnoDB_IO_r_wait_stddev` float DEFAULT NULL,
`InnoDB_IO_r_wait_median` float DEFAULT NULL,
`InnoDB_rec_lock_wait_min` float DEFAULT NULL,
`InnoDB_rec_lock_wait_max` float DEFAULT NULL,
`InnoDB_rec_lock_wait_pct_95` float DEFAULT NULL,
`InnoDB_rec_lock_wait_stddev` float DEFAULT NULL,
`InnoDB_rec_lock_wait_median` float DEFAULT NULL,
`InnoDB_queue_wait_min` float DEFAULT NULL,
`InnoDB_queue_wait_max` float DEFAULT NULL,
`InnoDB_queue_wait_pct_95` float DEFAULT NULL,
`InnoDB_queue_wait_stddev` float DEFAULT NULL,
`InnoDB_queue_wait_median` float DEFAULT NULL,
`InnoDB_pages_distinct_min` float DEFAULT NULL,
`InnoDB_pages_distinct_max` float DEFAULT NULL,
`InnoDB_pages_distinct_pct_95` float DEFAULT NULL,
`InnoDB_pages_distinct_stddev` float DEFAULT NULL,
`InnoDB_pages_distinct_median` float DEFAULT NULL,
`QC_Hit_cnt` float DEFAULT NULL,
`QC_Hit_sum` float DEFAULT NULL,
`Full_scan_cnt` float DEFAULT NULL,
`Full_scan_sum` float DEFAULT NULL,
`Full_join_cnt` float DEFAULT NULL,
`Full_join_sum` float DEFAULT NULL,
`Tmp_table_cnt` float DEFAULT NULL,
`Tmp_table_sum` float DEFAULT NULL,
`Tmp_table_on_disk_cnt` float DEFAULT NULL,
`Tmp_table_on_disk_sum` float DEFAULT NULL,
`Filesort_cnt` float DEFAULT NULL,
`Filesort_sum` float DEFAULT NULL,
`Filesort_on_disk_cnt` float DEFAULT NULL,
`Filesort_on_disk_sum` float DEFAULT NULL,
`Bytes_sum` float DEFAULT NULL,
`Bytes_min` float DEFAULT NULL,
`Bytes_max` float DEFAULT NULL,
`Bytes_pct_95` float DEFAULT NULL,
`Bytes_stddev` float DEFAULT NULL,
`Bytes_median` float DEFAULT NULL,
PRIMARY KEY (checksum, ts_min, ts_max),
KEY `idx_hostname_max_ts_min` (`host_max`,`ts_min`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
生成一週前100條慢日誌SQL數據
pt-query-digest /mysql/logs/mysql-slow.log --user=root --password=12345678 --socket=/tmp/mysql.sock --since=7d --review t=mysql_slow_query_review --history t=mysql_slow_query_review_history --max-hostname-length=0 --limit=100 --charset=utf8
部署腳本定時生成
#!/bin/bash
DIR="$( cd "$( dirname "$0" )" && pwd )"
cd $DIR
#配置數據庫的連接地址
monitor_db_host=127.0.0.1""
monitor_db_port=3306
monitor_db_user="root"
monitor_db_password="12345678"
monitor_db_socket="/tmp/mysql3306.sock"
dbname="percona_schema"
table_name1="mysql_slow_query_review"
table_name2="mysql_slow_query_review_history"
#實例慢日誌位置
slowquery_file="/mysql/logs/mysql-slow.log"
pt_query_digest="/usr/bin/pt-query-digest"
#刪除歷史表數據
truncate_sql1="truncate table ${table_name1}"
truncate_sql2="truncate table ${table_name2}"
mysql -h${monitor_db_host} -P${monitor_db_port} -u${monitor_db_user} -p${monitor_db_password} --socket=${monitor_db_socket} ${dbname} -e "${truncate_sql1}"
mysql -h${monitor_db_host} -P${monitor_db_port} -u${monitor_db_user} -p${monitor_db_password} --socket=${monitor_db_socket} ${dbname} -e "${truncate_sql2}"
#收集日誌
$pt_query_digest \
--user=$monitor_db_user --password=$monitor_db_password --port=$monitor_db_port --socket=$monitor_db_socket \
--review h=$monitor_db_host,t=mysql_slow_query_review \
--history h=$monitor_db_host,t=mysql_slow_query_review_history \
--limit=100 --since=7d --max-hostname-length=0 --charset=utf8 \
$slowquery_file > /tmp/analysis_slow_query.log
例舉用法示例
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 ‘2020-05-10 09:30:00’ --until ‘2020-05-10 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_join} || “”) eq “yes”)’ slow.log> slow_report6.log
7.把查詢保存到query_review表
pt-query-digest --user=root --password=12345678 --review h=localhost,D=test,t=query_review --create-review-table slow.log
8.把查詢保存到query_history表
pt-query-digest --user=root --password=12345678 --review h=localhost,D=test,t=query_history --create-review-table slow.log_0001
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