Skip to content

Reading the MySQL slow query log to find what is actually slow

The MySQL slow query log shows you what crossed a latency threshold once. The real cost is the query that runs ten thousand times a minute and never trips it.

A dashboard goes red and someone tails the slow query log. The first ten lines are a 12-second analytics export and a couple of expensive joins from a back-office report. None of those are what the customer is waiting on. The query holding up checkout is a 180ms SELECT that runs forty times per page load — fast enough to never appear in the log, slow enough in aggregate to dominate the workload. Reading the slow log raw gives you the wrong target almost every time.

What’s happening

slow_query_log records any statement whose execution time exceeds long_query_time, default 10 seconds. At that threshold you catch reporting queries and runaway joins, but you miss the steady-state load. Lowering long_query_time to 0.5 or 0.2 floods the log, and now you have a 4 GB file nobody can read. The point of the slow log is not to read it — it is to feed it to an aggregator that groups by query fingerprint and ranks by total time, not by single-execution time. log_queries_not_using_indexes sounds useful and is usually misleading: a SELECT against a 12-row lookup table doesn’t need an index, and the log will flag it every call.

How to verify

Enable the log with a sensible threshold and let it run for a representative window — a full business hour, not five minutes at 11pm:

SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 0.5;
SET GLOBAL slow_query_log_file = '/var/log/mysql/slow.log';
SET GLOBAL log_queries_not_using_indexes = 'OFF';

Then aggregate. mysqldumpslow ships with MySQL and groups by query pattern, but it sorts by single-execution time:

mysqldumpslow -s t -t 20 /var/log/mysql/slow.log

pt-query-digest from Percona Toolkit is what you actually want. It ranks by total time consumed across all calls:

pt-query-digest /var/log/mysql/slow.log | less

The top entry by Response time percentage is the query stealing the most database wall-clock time, regardless of how fast any single execution runs. To tell CPU-bound from IO-bound, look at Rows_examined vs Rows_sent: a query examining millions of rows to return ten is doing IO it shouldn’t and is a candidate for an index. A query examining what it sends but appearing hot is usually a missing cache layer or a tight loop in the application.

The fix

The first three entries from pt-query-digest are where to spend your week. Run EXPLAIN ANALYZE against production-shaped data and decide: does it need an index, a rewrite, a covering index that skips the table read, or a result that should live in Redis instead of being recomputed forty times per request. Resist adding indexes preemptively — every index is a write cost on every INSERT and UPDATE against that table.

If your MySQL workload has never been profiled this way, Stack Harbor’s Monitoring & Support team will instrument it and walk you through the top offenders — book a session.