Percona Toolkit使用之pt-query-digest

版权声明:本文为博主原创文章,转载敬请作出引用声明方便相互交流学习! https://blog.csdn.net/sweeper_freedoman/article/details/79931172

     pt-query-digest的功能是分析日志、进程列表以及tcpdump里的MySQL查询。

     用法如下:

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

     pt-query-digest分析慢查询日志、查询日志以及二进制日志文件里的MySQL查询。它也可以分析“ SHOW PROCESSLIST ”里的查询以及来自tcpdump的MySQL协议数据。默认情况下,查询按fingerprint分组然后按照查询时间降序展现(例如:最慢的查询排在最前面)。如果没有给出FILES选项,工具读取STDIN。可选项DSN(MySQL连接访问)用于某些特定选项,例如--since和--until。

     ①报告slow.log里最慢的查询:

pt-query-digest slow.log

     ②报告host1上进程列表里最慢的查询:

pt-query-digest --processlist h=host1

     ③用tcpdump捕捉MySQL协议数据然后报告最慢的查询:

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.log里的查询数据保存到host2用于复审和趋势分析:

pt-query-digest --review h=host2 --no-report slow.log

     pt-query-digest是个虽然复杂但是易用的分析MySQL查询的工具。它可以分析MySQL慢查询日志、查询日志以及二进制日志(二进制日志必须先转换为text,参考--type选项)里的查询。它也可以使用“ SHOW PROCESSLIST ”和来自tcpdump的MySQL协议数据。默认情况下,工具报告出哪些查询是最慢的,即最需要重点去优化。更多复杂的和自定义的报告可以通过使用选项例如--group-by、--filter和--embedded-attributes来创建。

     查询分析是一个需要经常去做的最佳实践。为了简化该工作,pt-query-digest提供了两个特性:查询复审(--review)和查询历史(--history)。当使用了--review选项时,所有的不重复查询被保存到数据库。当工具再次以--review选项执行时,数据库里面被标识为复审的查询将不会再打印到报告里。这突出了需要去复审的新查询。当使用--history选项时,每个不重复查询的查询指标(查询时间、锁定时间等)被保存到数据库。工具每次以--history选项运行的时候,比较历史的数据就会被保存,随着岁间的推移就可以用来作趋势和查询性能分析。

     pt-query-digest运行于一个个事件之上,事件即为被称作属性的键值对集合。你可以很快就识别出绝大多数的属性:Query_time、Lock_time等等。你只要查看一下慢查询日志就能看到它们。然而,也有一些并不包含在慢查询日志里,而实际上慢查询日志也可能包含不同类型的属性(例如,你可能有使用Percona patch的服务器)。

     默认的--output是一个查询分析报告。--[no]report选项控制是否打印该选项。有时候你可能想解析所有查询并阻止该报告输出,例如当使用--review或者--history选项时。

     对于查询分析的每个种类都有一段输出。一个“种类”的查询都含有相同的--group-by属性值,其值默认为fingerprint。fingerprint是经过移除常量、折叠空白符等处理后提取的查询文本版本。报告是格式化的,易于不用封装就粘贴进Email。另外所有的非查询语句行是以注释开头的,因此可以将它保存为.sql文件然后用你最喜欢的语句高亮文本编辑器打开它。开头有一个响应时间概况。

     这里描述的输出受--report-format选项控制。该选项允许你指定打印打印哪些东西以及以什么顺序打印。这里描述以默认顺序排序的默认输出。

     默认情况下,报告以一个所有执行的分析的段落开头。其信息与你将会看到的来自日志的查询的每个种类的内容十分相似,但不会包含为了保留全局分析而太昂贵的信息。报告也包含命令自身执行中的一些统计,例如CPU和内存的使用、运行时的本地日期时间以及输入文件读取和解析列表。

     接下来是事件的响应时间概况。这是之后查询报告详情中的不重复事件的高度概览。它包含以下列:

Column        Meaning
============  ==========================================================
Rank          The query's rank within the entire set of queries analyzed
Query ID      The query's fingerprint
Response time The total response time, and percentage of overall total
Calls         The number of times this query was executed
R/Call        The mean response time per execution
V/M           The Variance-to-mean ratio of response time
Item          The distilled query

     接下来是具体的查询报告,每个查询展现一段。

# Query 2: 0.01 QPS, 0.02x conc, ID 0xFDEA8D2993C9CAF3 at byte 160665

     接下来是这一种类查询的指标表。

#           pct   total    min    max     avg     95%  stddev  median
# Count       0       2
# Exec time  13   1105s   552s   554s    553s    554s      2s    553s
# Lock time   0   216us   99us  117us   108us   117us    12us   108us
# Rows sent  20   6.26M  3.13M  3.13M   3.13M   3.13M   12.73   3.13M
# Rows exam   0   6.26M  3.13M  3.13M   3.13M   3.13M   12.73   3.13M

     接下来是用户、数据库和查询时间区间统计。

