Skip to content

pt-query-digest — reading the slow query report

How to use pt-query-digest end-to-end — collect slow log or tcpdump, generate the digest, read each section, and turn the top offenders into actionable index or rewrite work.

pt-query-digest is the standard tool for turning MySQL slow query logs (or live tcpdump captures) into a ranked report of the queries that cost the most. This article is the operator-grade workflow we follow: capture the right data, generate the digest, read what each section is telling you, and translate the top offenders into concrete fixes — an index, a rewrite, a cache, or a query rule.

How to verify

pt-query-digest is one binary that reads multiple input formats. Confirm it is installed and the slow log is enabled before doing anything else.

pt-query-digest --version
mysql -e "SHOW VARIABLES LIKE 'slow_query_log%';"
mysql -e "SHOW VARIABLES LIKE 'long_query_time';"
mysql -e "SHOW VARIABLES LIKE 'log_slow_admin_statements';"
mysql -e "SHOW VARIABLES LIKE 'log_queries_not_using_indexes';"
ls -l /var/log/mysql/mysql-slow.log

If slow_query_log=OFF, you have no data. Enable it for a sampling window (10-30 minutes is usually enough), capture the log, then disable to avoid disk pressure.

What’s happening

MySQL writes one entry to the slow query log for every query whose execution time exceeds long_query_time seconds, plus optionally queries that did not use an index. Each entry carries the time, the user, the database, the lock time, rows examined, rows sent, and the query text.

pt-query-digest parses the log, groups queries by fingerprint (the normalized form of the query — literals replaced by placeholders, whitespace canonicalized), and aggregates per fingerprint:

  • Total time spent on this fingerprint across all executions
  • Number of executions
  • 95th-percentile latency
  • Total rows examined
  • Total rows sent
  • A representative sample query

The report opens with the global summary, then ranks fingerprints by total time descending. The top fingerprints by total time are the ones to fix — a query that runs 1 ms but executes a million times can dominate the report ahead of a 30-second query that runs hourly.

For a server where the slow log is too disruptive to enable at long_query_time=0, pt-query-digest --type=tcpdump reads a tcpdump capture of the MySQL traffic and produces the same report without touching the server’s config.

The procedure

  1. Enable the slow query log for a sampling window. Lower long_query_time to 0 to capture everything; set it back to its previous value when done.

    SET GLOBAL long_query_time = 0;
    SET GLOBAL slow_query_log = ON;
    SET GLOBAL log_queries_not_using_indexes = ON;
    -- wait 15-30 minutes
    SET GLOBAL slow_query_log = OFF;
    SET GLOBAL long_query_time = 0.5;
  2. Generate the digest report.

    sudo pt-query-digest /var/log/mysql/mysql-slow.log > /tmp/digest.txt
    less /tmp/digest.txt
  3. Read the global summary at the top. Key numbers:

    • Time range — when the sample was collected.
    • Overall stats — total queries, unique queries, total execution time, time spent on the slowest 1%.
    • Profile — the top N fingerprints with their share of total time.
  4. Read the profile table. The columns are:

    • Rank — position in the report.
    • Query ID — fingerprint hash (you can refer to this elsewhere).
    • Response time — total ms and share of total.
    • Calls — execution count.
    • R/Call — mean latency.
    • V/M — variance-to-mean ratio (high values mean inconsistent latency).
    • Item — the digest text.
  5. Drill into the top fingerprint. The detailed section shows the sample query, its histograms, the tables it touches, and the explain plan.

    # Query 1: 12 QPS, 8.2s concurrency, ID 0xABCDEF1234567890
    # Time range: 2026-06-02 10:00:00 to 10:30:00
    # Attribute    pct   total     min     max     avg     95%
    # ============ === ======= ======= ======= ======= =======
    # Count        18      21600
    # Exec time    78    9000s    50ms    2.1s    417ms   850ms
    # Rows examine 65    1.2G    1.0k   100k    55k     90k
    # Rows sent    12   140k     5      120     6.5     20

    This fingerprint runs 12 times per second, takes 9000 seconds of total CPU over the window, examines on average 55000 rows per call but only sends 6.5 — classic missing-index symptom.

  6. Run EXPLAIN ANALYZE on the sample query. The digest gives you the query; the explain plan tells you what the optimizer is doing.

  7. Translate into a fix. For the example above, an index that turns 55000 rows examined into 7 would close the loop. Apply, sample again, confirm the fingerprint is no longer in the top of the report.

  8. For repeated runs, feed the digest to a stable store. --review saves digest results to a MySQL table; the next run shows you which fingerprints are new and which already had review notes attached.

    pt-query-digest \
      --review h=localhost,D=percona,t=query_review \
      --review-history h=localhost,D=percona,t=query_review_history \
      /var/log/mysql/mysql-slow.log

Common pitfalls

  • long_query_time=0 left on in production — the slow log captures every query and the file grows by gigabytes per minute. Always reset to a sensible value (0.5s or 1s) when the sampling window ends.
  • Slow log on a Galera cluster donor — the donor’s slow log fills while it streams an SST; the file can outgrow the disk in an hour. Disable the slow log before initiating SST.
  • Treating mean latency as the metric — a query with 1ms mean and 950ms 95th-percentile is in the tail, and the tail is where users notice. Sort by V/M to find inconsistent queries; sort by 95th percentile to find tail offenders.
  • Forgetting the lock time — pt-query-digest reports lock time separately from execution time. A query with low exec but high lock time is being blocked, not slow itself.
  • Misreading rows examined vs rows sent — high examined-to-sent ratio is the classic missing-index signature. A query that examines a million rows to return one is almost always fixable with an index.

pt-query-digest is the tool that closes the gap between “the database feels slow” and “this specific query, with this fingerprint, is the cause.” In the engagements we run, the digest is part of the weekly database review — captured against a representative window, ranked, and the top fingerprints get explain plans, index proposals, or query rewrites in a tracked queue. For the broader pattern of operating MySQL with this discipline, see /en/knowledge-base/mysql-percona-toolkit-essentials/.