Slow Query Log Analysis: Rank by Total Time, Not by Loudest Query

Slow query log analysis is the practice of aggregating a database server’s slow query log by query fingerprint — the statement with its literals stripped away — and ranking those fingerprints by total time, so the query shape that costs the database the most gets fixed first. A raw slow log sorted by duration points at the loudest single run; the aggregate usually tells a different story. This guide covers the method, the rows-examined smell, how to switch the slow log on safely on MySQL and PostgreSQL, how the two servers’ logging differs, and a worked example — all checkable in your browser with the Slow Query Log Analyzer, which never uploads the log.

What a slow query log is

A slow query log is a file the database server writes itself: every statement whose execution time crossed a configured threshold gets an entry with its duration and, on MySQL, the lock time and the number of rows examined and sent, followed by the statement text. MySQL and MariaDB have written this log for decades (slow_query_log, long_query_time); PostgreSQL writes the equivalent when log_min_duration_statement is set, as one "duration: N ms" line per statement in the server log.

Two properties shape everything else in this guide. First, the log is threshold-bound: nothing under the threshold exists in it, so it can never show you the fast-but-frequent queries that may dominate real load. Second, the log is literal: the statements are recorded with the actual values users searched, filtered and logged in with. That makes the slow log one of the most useful files on a database server — and one of the most sensitive, a point section 4 returns to.

Aggregate thinking: total time by fingerprint

Read raw, a slow log invites the wrong fix. The top line by duration — one 75-second report run at 3 a.m. — is memorable, but a 3-second lookup that runs 400 times a day costs twenty minutes of database time daily and never makes a headline. The fix is to aggregate: strip the literals from each statement (customer_id = 1001 and customer_id = 1002 become the same shape), group identical shapes into fingerprints, and rank by total time — count × average — the method Percona’s pt-query-digest made standard and the one the Slow Query Log Analyzer applies in the browser.

Ranking by total time changes the work list. The dominant fingerprint — anything over half the log’s total — is the first fix, whether it needs an index, a rewrite or a cache. Averages alone mislead, so read them with the max and the p95: a fingerprint whose slowest run is ten times its average is a different problem (lock waits, a cold cache, one huge customer) from one that is uniformly slow. And keep the unparsed-lines count in view: lines the analyzer could not attribute are reported, not hidden, because a log you only partially read is a conclusion you only partially earned. The Slow Query Log Analyzer with the MySQL demo log loaded: 14 entries parsed, 4 unique fingerprints, 151.5 seconds total query time

Rows examined vs rows sent: the scan smell

MySQL records two counters per entry that PostgreSQL’s duration lines do not: Rows_examined, how many rows the server inspected, and Rows_sent, how many it returned to the client. Their ratio is the fastest smell test in slow-log work. A query that examines 120 rows to send 20 is doing honest work; one that examines 860,000 to send 1,000 is reading a mountain to hand back a handful — the classic signature of a missing index, an index the optimizer cannot use for that predicate, or a table that has outgrown the query written for it.

Aggregate the counters per fingerprint, not per run: a single scan can be a backup or a one-off report, but a fingerprint averaging over 10,000 examined rows per query is a standing tax on every request that touches it. The analyzer flags exactly that. Two cautions: examined counts rows the engine touched, including ones rejected by a WHERE clause it could not push into the index, so a high number accuses the access path, not the data volume; and PostgreSQL logs do not carry the counters at all, which is one reason the comparison in section 5 matters — on PostgreSQL, reach for pg_stat_statements when you need rows.

Turning the slow log on — safely

On MySQL, SET GLOBAL slow_query_log = ON and long_query_time = 1 (seconds; fractions allowed) starts logging without a restart, and log_queries_not_using_indexes can be added briefly to catch full scans that finish fast on a small table. On PostgreSQL, log_min_duration_statement = 1000 (milliseconds) in postgresql.conf plus a reload does the same. Start at 1–2 seconds (or 1000–2000 ms): the factory defaults — MySQL’s 10 seconds, PostgreSQL’s disabled — catch only catastrophes. Lower the threshold for a bounded window on a busy server rather than leaving 0 (log everything) on forever; the log itself costs disk and a little CPU.

