Backend & Data
Database indexes: how one line makes a query 1000× faster
You've all seen it: a page that loads in 20 milliseconds one day and grinds for 8 seconds once the table grows. Often the fix is a single line — CREATE INDEX — and the speed-up is not 2× but hundreds or thousands of times. The reason is the difference between reading everything and knowing where to look.
Without an index: read every row
Ask a database for WHERE email = 'x@y.com' and, with no index, it has no choice but to check every single row to see which ones match — a "full table scan." At a thousand rows that's instant. At ten million, it's a slog that gets slower every day the table grows.
With an index: know where to look
An index is a separate, pre-sorted copy of the column, organised as a tree (a "B-tree"). Finding a value means starting at the top and, at each step, discarding half of what's left — like finding a name in a phone book by opening near the right letter, not reading cover to cover. Ten million rows takes only a couple of dozen hops. That's why the speed-up is so dramatic and only grows with the table.
No index = read the whole book to find one name. Index = the book is alphabetised, so you flip straight to it. Same answer, wildly different effort.
So why not index everything?
Indexes aren't free:
- Writes cost more. Every insert or update must also update each index, so over-indexing slows down writes.
- They take space — an index is extra data stored alongside the table.
- They only help the right queries. An index on
emailspeeds up lookups by email; it does nothing for a search onname.
The craft is indexing the columns you actually filter, join, or sort on — and no more. Find your slowest query, look at what it filters by, and index that. It's often the single highest-leverage line of code you'll write all week.