pt-query-digest

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

pt-query-digest

可以用来分析binlog、General log、slowlog,也可以通过show processlist或者通过tcpdump抓取的MySQL协议数据来进行分析


用法:


(1)直接分析slow日志:

 pt-query-digest INDEX01184W-slow.log > slow_report1.log


(2)分析最近12小时内的查询:

pt-query-digest --since=12h INDEX01184W-slow.log > slow_report2.log


(3)分析指定时间范围内的查询:

#pt-query-digest  --since '2017-02-01 09:30:00' --until '2017-03-01 10:00:00' INDEX01184W-slow.log >> slow_report3.log


(4)分析指含有select语句的慢查询

pt-query-digest --filter '$event->{fingerprint} =~ m/^select/i' INDEX01184W-slow.log> slow_report4.log


(5) 针对某个用户的慢查询

pt-query-digest --filter '($event->{user} || "") =~ m/^root/i' INDEX01184W-slow.log> slow_report5.log


(6) 查询所有所有的全表扫描或full join的慢查询

pt-query-digest --filter '(($event->{Full_scan} || "") eq "yes") ||(($event->{Full_join} || "") eq "yes")' INDEX01184W-slow.log> slow_report6.log


(7)把查询保存到query_review表(#默认会创建percona_schema库和query_review表 )

 pt-query-digest --user=root --password=147258 --review h=localhost INDEX01184W-slow.log 


(8)把查询保存到query_history表

pt-query-digest  --user=root –password=abc123 --review  h=localhost INDEX01184W-slow.log


(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.000003 > mysql-bin000003.sql

pt-query-digest  --type=binlog  mysql-bin000003.sql > slow_report10.log


(11)分析general log

pt-query-digest  --type=genlog  general.log > slow_report11.log


例:

#查询两条慢SQL:

root@localhost [(none)]>select sleep(3);

+----------+

| sleep(3) |

+----------+

|        0 |

+----------+

1 row in set (3.70 sec)

root@localhost [(none)]>select sleep(4);

+----------+

| sleep(4) |

+----------+

|        0 |

+----------+

1 row in set (4.02 sec)

root@localhost [(none)]>select sleep(8);

+----------+

| sleep(8) |

+----------+

|        0 |

+----------+

1 row in set (8.07 sec)


#查看slow日志,可以发现会记录上面两条SQL:

[root@Darren1 data]# cat slow.log

# Time: 2017-06-02T05:06:04.452125Z

# User@Host: root[root] @ localhost []  Id:  5565

# Query_time: 3.665139  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0

SET timestamp=1496379964;

select sleep(3);

# Time: 2017-06-02T05:35:42.145231Z

# User@Host: root[root] @ localhost []  Id:  6454

# Query_time: 4.013508  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0

SET timestamp=1496381742;

select sleep(4);

# Time: 2017-06-02T07:29:33.820712Z

# User@Host: root[root] @ localhost []  Id:  9867

# Query_time: 8.032160  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0

SET timestamp=1496388573;

select sleep(8);


#使用pt-query-digest分析slow日志文件:

[root@Darren1 data]# pt-query-digest slow.log

# 170ms user time, 70ms system time, 24.36M rss, 204.71M vsz

# Current date: Fri Jun  2 15:30:17 2017

# Hostname: Darren1

# Files: slow.log

# Overall: 3 total, 1 unique, 0.00 QPS, 0.00x concurrency ________________

# Time range: 2017-06-02T05:06:04 to 2017-06-02T07:29:33

# Attribute          total     min     max     avg     95%  stddev  median

# ============     ======= ======= ======= ======= ======= ======= =======

# Exec time            16s      4s      8s      5s      8s      2s      4s

# Lock time              0       0       0       0       0       0       0

# Rows sent              3       1       1       1       1       0       1

# Rows examine           0       0       0       0       0       0       0

# Query size            45      15      15      15      15       0      15


第一部分:

Overall: 总共有多少条查询,上例为总共3个查询

unique: 对SQL进行分类,总的SQL种类,上例为1种

Time range: 查询执行的时间范围

total: 总计   min:最小   max: 最大  avg:平均

95%: 把所有值从小到大排列,位置位于95%的那个数,这个数一般最具有参考价值。

median: 中位数,把所有值从小到大排列,位置位于中间那个数。

# Profile

# Rank Query ID           Response time  Calls R/Call V/M   Item

# ==== ================== ============== ===== ====== ===== ======

#    1 0xF9A57DD5A41825CA 15.7108 100.0%     3 5.2369  0.68 SELECT


第二部分:

对SQL进行分组,然后对各类查询的执行情况进行分析,结果按总执行时长,从大到小排序。

Response: 总的响应时间。

time: 该查询在本次分析中总的时间占比。

calls: 执行次数,即本次分析总共有多少条这种类型的查询语句。

R/Call: 平均每次执行的响应时间。

Item : 查询对象

# Query 1: 0.00 QPS, 0.00x concurrency, ID 0xF9A57DD5A41825CA at byte 409

# This item is included in the report because it matches --limit.

# Scores: V/M = 0.68

# Time range: 2017-06-02T05:06:04 to 2017-06-02T07:29:33

# Attribute    pct   total     min     max     avg     95%  stddev  median

# ============ === ======= ======= ======= ======= ======= ======= =======

# Count        100       3

# Exec time    100     16s      4s      8s      5s      8s      2s      4s

# Lock time      0       0       0       0       0       0       0       0

# Rows sent    100       3       1       1       1       1       0       1

# Rows examine   0       0       0       0       0       0       0       0

# Query size   100      45      15      15      15      15       0      15

# String:

# Hosts        localhost

# Users        root

# Query_time distribution

#   1us

#  10us

# 100us

#   1ms

#  10ms

# 100ms

#    1s  ################################################################

#  10s+

# EXPLAIN /*!50100 PARTITIONS*/

select sleep(8)\G


第三部分:

Databases: 库名

Users: 各个用户执行的次数(占比)

Query_time distribution : 查询时间分布图, 长短体现区间占比,本例中SQL处于1s-10s。

Tables: 查询中涉及到的表

Explain: 示例

#把分析结果记录到表中DSN

[root@Darren1 data]# pt-query-digest --user=root --password=147258 --review h=localhost slow.log 

root@localhost [percona_schema]>select * from percona_schema.query_review\G

......

*************************** 2. row ***************************

   checksum: 17988922643135866314

fingerprint: select sleep(?)

     sample: select sleep(8)

 first_seen: 2017-06-02 05:06:04

  last_seen: 2017-06-02 07:29:33

reviewed_by: NULL

reviewed_on: NULL

   comments: NULL

2 rows in set (0.00 sec)




本文转自 Darren_Chen 51CTO博客,原文链接:http://blog.51cto.com/darrenmemos/1931782,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
机器学习/深度学习 自然语言处理 算法
DL应用:query生成和query推荐
引言  在机器翻译、图片描述、语义蕴涵、语音识别和文本摘要中,序列到序列的问题已经有太多大牛研究了,也取得了很多突破。谷歌的Attention is all you need[1],舍弃并超越了主流的rnn与cnn序列建模框架,刷出了新的state of the art,这种大胆创新的精神值得我们学习。
15174 1
|
18天前
Due to limitations of the com.mongodb.BasicDocument, you can‘t add a second ‘_id‘ criteria. Query al
Due to limitations of the com.mongodb.BasicDocument, you can‘t add a second ‘_id‘ criteria. Query al
24 2
|
17天前
|
关系型数据库 MySQL
MySQL创建表出现 Specified key was too long; max key length is 767 bytes
MySQL创建表出现 Specified key was too long; max key length is 767 bytes
17 2
|
关系型数据库 MySQL
pt-table-checksum原理详解
环境 MySQL: MySQL 5.6.27 OS: centos 6.6 tool: pt-table-checksum 2.2.15 它能做什么 业界最流行的MySQL主从数据对比工具,数据一致性检测最好的的工具,没有之一 如何使用 ./pt-table-che
7327 0
|
关系型数据库 MySQL 索引
【Database】排错:Mysql5.6报错Specified key was too long; max key length is 767 bytes
在某个实验系统部署的过程中,出现mysql报错,是特定版本的处理错误,在查阅官网文档时得到解决方案
1322 0
【Database】排错:Mysql5.6报错Specified key was too long; max key length is 767 bytes
/usr/include/openssl/des.h:91:9: error: unknown type name ‘DES_LONG’ DES_LONG deslong[2];
/usr/include/openssl/des.h:91:9: error: unknown type name ‘DES_LONG’ DES_LONG deslong[2];
98 0
【TP5.1】variable type error: array
【TP5.1】variable type error: array
146 0
【TP5.1】variable type error: array
|
SQL 索引
pt-query-digest
pt-query-digest 可以用来分析binlog、General log、slowlog,也可以通过show processlist或者通过tcpdump抓取的MySQL协议数据来进行分析 用法: (1)直接分析slow日志:  pt-query-digest INDEX01184W-slow.
1246 0
|
关系型数据库 数据库 索引
pt-table-checksum
pt-table-checksum是目前可以说是最好的查看主从一致性的工具 先来个使用例子,有助快速上手使用 在主库执行:mysql>GRANT SELECT, PROCESS, SUPER, REPLICATION SLAVE,CREATE,DELETE,INSERT,UPDATE ON .
1643 0