# Users       1   user1
# Databases   2     db1(1), db2(1)
# Time range 2008-11-26 04:55:18 to 2008-11-27 00:15:15

     查询执行时间分布。

# Query_time distribution
#   1us
#  10us
# 100us
#   1ms
#  10ms  #####
# 100ms  ####################
#    1s  ##########
#  10s+

     优化查询语句可能用到的需要查看表信息时的相关命令。

# Tables
#    SHOW TABLE STATUS LIKE 'table1'\G
#    SHOW CREATE TABLE `table1`\G
# EXPLAIN
SELECT * FROM table1\G

     以下为个人本地环境的测试数据。

     读取慢查询日志。

root@ubuntu:~# pt-query-digest /usr/local/mysql/data/ubuntu-slow.log

# 17.6s user time, 720ms system time, 34.79M rss, 99.55M vsz
# Current date: Fri Apr 13 23:40:19 2018
# Hostname: ubuntu
# Files: /usr/local/mysql/data/ubuntu-slow.log
# Overall: 199.26k total, 39 unique, 0.25 QPS, 0.00x concurrency _________
# Time range: 2018-04-04T13:43:07 to 2018-04-13T15:30:21
# Attribute          total     min     max     avg     95%  stddev  median
# ============     ======= ======= ======= ======= ======= ======= =======
# Exec time          2115s     2us   1071s    11ms    73us      2s    38us
# Lock time           31ms       0     6ms       0       0    14us       0
# Rows sent         17.22k       0     506    0.09       0    5.40       0
# Rows examine      56.35k       0    1012    0.29       0   13.97       0
# Query size         4.00M      11     206   21.04   20.43    2.34   20.43

# Profile
# Rank Query ID           Response time   Calls  R/Call   V/M   Item
# ==== ================== =============== ====== ======== ===== ==========
#    1 0x73667642E516B563 1644.5166 77.7%      8 205.5646 53... 
#    2 0xBFD191434B609D80  429.0180 20.3%     13  33.0014  0.00 SELECT
# MISC 0xMISC               41.8789  2.0% 199236   0.0002   0.0 <37 ITEMS>

# Query 1: 0.01 QPS, 2.26x concurrency, ID 0x73667642E516B563 at byte 765
# This item is included in the report because it matches --limit.
# Scores: V/M = 536.42
# Time range: 2018-04-06T16:21:42 to 2018-04-06T16:33:49
# Attribute    pct   total     min     max     avg     95%  stddev  median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count          0       8
# Exec time     77   1645s     11s   1071s    206s   1063s    332s     94s
# Lock time      0       0       0       0       0       0       0       0
# Rows sent      0       0       0       0       0       0       0       0
# Rows examine   0       0       0       0       0       0       0       0
# Query size     0     112      14      14      14      14       0      14
# String:
# Hosts        192.168.112.1
# Users        root
# Query_time distribution
#   1us
#  10us
# 100us
#   1ms
#  10ms
# 100ms
#    1s
#  10s+  ################################################################
DO SLEEP(1111)\G

# Query 2: 0.00 QPS, 0.12x concurrency, ID 0xBFD191434B609D80 at byte 43045659
# This item is included in the report because it matches --limit.
# Scores: V/M = 0.00
# Time range: 2018-04-13T14:29:57 to 2018-04-13T15:30:21
# Attribute    pct   total     min     max     avg     95%  stddev  median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count          0      13
# Exec time     20    429s     33s     33s     33s     32s       0     32s
# Lock time      0       0       0       0       0       0       0       0
# Rows sent      0      13       1       1       1       1       0       1
# Rows examine   0       0       0       0       0       0       0       0
# Query size     0     481      37      37      37      37       0      37
# String:
# Hosts        192.168.112.1
# Users        root
# Query_time distribution
#   1us
#  10us
# 100us
#   1ms
#  10ms
# 100ms
#    1s
#  10s+  ################################################################
# EXPLAIN /*!50100 PARTITIONS*/
SELECT SLEEP(11) AS l, SLEEP(22) AS r\G

     读取tcpdump。

root@ubuntu:~# tcpdump -s 65535 -x -nn -q -tttt -i any -c 11 port 3306 | pt-query-digest --type=tcpdump
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes
Reading from STDIN ...
11 packets captured
20 packets received by filter
0 packets dropped by kernel

# 180ms user time, 40ms system time, 34.36M rss, 98.80M vsz
# Current date: Fri Apr 13 23:42:13 2018
# Hostname: ubuntu
# Files: STDIN
# Overall: 3 total, 3 unique, 2.75k QPS, 1.47x concurrency _______________
# Time range: 2018-04-13 23:42:12.999130 to 23:42:13.000219
# Attribute          total     min     max     avg     95%  stddev  median
# ============     ======= ======= ======= ======= ======= ======= =======
# Exec time            2ms   193us     1ms   533us     1ms   396us   287us
# Rows affecte           0       0       0       0       0       0       0
# Query size            63      16      30      21   28.75    6.03   16.81
# Warning coun           1       0       1    0.33    0.99    0.47       0