The safety point nobody warns about: a slow log contains real user data. Literals are recorded verbatim — email addresses, names, search terms, reset tokens in a WHERE clause. Treat the file like a database dump: restrictive permissions, no pasting into hosted analyzers, no attaching to public tickets. That exposure is precisely why the Slow Query Log Analyzer runs entirely in the browser: the log is parsed on your machine and never uploaded, so the aggregate view costs you none of the data. Where policy forbids literals entirely, sanitised logging options exist on both servers, but the default assumption should be that the file is sensitive.

MySQL vs PostgreSQL slow logging, in one pass

The shapes differ. MySQL writes a multi-line block per query — a "# Time:" line, user and host, a "# Query_time:" header with duration, lock time, rows sent and rows examined, then the SQL — into a dedicated file (or table). PostgreSQL writes one line per statement into the general server log, prefixed by whatever log_line_prefix you configured, carrying the duration in milliseconds and the statement (or the execute of a prepared statement), but no row counters. Tools that read one format rarely read the other; the analyzer reads both and says which it found.

The ecosystems differ too. MySQL’s reference aggregator is Percona’s pt-query-digest, which adds percentiles, histograms and per-fingerprint examples from files or a live server. PostgreSQL’s centre of gravity is pg_stat_statements, an in-server extension that aggregates normalised statements continuously — closer to a dashboard than a log analysis — with log-based tools reading the duration lines when the extension is unavailable. Neither replaces the other: the log is what you reach for when the pain already happened and you need the evidence file.

A worked example, and checking your own log

The analyzer’s built-in MySQL demo is a synthetic slow log of 14 entries. Aggregated, it has 4 fingerprints and 151.5 seconds of total query time. The report join ranks first: 3 runs, 84 seconds — 55% of the log — because one run took 75 seconds. The events query is second at 47.5 seconds across 4 runs and is the scan smell made visible: 3,440,000 rows examined in total, 860,000 per run, for 1,000 rows sent each time. The orders lookup runs most often (6 times) but totals only 18 seconds. Raw, sorted by duration, the 75-second run still tops the list — but only the aggregate shows that the events scan, not the report, is the standing daily tax after it.

To check your own log: export or copy a bounded slice (the analyzer takes up to 20 MB), open the Slow Query Log Analyzer, and paste or drop the file — it stays on your machine. Read the fingerprint table top-down, open the worst instance of the top fingerprint, and check the findings: scan-heavy, outlier, single-slow, dominant. Then fix one shape, re-measure with a fresh log, and repeat. For a general log file — application output, web server logs — the family tool is the Log Parser; this one is deliberately only for database slow logs.

A MySQL slow log excerpt (from the demo)

# Time: 2026-11-28T09:15:01.000000Z
# User@Host: app[app] @ web-1 [10.0.0.5]  Id:    42
# Query_time: 2.500000  Lock_time: 0.000100  Rows_sent: 20  Rows_examined: 120
SELECT * FROM orders WHERE customer_id = 1001 AND status = 'paid';
# Time: 2026-11-28T09:31:44.000000Z
# User@Host: app[app] @ web-1 [10.0.0.5]  Id:    57
# Query_time: 12.500000  Lock_time: 0.000050  Rows_sent: 1000  Rows_examined: 850000
SELECT * FROM events WHERE event_type = 'click';

Sources

Frequently asked questions

What threshold should I set for the slow log?

Start at 1–2 seconds on MySQL (long_query_time) or 1000–2000 ms on PostgreSQL (log_min_duration_statement) for a bounded window — a day of real traffic, including your peak. The defaults (10 seconds, or off) only catch catastrophes. If the file explodes, your problem is bigger than the threshold; if it stays nearly empty, lower it briefly rather than concluding the database is healthy — the log cannot show queries under the threshold at all.

Will turning the slow log on slow my server down?

Slightly, and measurably less than the queries it catches. Writing the log costs disk I/O per slow statement and a little CPU; at a 1-second threshold on a healthy server that is noise. The real costs are operational: disk growth on a busy server (rotate it), and the data exposure of the literals it records. Logging every statement (threshold 0) on a production server is the configuration to avoid — that one can hurt, and it buries the signal.

Why not just sort the log by Query_time and fix the top line?

Because duration ranking answers "what was the single worst moment", and databases usually suffer from totals. In the worked example the 75-second run is real, but the events scan costs 47.5 seconds in the same window and repeats every day, while the loudest line may be a monthly report. Aggregating by fingerprint — count, total, average, p95 — is what turns a list of anecdotes into a work list. Sort by total time first; use the single worst run as evidence inside its fingerprint, not as the ranking.