Forty Seconds: Your First EXPLAIN ANALYZE

The Detective’s Workflow

12 min

The last watch of Chapter One

Six hundred hours, station time. A few watches ago, at this very hour, the morning report hung—and QUERY first opened the hatch to the lower decks for you. Since then, you’ve learned the language of plans: cost and rows, actual time and loops, pages and Rows Removed, estimates versus reality.

There are no new tools today. Today, you’ll learn the order in which an engineer uses the ones you already have.

QUERY: Any cadet poking around at random will find something suspicious in a plan by the fifth try. An engineer always searches in the same order—and that’s why they find it on the first.

A workbench under a lamp, arranged like an investigator’s desk: a rolled-up chalk plan, a brass stopwatch, and a page-slab under a magnifying glass; only at the very end of the row hangs an untouched wrench. Beside it is a box of unused brass index-keys. The cadet stands with their hands behind their back, the robot cat sits directly in front of the wrench and blocks it, and a parrot perches on the hook above.
First the plan, then the stopwatch, then the page map. The wrench comes last: adding an index blindly is treatment without a diagnosis.

The detective’s five steps

When a query hangs, an engineer doesn’t just “look at the plan.” They work through it in the same order every time.

Step 1. Get the complete plan: EXPLAIN (ANALYZE, BUFFERS). One run gives you all the evidence: the route, actual time, rows, and pages. This is safe for SELECT statements (and you remember from the Vault that ANALYZE on really executes the statement).

Step 2. Find the node where the time is going. Don’t pick the lowest node or the one that looks scariest. Find the node with the greatest self time: multiply its actual time by loops—the technique from the timing lesson—and subtract the time spent in its children, because a parent’s time includes their time. Usually, nine-tenths of the whole run is spent in just one or two nodes.

Step 3. Compare the estimate with reality—est versus actual—at that node. Est is the same rows from the first set of parentheses, the navigator’s forecast; actual is what really happened in the second. If they differ by an order of magnitude, the planner is flying blind: the route was built from bad maps, so you need to fix the maps rather than the road. If the estimate was accurate, the navigator saw the situation clearly and still chose this route. Its model found nothing cheaper, so you need to examine the road itself.

Step 4. Check what was read for nothing: Rows Removed and Buffers. How many rows did the node discard after reading them, and how many pages did it pull in for the handful it actually needed? This turns the complaint “it’s slow” into concrete evidence.

Step 5. Only now think about the fix. An index, a rewritten query, updated maps: choose the treatment based on the diagnosis from steps 2–4. Notice where this step appears: last. Any fix before this point is a guess.

How to read a plan: complete plan → node where the time is going → est versus actual → Rows Removed and Buffers → only then the fix

Case No. 1: the report that hung

Apply the workflow to the case that opened this chapter. In the first lesson, you looked at this query with a plain —the plan without the run—and found a suspicious SubPlan in the tree that scanned the entire readings log. That was a suspicion. Now collect the evidence.

Step one is to get the complete plan. Run it, and don’t be surprised if the cell takes longer than usual: this time the query really executes, including all two hundred passes over the log.

EXPLAIN (ANALYZE, BUFFERS)
SELECT
  s.sensor_id,
  s.deck,
  (SELECT COUNT(*)
   FROM ls_readings r
   WHERE r.sensor_id = s.sensor_id) AS readings_total
FROM ls_sensors s
WHERE s.deck IN ('A', 'B', 'C', 'D', 'E')
ORDER BY s.sensor_id;

Steps 2–5: the diagnosis

Step 2—where the time goes. At the top is an Index Scan on the sensors’ PRIMARY KEY (this lets the navigator return rows directly in ORDER BY order, so it doesn’t even need a Sort node). Its time is huge, but almost all of it comes from its children. Now look at Seq Scan on ls_readings inside the SubPlan: a few milliseconds per run seems trivial until you multiply it by loops=200. Those two hundred runs consume almost the entire query. Its self time is the highest in the tree. You’ve found the node.

Step 3—est versus actual. At the hot node, the estimate is rows=450; the actual value is rows=539 on average per run. That isn’t even a factor of one and a half, let alone an order of magnitude. The planner isn’t flying blind: the maps are current, and the route was chosen accurately from them. Fixing the statistics would be pointless because the route itself is expensive.

Step 4—what was read for nothing. Rows Removed by Filter: 179461 means that every run discards one hundred seventy-nine thousand rows from the log just to keep about 539. And Buffers: shared hit=302600 is the same batch of 1,513 pages you counted in the BUFFERS lesson, read two hundred times in a row. (You’ll also see JIT lines near the bottom of the plan. On expensive routes, the machine compiles parts of the plan on the fly; they don’t matter for our diagnosis.)

Step 5—the fix? The diagnosis is complete: the query rereads the entire log two hundred times because the is correlated, and the machine has no way to read just one sensor’s readings directly. There are at least two treatments: rewrite the query so the log is read once, or give the navigator a short route to one sensor’s readings. You’ll learn both in the next chapters. Today, you still haven’t created a single index, and that is the right choice. For the first time, instead of a complaint that “it’s slow,” you have a case backed by evidence.

At the hatch

You and QUERY climb back up from the lower decks. The machine’s hum behind you sounds different now—not noise, but a language you’re starting to understand.

QUERY: Good watch. You read the plan the way an engineer does: step by step, without guessing. You know, I once found a strange index in Kotomarket’s plan logs that… Never mind. Old tab. Let’s say I was dozing.

He trails off mid-sentence and changes the subject far too quickly; the tail of his process freezes for a moment. You don’t ask. Not yet.

The report still isn’t fixed, and that’s the right outcome: a diagnosis without treatment is better than treatment without a diagnosis. The next chapter begins the craft itself: what routes through data are available and why the navigator chooses one over another.

Check yourself
A query hangs. In what order does an engineer proceed?