简单介绍
pt-query-digest analyzes MySQL queries from slow, general, and binary log files.
It can also analyze queries from C and MySQL protocol data
from tcpdump. By default, queries are grouped by fingerprint and reported in
descending order of query time (i.e. the slowest queries first). If no C
are given, the tool reads C. The optional C is used for certain
options like L<”–since”> and L<”–until”>.
我们在数据库的日常运维当中需要经常查看mysql的慢日志来方便我们连接数据库的运行情况,这里介绍一个分析慢查询日志的工具,percona-toolkit工具中的pt-query-digest,它不光可以分析slow
log还可以分析binlog,general
log等可以将分析结果输出指定文件中,分析过程是先对查询语句的条件参数化,然后对参数化以后的查询进行分组统计,统计出各种信息,方便我们来分析优化
参数说明
--filter 对输入的慢查询按指定的字符串进行匹配过滤后再进行分析
--history 将分析结果保存到表中,分析结果比较详细,下次再使用--history时,如果存在相同的语句,且查询所在的时间区间和历史表中的不同,则会记录到数据表中,可以通过查询同一CHECKSUM来比较某类型查询的历史变化
--limit 限制输出结果百分比或数量,默认值是20,即将最慢的20条语句输出,如果是95%则按总响应时间占比从大到小排序。
--output 分析结果输出类型,值可以是report(标准分析报告)、slowlog(Mysql slow log)、json、json-anon,一般使用report,以便于阅读
--since 从该指定日期开始分析。
--until 截止时间,配合—since可以分析一段时间内的慢查询。
使用实践
[root@koby logs]# pt-query-digest --report slow.log >slow_report.log
[root@koby logs]# cat slow_report.log
# Hostname: koby
# Files: slow.log
# Overall: 42 total, 18 unique, 0.00 QPS, 0.00x concurrency ______________
# Time range: 2017-08-14T07:08:20 to 2017-10-31T08:37:14
# Attribute total min max avg 95% stddev median
# ============ ======= ======= ======= ======= ======= ======= =======
# Exec time 1370s 1s 692s 33s 49s 105s 6s
# Lock time 3ms 21us 170us 67us 108us 33us 66us
# Rows sent 21.68M 0 3.22M 528.55k 419.40k 626.74k 419.40k
# Rows examine 24.54M 280 1.38M 598.29k 1.32M 488.71k 419.40k
# Query size 10.39k 22 1.05k 253.40 652.75 260.22 166.51
# Profile
# Rank Query ID Response time Calls R/Call V/M Item
# ==== ================== ============== ===== ======== ===== ============
# 1 0xB1811D190527FB1F 718.2060 52.4% 6 119.7010 53... SELECT promotion_ticket
# 4 0xA31F41C9E0EBEF9E 101.6377 7.4% 1 101.6377 0.00 SELECT crm_servicer_zone
# 6 0x270FD742CA1B070A 44.5138 3.2% 2 22.2569 31.13 SELECT m_town
# Hostname: koby
# Files: slow.log
# Overall: 42 total, 18 unique, 0.00 QPS, 0.00x concurrency ______________
# Time range: 2017-08-14T07:08:20 to 2017-10-31T08:37:14
# Attribute total min max avg 95% stddev median
# ============ ======= ======= ======= ======= ======= ======= =======
# Exec time 1370s 1s 692s 33s 49s 105s 6s
# Lock time 3ms 21us 170us 67us 108us 33us 66us
# Rows sent 21.68M 0 3.22M 528.55k 419.40k 626.74k 419.40k
# Rows examine 24.54M 280 1.38M 598.29k 1.32M 488.71k 419.40k
# Query size 10.39k 22 1.05k 253.40 652.75 260.22 166.51
这是输出的第一部分(Overall)
Overall :代表一共有多少个查询
unique :唯一查询量,即对查询条件进行参数化以后,总共有多少不同的查询
Attribute total min max avg 95% stddev median
属性 总计 最小 最大 平均 95% 标准 中等
Exec time :语句执行时间
Lock time :锁占用时间
Rows sent :发送到客户端的行数
Rows examine select : 语句扫描行数
Query size :查询的字符串