Skip to content

Troubleshooting guide

MySQL slow query log: how to enable and read it

The slow query log records statements above a time threshold. Summarised properly, it shows which few query shapes consume most of your database.

3 min read11 sectionsUpdated

On this page

Key points

  • It can be enabled with no restart.
  • The default long_query_time of 10 seconds is far too high.
  • Aggregate by query shape, do not read it line by line.
  • Rows_examined versus Rows_sent shows waste.

What it records

Each entry has the query, its time, lock time, rows examined and rows sent. Used alone it shows only the slow extremes. Many costly workloads consist of fast queries run enormously often, so lower the threshold for a while to catch those too.

What it usually reveals

  • Full table scans from a missing index.
  • Large OFFSET pagination, where the server reads and discards rows.
  • Sorting on disk because no index supports ORDER BY.
  • Implicit type conversions, such as comparing a string column to a number, which disable the index.

Enable and summarise

Turn it on at runtime, leave it for a representative period, and aggregate.

bash
-- Enable without restarting
SET GLOBAL slow_query_log = 1;
SET GLOBAL long_query_time = 0.2;
SHOW VARIABLES LIKE 'slow_query_log_file';

# Summarise (shell)
mysqldumpslow -s t -t 10 /var/lib/mysql/host-slow.log
pt-query-digest /var/lib/mysql/host-slow.log | head -60

Act on it

Take the top query shape, then run EXPLAIN. Look for type: ALL, and a very high rows estimate. Add a composite index that matches your WHERE columns, followed by the ORDER BY columns. Replace deep OFFSET with keyset pagination. Fix type mismatches. Then re-measure, because the second-worst query often becomes worth fixing next.

Keep the overhead reasonable

With a very low threshold on a busy system the log can grow quickly, and writing it costs a little I/O. Enable a low threshold for an hour during peak, then raise it again. Use rotation for the file. On managed services such as RDS, the log can go to a table or be exported to CloudWatch, and the performance schema offers a low-overhead alternative: sys.statement_analysis gives aggregated figures for every statement shape without any log file at all, which is often enough for routine review.

Catch it automatically

Log files show you what already happened. APM attaches query time to the request that triggered it, so you can see which endpoint and which release introduced a slow query, and MySQL monitoring charts the server's slow query rate.

Frequently asked questions

What should long_query_time be?

Between 100 ms and 1 second is typical. Use a lower value briefly when investigating.

Does it slow the server down?

Slightly. It is usually acceptable, particularly with a sensible threshold.

Does a slow query always need an index?

No. It may be waiting on locks. Compare Lock_time against Query_time.

Check it now, free

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.