# Profile
# Rank Query ID           Response time Calls R/Call V/M   Item
# ==== ================== ============= ===== ====== ===== =============
#    1 0x5D51E5F01B88B79E  0.0011 69.8%     1 0.0011  0.00 ADMIN CONNECT
#    2 0x746D96521AE5B82C  0.0003 18.1%     1 0.0003  0.00 SET
#    3 0x654AA5C414A9A261  0.0002 12.1%     1 0.0002  0.00 SET

# Query 1: 0 QPS, 0x concurrency, ID 0x5D51E5F01B88B79E at byte 1192 _____
# This item is included in the report because it matches --limit.
# Scores: V/M = 0.00
# Time range: all events occurred at 2018-04-13 23:42:12.999130
# Attribute    pct   total     min     max     avg     95%  stddev  median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count         33       1
# Exec time     69     1ms     1ms     1ms     1ms     1ms       0     1ms
# Rows affecte   0       0       0       0       0       0       0       0
# Query size    47      30      30      30      30      30       0      30
# Warning coun   0       0       0       0       0       0       0       0
# String:
# Databases
# Hosts        192.168.112.1
# Users        root
# Query_time distribution
#   1us
#  10us
# 100us
#   1ms  ################################################################
#  10ms
# 100ms
#    1s
#  10s+
administrator command: Connect\G

# Query 2: 0 QPS, 0x concurrency, ID 0x746D96521AE5B82C at byte 2416 _____
# This item is included in the report because it matches --limit.
# Scores: V/M = 0.00
# Time range: all events occurred at 2018-04-13 23:42:12.999699
# Attribute    pct   total     min     max     avg     95%  stddev  median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count         33       1
# Exec time     18   290us   290us   290us   290us   290us       0   290us
# Rows affecte   0       0       0       0       0       0       0       0
# Query size    26      17      17      17      17      17       0      17
# Warning coun   0       0       0       0       0       0       0       0
# String:
# Databases
# Hosts        192.168.112.1
# Users        root
# Query_time distribution
#   1us
#  10us
# 100us  ################################################################
#   1ms
#  10ms
# 100ms
#    1s
#  10s+
SET NAMES utf8mb4\G

# Query 3: 0 QPS, 0x concurrency, ID 0x654AA5C414A9A261 at byte 2941 _____
# This item is included in the report because it matches --limit.
# Scores: V/M = 0.00
# Time range: all events occurred at 2018-04-13 23:42:13.000219
# Attribute    pct   total     min     max     avg     95%  stddev  median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count         33       1
# Exec time     12   193us   193us   193us   193us   193us       0   193us
# Rows affecte   0       0       0       0       0       0       0       0
# Query size    25      16      16      16      16      16       0      16
# Warning coun 100       1       1       1       1       1       0       1
# String:
# Databases
# Hosts        192.168.112.1
# Users        root
# Query_time distribution
#   1us
#  10us
# 100us  ################################################################
#   1ms
#  10ms
# 100ms
#    1s
#  10s+
SET PROFILING=1;\G

     不打印报告而是将结果写入库表复用。

CREATE TABLE IF NOT EXISTS `player`.`query_review` (
   checksum     BIGINT UNSIGNED NOT NULL PRIMARY KEY,
   fingerprint  TEXT NOT NULL,
   sample       TEXT NOT NULL,
   first_seen   DATETIME,
   last_seen    DATETIME,
   reviewed_by  VARCHAR(20),
   reviewed_on  DATETIME,
   comments     TEXT
);

root@ubuntu:~# pt-query-digest --review h=192.168.112.129,P=3306,u=root,p=123456,D=player,t=query_review --no-report /usr/local/mysql/data/ubuntu-slow.log

mysql> SELECT * FROM `player`.`query_review`\G
*************************** 1. row ***************************
   checksum: 8315463791669589347
fingerprint: do sleep(?)
     sample: DO SLEEP(1111)
 first_seen: 2018-04-06 16:21:42
  last_seen: 2018-04-06 16:33:49
reviewed_by: NULL
reviewed_on: NULL
   comments: NULL
*************************** 2. row ***************************
   checksum: 13821988449590222208
fingerprint: select sleep(?) as l, sleep(?) as r
     sample: SELECT SLEEP(69) AS l, SLEEP(96) AS r
 first_seen: 2018-04-13 14:29:57
  last_seen: 2018-04-13 15:41:51
reviewed_by: NULL
reviewed_on: NULL
   comments: NULL
2 rows in set (0.00 sec)



     参考:

https://www.percona.com/doc/percona-toolkit/LATEST/pt-query-digest.html

猜你喜欢

转载自blog.csdn.net/sweeper_freedoman/article/details/79931172
今日推荐