Lesson 11 of 11 · 28 min

Troubleshooting slow queries

By the end of this lesson you can work through a slow query methodically: measure it, find the expensive node, compare estimates with reality, identify the cause (missing index, stale statistics, non-sargable filter, row multiplication, unnecessary sort) and verify that the fix keeps the result unchanged.

Slow queries have a small number of causes, and EXPLAIN ANALYZE shows which one you're dealing with. Work through the steps in order, and change one thing at a time.

The checklist

  1. Reproduce and measure. Get the exact query and parameters. Run EXPLAIN (ANALYZE, BUFFERS) a few times; note the plan and a typical time. (In this course: compare on the same device only.)
  2. Find the expensive node. Look for the node whose actual time (× loops) accounts for most of the total, or the node that reads the most rows or buffers. Everything else is noise.
  3. Compare estimated and actual rows, bottom-up. A large gap points to statistics: stale (run ANALYZE), skewed values, correlated columns, or expressions without statistics.
  4. Check the scans. A Seq Scan with a large Rows Removed by Filter and a small output is an index candidate. Is there an index? Is the condition sargable? Is the value selective?
  5. Check the joins. A Nested Loop with a high loops count over a Seq Scan means a missing index on the inner join column, or a bad estimate. A Hash with Batches > 1 means too little memory.
  6. Check sorts and limits. A Sort feeding a Limit can often be replaced by an index in the right order. Sort Method: external merge means it spilled to disk.
  7. Check row counts against the business question. If a join produces many more rows than the report has facts, rows are being multiplied, and the result is probably wrong as well as slow.
  8. Fix one thing, re-measure, and verify the result is unchanged. Keep the before/after plans as evidence.
Included with a Formula Quest membership

This lesson, its 4 graded exercises and every other Members lesson in SQL Quest come with a Formula Quest membership that includes SQL (Single Quest with SQL selected, Quest Trio including SQL, or Quest Unlimited), or with SQL access from your organization.

Free to everyone: all of SQL Foundations and Retrieving and Filtering Data, the first lesson of every track, the playground, the dataset library and the SQL reference. See free lessons