Percona Toolkit

前兩天折騰了mysqlsla,今天才發現hackmysql.com中的工具已淪爲歷史產物,建議使用Percona Toolkit代替。
1、下載安裝html

[root@VMUest tools]# wget https://www.percona.com/downloads/percona-toolkit/3.0.2/binary/tarball/percona-toolkit-3.0.2_i386.tar.gz
[root@VMUest tools]# tar xf percona-toolkit-3.0.2_i386.tar.gz -C /usr/local/
[root@VMUest tools]# cd /usr/local/
[root@VMUest local]# mv /usr/local/percona-toolkit-3.0.2/ /usr/local/toolkit

[root@VMUest local]# cd toolkit
[root@VMUest toolkit]# cat README
#配置編譯安裝
[root@VMUest toolkit]# perl Makefile.PL
Checking if your kit is complete...
Looks good
Warning: prerequisite DBD::mysql 3 not found.
Writing Makefile for percona-toolkit
#根據需求安裝,以前mysqlsla已安裝部分依賴包
[root@VMUest toolkit]# yum install perl-DBD-MySQL

[root@VMUest toolkit]# perl Makefile.PL
[root@VMUest toolkit]# make && make test && make install

完成後相關工具自動添加到環境變量,個人機器上在/usr/local/bin/目錄下有對應的文件mysql

#查看安裝了哪些工具
[root@VMUest toolkit]# man percona-toolkit
#查看具體工具如何使用
[root@VMUest toolkit]# man pt-query-digest
[root@VMUest toolkit]# pt-query-digest --help

2、用法示例git

#1、直接分析慢查詢文件並將結果寫入118slow_report.log文件
[root@VMUest ~]# pt-query-digest /myshare/118mysqldumpslow-20161227.log > /myshare/118slow_report.log
[root@VMUest ~]# pt-query-digest --group-by fingerprint --order-by Query_time:sum --limit 10 /myshare/118mysqldumpslow-20161227.log
#2、查看指定fingerprint(參數化Query ID)的語句
[root@VMUest ~]# pt-query-digest /myshare/118mysqldumpslow-20161227.log --filter '$event->{fingerprint} && make_checksum($event->{fingerprint}) eq "6C0C2937AEF1D27C"'
[root@VMUest ~]# pt-query-digest /myshare/118mysqldumpslow-20161227.log --no-report --output slowlog --filter '$event->{fingerprint} && make_checksum($event->{fingerprint}) eq "6C0C2937AEF1D27C"'
#3、指定時間範圍內的查詢
[root@VMUest ~]# pt-query-digest /myshare/118mysqldumpslow-20161227.log --since '2016-12-27 07:51:20' --until '2016-12-27 10:10:34'
#4、分析含有delete語句的慢查詢
[root@VMUest ~]# pt-query-digest --filter '$event->{fingerprint} =~ m/^delete/i' /myshare/118mysqldumpslow-20161227.log
#5、把查詢保存到query_review表
[root@VMUest ~]# pt-query-digest --user=mydba --password=mysql5635 --review h=192.168.85.129,P=3306,D=test,t=query_review /myshare/118mysqldumpslow-20161227.log
mysql> select * from test.query_review;
#6、把查詢保存到query_history表
[root@VMUest ~]# pt-query-digest --user=mydba --password=mysql5635 --history h=192.168.85.129,P=3306,D=test,t=query_history /myshare/118mysqldumpslow-20161227.log
mysql> select * from test.query_history;
 
#7、分析binlog
[root@VMUest ~]# mysqlbinlog /usr/local/mysql/log/mysql-bin.000001 > /myshare/mysql-bin000001.sql
[root@VMUest ~]# pt-query-digest --type=binlog /myshare/mysql-bin000001.sql
#8、分析general log
mysql> set global general_log_file='/usr/local/mysql/log/mysql-general.log';
mysql> set global general_log =1;
[root@VMUest ~]# pt-query-digest --type=genlog /usr/local/mysql/log/mysql-general.log

3、報告解釋github

[root@VMUest ~]# pt-query-digest --filter '$event->{fingerprint} =~ m/^delete/i' /myshare/118mysqldumpslow-20161227.log --limit 100%

# 5.4s user time, 80ms system time, 13.06M rss, 17.15M vsz
# Current date: Fri Mar 31 17:37:00 2017
# Hostname: VMUest
# Files: /myshare/118mysqldumpslow-20161227.log
# Overall: 8 total, 2 unique, 0.00 QPS, 0.03x concurrency ________________
# Time range: 2016-12-27 00:00:28 to 00:30:35
# Attribute          total     min     max     avg     95%  stddev  median
# ============     ======= ======= ======= ======= ======= ======= =======
# Exec time            56s   256us     16s      7s     15s      5s      9s
# Lock time             9s    20us      9s      1s      9s      3s   128us
# Rows sent              0       0       0       0       0       0       0
# Rows examine       2.52M       0 657.18k 322.91k 650.62k 220.00k 410.39k
# Query size           504      56      84      63   80.10   11.21   54.21

# Profile
# Rank Query ID           Response time  Calls R/Call V/M   Item
# ==== ================== ============== ===== ====== ===== ==============
#    1 0x6C0C2937AEF1D27C 56.1986 100.0%     6 9.3664  1.08 DELETE Ranking
#    2 0xE0735FE2ADE92491   0.0008  0.0%     2 0.0004  0.00 DELETE Rank

