Using pt-query-digest to Analyze MySQL Slow Query Logs
This article introduces pt-query-digest from Percona Toolkit, explains how to install the toolkit, configure MySQL slow‑query logging, run pt-query-digest on the slow log, interpret its detailed output, and locate specific SQL statements for performance tuning.
Author Zhang Wei, a DBA at iKang Beijing, shares his experience with MySQL daily issue handling and DMP product maintenance, focusing on the use of Percona Toolkit's pt-query-digest for slow‑query analysis.
Installation of Percona Toolkit
Download the latest RPM package:
wget https://www.percona.com/downloads/percona-toolkit/3.2.1/binary/redhat/7/x86_64/percona-toolkit-3.2.1-1.el7.x86_64.rpmInstall required Perl modules:
yum install perl-DBI.x86_64
yum install perl-DBD-MySQL.x86_64
yum install perl-IO-Socket-SSL.noarch
yum install perl-Digest-MD5.x86_64
yum install perl-TermReadKey.x86_64Install the toolkit:
rpm -iv percona-toolkit-3.2.1-1.el7.x86_64.rpm
rpm -qa | grep perconaThe binaries are placed in /usr/bin .
Check MySQL slow‑log configuration
mysql> show variables like '%slow%';
+---------------------------+-------------------------------------+
| Variable_name | Value |
+---------------------------+-------------------------------------+
| log_slow_admin_statements | ON |
| log_slow_slave_statements | ON |
| slow_launch_time | 2 |
| slow_query_log | ON |
| slow_query_log_file | /opt/mysql/data/7777/mysql-slow.log |
+---------------------------+-------------------------------------+Adjust the query‑time threshold for testing:
set global long_query_time=0.001000;Run pt-query-digest on the slow log
pt-query-digest mysql-slow.logThe tool outputs overall statistics (total queries, unique queries, QPS, concurrency), time range, and attribute summaries such as execution time, lock time, rows sent/examined, and query size.
Interpretation of the output
The report is divided into three parts:
Overall information with resource usage and query distribution.
Profile section showing the top queries by response time, calls, and V/M score.
Detailed per‑query sections listing counts, execution/lock times, rows, query size, database, host, user, and time‑distribution histograms.
Example of a detailed query block:
# Query 1: 1.02k QPS, 10.94x concurrency, ID 0xFFFCA4D67EA0A788813031B8BBC3B329 at byte 26111916
# Exec time 90 328s 1ms 129ms 11ms 23ms 8ms 9ms
# String: COMMITTo locate the original SQL in the slow‑log file, use the byte offset provided:
tail -c +26111916 ./mysql-slow.log | headIn production, the identified slow statements can be optimized based on the detailed metrics.
References
Percona Toolkit pt-query-digest documentation
MySQL Slow Query Log documentation
Aikesheng Open Source Community
The Aikesheng Open Source Community provides stable, enterprise‑grade MySQL open‑source tools and services, releases a premium open‑source component each year (1024), and continuously operates and maintains them.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.