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
-- 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 -60Act 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.
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
- Response time checker: Measure TTFB with DNS, connect, TLS and server time.
Related reading
- What is Total Blocking Time (TBT)?: Total Blocking Time explained: long tasks, how TBT is calculated, the 200 ms target in Lighthouse, how it relates to INP and how to break up main-thread work.
- DNS propagation: how long it takes and how to check it: How DNS propagation really works: why TTL decides the wait, how nameserver changes differ, how to check resolvers with dig and how to migrate with no downtime.
- How to reduce TTFB (server response time): Practical ways to reduce time to first byte: edge caching, fewer redirects, faster queries, warm runtimes and closer servers, in order of impact.
- How to set a log retention policy: Decide how long to keep logs, traces and metrics: debugging needs, security and compliance, cost, tiered retention and a sample policy to adapt.
- php-fpm "server reached pm.max_children": how to fix it: Fix php-fpm server reached pm.max_children: calculate the right worker count from memory, find slow scripts with the slow log and choose between pm modes.
- How to reduce Total Blocking Time (TBT): Reduce Total Blocking Time: find long tasks in the Performance panel, split JavaScript, yield to the main thread and defer third-party scripts until needed.
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.