# Query 1: 0.00 QPS, 0.03x concurrency, ID 0x6C0C2937AEF1D27C at byte 5162025
# This item is included in the report because it matches --limit.
# Scores: V/M = 1.08
# Time range: 2016-12-27 00:00:28 to 00:30:35
# Attribute    pct   total     min     max     avg     95%  stddev  median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count         75       6
# Exec time     99     56s      5s     16s      9s     15s      3s     10s
# Lock time     99      9s    20us      9s      2s      9s      3s    37us
# Rows sent      0       0       0       0       0       0       0       0
# Rows examine 100   2.52M 241.92k 657.18k 430.55k 650.62k 142.45k 487.82k
# Query size    66     336      56      56      56      56       0      56
# String:
# Databases    Zipai
# Hosts        192.168.85.129
# Users        root
# Query_time distribution
#   1us
#  10us
# 100us
#   1ms
#  10ms
# 100ms
#    1s  ################################################################
#  10s+  ################################
# Tables
#    SHOW TABLE STATUS FROM `Zipai` LIKE 'Ranking'\G
#    SHOW CREATE TABLE `Zipai`.`Ranking`\G
delete from Ranking where eventTypeID=2 and rankTypeID=7\G
# Converted for EXPLAIN
# EXPLAIN /*!50100 PARTITIONS*/
select * from  Ranking where eventTypeID=2 and rankTypeID=7\G

# Query 2: 0 QPS, 0x concurrency, ID 0xE0735FE2ADE92491 at byte 5183064 __
# This item is included in the report because it matches --limit.
# Scores: V/M = 0.00
# Time range: all events occurred at 2016-12-27 00:02:00
# Attribute    pct   total     min     max     avg     95%  stddev  median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count         25       2
# Exec time      0   827us   256us   571us   413us   571us   222us   413us
# Lock time      0   730us   205us   525us   365us   525us   226us   365us
# Rows sent      0       0       0       0       0       0       0       0
# Rows examine   0       0       0       0       0       0       0       0
# Query size    33     168      84      84      84      84       0      84
# String:
# Databases    Zipai (1/50%), test_db (1/50%)
# Hosts        %
# Users        root
# Query_time distribution
#   1us
#  10us
# 100us  ################################################################
#   1ms
#  10ms
# 100ms
#    1s
#  10s+
# Tables
#    SHOW TABLE STATUS FROM `Zipai` LIKE 'Rank'\G
#    SHOW CREATE TABLE `Zipai`.`Rank`\G
delete from Rank where typeID=16 or typeID=17 or typeID=13 or typeID=14 or typeID=15\G
# Converted for EXPLAIN
# EXPLAIN /*!50100 PARTITIONS*/
select * from  Rank where typeID=16 or typeID=17 or typeID=13 or typeID=14 or typeID=15\G
View Code

3.一、整體統計結果

第一行:此工具執行日誌分析時的所用時間、內存資源(rss物理內存佔用大小,vsz虛擬內存佔用大小)
第二行:分析時的系統時間
第三行:進行分析的主機名,非記錄日誌的數據庫服務器
第四行:分析的日誌文件名稱
第五行:文件中總共的語句數量(Overall 8),惟一的語句數量(對語句進行了格式化,unique 2),QPS,併發數
Time range:查詢執行的時間範圍
total:總計; min:最小; max:最大; avg:平均;95%:把全部值從小到大排列,位置位於95%的那個數,這個數通常最具備參考價值;median:中位數,把全部值從小到大排列,位置位於中間那個數
Exec time:語句執行時間;Lock time:鎖佔有時間;Rows sent:發送到客戶端的行數;Row examine:掃描的行數;Query size:查詢語句的字符數
3.二、查詢分組統計結果

Response time:總的響應時間以及該查詢在本次分析中總的時間佔比
Calls:執行次數,即本次分析總共有多少條這種類型的查詢語句
R/Call:平均每次執行的響應時間
Item:查詢對象
3.三、每一種查詢的詳細統計結果

最上面的表格列出了百分比、總計、最大、最小、平均、95%等各項目的統計
Databases:庫名
Users:用戶
Query_time distribution:查詢時間分佈, 長短體現區間佔比,本例中1s-10s之間查詢數量是10s以上的兩倍
Tables:查詢中涉及到的表
Explain:示例,非select語句會轉換爲select語句
14:43 2017/6/20 補充sql

# 參數說明
--limit        95%:20
--outliers     Query_time:1:10

limit默認返回前20條或者總響應時間佔比(從大到小)累計達到95%(取最早知足的)最差的查詢;outliers默認返回95%欄位的Query_time>=1秒,且執行次數>=10次。最終結果是取limit和outliers的並集。這也就是爲何顯示指定limit參數,返回的結果並不全知足limit參數,還會返回匹配outliers的結果。
若是使用了--limit或(和)--outliers參數,在返回結果的第二部分"查詢分組統計結果",rank的最末行是MISC,表明那些沒有計算入內的查詢統計結果。詳細內容參考官方文檔pt-query-digest.數據庫

相關文章
相關標籤/搜索