The Report That Froze
Chapter 1 — “Forty Seconds”
Morning at Vault-9 starts with the life-support report: a summary for each deck goes to the commandant at six sharp. Today, it didn't.
You open the log. A query that had completed almost instantly for months took forty seconds. The station hasn't changed. The query hasn't changed. The archive has: the life-support log gained another quarter million readings, and the old queries started to crawl.
Every cadet's first instinct is, “It's slow, so it needs an index.” Stop there. Adding an index blindly is like prescribing medicine without a diagnosis: sometimes it helps, sometimes it does nothing, and sometimes it makes things worse.
QUERY: Forty seconds isn't a failure. It's a symptom. Come downstairs — I'll show you where the station decides which route your query will take.
QUERY opens a hatch you thought was just decoration. Beneath the residential decks is the engine room, warm and humming, with neat rows of machinery. This is where every query you write becomes a route through the data. Today, you'll see that route for yourself for the first time.

What happens to a query after you press Enter
SQL is a declarative language: you describe what you want to get without telling the database how to get it. The database decides the “how” in three steps:
- The reads the query text and turns it into a structure: which tables, which conditions, which columns.
- The planner considers possible routes — scan the whole table or use an index, what order to tables in, which algorithms to use — and picks the cheapest one according to its cost model.
- The executor follows the chosen route and produces the rows.
The same question can be answered through dozens of different routes, and the difference can be orders of magnitude rather than a few percent: the same result might take milliseconds or minutes.
The planner is the machine we came down here to see. It can't see your intentions — only the query, the schema, and its own maps of the data. This entire course is about understanding its decisions and learning how to influence them.
QUERY: I call it the navigator. The navigator isn't “stupid” — sometimes its maps are outdated, and sometimes its orders are hard to read. Both can be fixed.
Feel those forty seconds
Here's a smaller version of that same report: for every sensor on the first five decks, count how many readings it has sent over its entire history. The ls_readings log contains one hundred eighty thousand rows, and for EACH of the two hundred sensors, the query scans the entire log again from the beginning.
Run it and keep your eyes on the timer:
The blueprint before the trip
The timer tells you “this is bad,” but it doesn't tell you why. To see the cause, the database gives us EXPLAIN: show the route the navigator chose without running the query.
EXPLAIN <your query>;
The result is a plan — a tree of operations, the blueprint for that route. The command is pronounced “explain,” and it's worth remembering right away: this is how we see the navigator's decision. If you've taken the beginner course, you've already seen a blueprint like this in the Vault; if not, that's fine — we'll build it up from scratch here.
Open the blueprint for the report that froze:
EXPLAIN (pronounced “explain”) is a command that asks the database to explain HOW it plans to execute a query. You put it before a regular query; the query itself is not executed, so the command is safe and returns immediately even for a heavy report. The result is a plan — a list of steps the machine would take: what to read, how to it, what to sort.
What you just saw
There are two things to take away from this blueprint today:
Seq Scan on ls_readings— a sequential scan of the log: all one hundred eighty thousand rows, one by one. That isn't necessarily a problem — sometimes a full scan really is the cheapest option.SubPlan— a that the executor runs again for every row of the outer query. A full scan × two hundred sensors gives us the arithmetic behind the report that froze.
Notice what we did NOT do: we didn't create a single index. We don't know yet whether an index is what we need — perhaps the query should be rewritten, or perhaps we need both. Diagnose the problem from the blueprint, not from the symptom.
QUERY: Remember today's order of operations. Blueprint first. Conclusions second. Cadets who do it the other way around fill the archive with indexes like weeds — and the reports still crawl.
In the next lesson, we'll break down the numbers on the blueprint: what cost means, why those numbers are parrots rather than milliseconds, and how to read a tree that executes from the bottom up.
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;