Chapter Eight · Speed

Making It Fast — Indexes

By now the Marquee's data is shared, structured, and trustworthy — and, at forty thousand bookings, slow. This chapter shows why queries degrade as tables grow, then introduces the index: a sorted structure that turns forty thousand checks into a few hops, without changing a single query. It ends with the honest part — what every index costs on every write, and how to ask the engine itself which route it chose.

4 topics

Seven chapters in, the Marquee's database does everything the spreadsheet couldn't: one copy of the truth, rules enforced on every write, double-bookings impossible. Then the cinema grows, the bookings table reaches forty thousand rows, and a page that was instant last month takes four seconds. Nothing is wrong. Every answer is still correct. The database is simply doing what it was asked in the most patient way imaginable: checking every single row, because nothing told it a better way exists.

This chapter is about that better way. First the mechanism of slowness itself, so the cure makes sense — the full scan, and why growth alone degrades it. Then the index: what it physically is, why the query doesn't change, and the phone-book rule that predicts when it helps. Then the bill, because indexes are paid for by every write. And finally a first conversation with the query planner, the part of the engine that has been quietly choosing routes all along.

Find "Night Bus" in a phone book of 10,000 entries — two ways to search
Flip every page
The full scan · check every entry, one by one · about 10,000 checks · doubles when the book doubles
Thumb-cut to N
The index · the alphabetical order does the work · about 3 hops · barely notices when the book doubles

Topics in This Chapter