How It Finds Things So Fast
You hand a database a query and the answer comes back in a blink — even across millions of rows. How? It doesn't read every row to find what you asked for. It keeps a separate, sorted helper list of where things are — an index — and jumps straight there. That's the trick that keeps it fast even when the table is enormous.
Think of finding one topic in a thick book. You could read all four hundred pages until you hit it. Or you flip to the index at the back, find the word, and it says "page 213." You jump straight there. A database index is exactly that — a back-of-the-book guide to where each value lives.
Why reading every row won't do
A small table is fine to scan top to bottom; there's nothing to it. But picture a table of five million customers and a query asking for the ones in Boston. Checking every single row, one by one, is a mountain of looking — and if a hundred people ask at once, speed collapses right when you need it most.
So instead of walking the whole table, the database consults a shortcut it prepared in advance. The query stops being a long stroll past every row and becomes a quick jump to the right ones.
An index is that shortcut: a separate, sorted list the database builds and quietly keeps up to date for you. Say you often search customers by city. The database can hold a list of cities, sorted alphabetically, each one paired with a pointer to its rows. When you ask for Boston, it goes to "B," finds Boston, and follows the pointers straight to those exact rows.
Because the list is sorted, finding a value in it is lightning quick — the way you find a word in a dictionary without reading every page. You open near the middle, see you've gone too far or not far enough, and close in fast. The database narrows the same way, landing on the answer in a handful of hops instead of millions of steps.
A small price, well worth paying
Nothing is free. An index takes a little extra space, and it has to stay accurate — every time you add or change a row, the database also nudges the index to match. That's a touch of work on the way in.
But it buys enormous speed on the way out, and we read data far more often than we change it. So the trade is almost always a bargain: pay a little when writing, save a fortune when asking. That's why people pick which columns to index with care — you index the facts you search by most, like city or order number, not every column under the sun.
This also explains a thing you may have wondered about: how a search bar finds your order among millions in an eyeblink. It isn't reading them all. It's flipping to the index, finding your number, and jumping straight there — the back-of-the-book trick, working for you.
Your turn
Open a book that has an index and find one topic using it. Then imagine finding that same topic without it — page by page, front to back. Feel the gap in effort. That gap, multiplied by millions of rows, is what a database index quietly saves you on every fast answer you've ever gotten.
Next, in our final stop, we'll zoom out and put the whole picture together. 🔦
Stuck or curious?
Ask Pip about this lesson — tap the porthole bottom-right.