利用pt-query-digest分析MySQL慢查詢

來源:互聯網
上載者:User

標籤:.sql   root   tables   輸出   tcp   全表掃描   txt   mysql慢查詢   distinct   

1、用法與參數說明
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、json、json-anon,一般使用report,以便於閱讀。--since                ##從什麼時間開始分析,值為字串,可以是指定”yyyy-mm-dd [hh:mm:ss]”、s(秒)、h(小時)、m(分鐘)、d(天),如12h就表示從12小時前開始統計。--until                ##截止時間,配合—since可以分析一段時間內的慢查詢。

常用命令

##分析慢查詢檔案:pt-query-digest  slow.log > slow_report.log##分析最近12小時pt-query-digest  --since=12h  slow.log > slow_report2.log##分析指定時間範圍pt-query-digest slow.log --since ‘2017-01-07 09:30:00‘ --until ‘2017-01-07 10:00:00‘> > slow_report3.log##分析只含有select語句的慢查詢pt-query-digest --filter ‘$event->{fingerprint} =~ m/^select/i‘ slow.log> slow_report4.log##針對某個使用者的慢查詢pt-query-digest --filter ‘($event->{user} || "") =~ m/^root/i‘ slow.log> slow_report5.log##查詢所有所有的全表掃描或full join的慢查詢pt-query-digest --filter ‘(($event->{Full_scan} || "") eq "yes") ||(($event->{Full_join} || "") eq "yes")‘ slow.log> slow_report6.log##把查詢儲存到query_review表pt-query-digest --user=root –password=abc123 --review  h=localhost,D=test,t=query_review--create-review-table  slow.log##把查詢儲存到query_history表pt-query-digest  --user=root –password=abc123 --review  h=localhost,D=test,t=query_history--create-review-table  slow.log_0001pt-query-digest  --user=root –password=abc123 --review  h=localhost,D=test,t=query_history--create-review-table  slow.log_0002##通過tcpdump抓取mysql的tcp協議資料,然後再分析tcpdump -s 65535 -x -nn -q -tttt -i any -c 1000 port 3306 > mysql.tcp.txtpt-query-digest --type tcpdump mysql.tcp.txt> slow_report9.log##分析binlogmysqlbinlog mysql-bin.000093 > mysql-bin000093.sqlpt-query-digest  --type=binlog  mysql-bin000093.sql > slow_report10.log##分析general logpt-query-digest  --type=genlog  localhost.log > slow_report11.log
2、報告解讀

總體統計結果
Overall:       總共有多少條查詢
Time range:查詢執行的時間範圍
unique:       唯一查詢數量,即對查詢條件進行參數化以後,總共有多少個不同的查詢
total:           總計   min:最小   max:最大  avg:平均
95%:          把所有值從小到大排列,位置位於95%的那個數,這個數一般最具有參考價值
median:     中位元,把所有值從小到大排列,位置位於中間那個數

# Overall: 2 total, 2 unique, 0.00 QPS, 0.00x concurrency ________________# Time range: 2018-08-13T12:58:45 to 2018-08-14T13:02:05# Attribute          total     min     max     avg     95%  stddev  median# ============     ======= ======= ======= ======= ======= ======= =======# Exec time             6s    10ms      6s      3s      6s      5s      3s# Lock time          209us    82us   127us   104us   127us    31us   104us# Rows sent            100       0     100      50     100   70.71      50# Rows examine         411     111     300  205.50     300  133.64  205.50# Query size           310      76     234     155     234  111.72     155

查詢分組統計結果

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/M   # ==== ================================= ============= ===== ====== ===== #    1 0xF0C5AE75A52E847D737F39F04B19...  6.4700 99.8%     1 6.4700  0.00 SELECT sbtest?# MISC 0xMISC                             0.0100  0.2%     1 0.0100   0.0 <1 ITEMS>

SQL統計

由下面查詢的詳細統計結果,最上面的表格列出了執行次數、最大、最小、平均、95%等各項目的統計。
ID:                                   查詢的ID號,和的Query ID對應
Databases:                      資料庫名
Users:                              各個使用者執行的次數(佔比)
Query_time distribution : 查詢時間分布, 長短體現區間佔比,本例中1s-10s之間查詢數量是10s以上的兩倍。
Tables:                            查詢中涉及到的表
Explain:                           SQL語句

# Query 1: 0 QPS, 0x concurrency, ID 0xF0C5AE75A52E847D737F39F04B198EF6 at byte 0# This item is included in the report because it matches --limit.# Scores: V/M = 0.00# Time range: all events occurred at 2018-08-13T12:58:45# Attribute    pct   total     min     max     avg     95%  stddev  median# ============ === ======= ======= ======= ======= ======= ======= =======# Count         50       1# Exec time     99      6s      6s      6s      6s      6s       0      6s# Lock time     39    82us    82us    82us    82us    82us       0    82us# Rows sent    100     100     100     100     100     100       0     100# Rows examine  72     300     300     300     300     300       0     300# Query size    24      76      76      76      76      76       0      76# String:# Databases    yong# Hosts        192.168.10.16# Users        yong# Query_time distribution#   1us#  10us# 100us#   1ms#  10ms# 100ms#    1s  #################################################################  10s+# Tables#    SHOW TABLE STATUS FROM `yong` LIKE ‘sbtest8‘\G#    SHOW CREATE TABLE `yong`.`sbtest8`\G# EXPLAIN /*!50100 PARTITIONS*/SELECT DISTINCT c FROM sbtest8 WHERE id BETWEEN 519478 AND 519577 ORDER BY c\G

 

利用pt-query-digest分析MySQL慢查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.