I’ve become increasingly fascinated by the technicalities of computation. What is actually happening behind the curtain when I do something I’ve done thousands of times over the years?
Maybe this becomes a little series. Maybe it doesn’t. For now, I’m putting my thoughts down because I find it interesting.
What is so interesting, I hear you ask? Database indexing! Riveting stuff, I know.
It wasn’t something I’d particularly sought out to understand in the busyness of simply executing queries. But stick with me. The idea is surprisingly approachable.
Let’s say we have a table of 15 Apple Store managers and the locations they manage. Finding Mohsin by checking every row is hardly a demanding task.
Now imagine a million rows. Clearly, we’ve moved beyond Apple Store managers, but you get the idea. Searching the whole table to find one person starts to look wasteful.
For this example, we create a separate index containing the names in sorted order, alongside a way to locate their original records. We haven’t rearranged the table itself.
“But that list is just as long!”
Yes. This is where the tree comes in.
Following the signposts
B-trees are balanced search trees. Each branching point can lead to many destinations, rather than just two. A common database variation is the B+ tree, where the upper levels act as signposts and the bottom level, the leaves, holds the entries we’re looking for.
Imagine our sorted names occupy three blocks, called pages. A page above them contains two signposts: H and P.
In our simplified example, names before H go left, names from H up to P go into the middle, and names from P onwards go right.
We’re looking for Mohsin. M falls between H and P, so the database follows the middle pointer. It searches that page for the matching entry, then uses its row locator to retrieve the store location from the table.
That locator isn’t necessarily a spreadsheet-style row number. Its form depends on the database.
The useful idea is that we’ve avoided visiting the other pages entirely. Microsoft’s index guide explains this structure in more detail.
What happens when it gets bigger?
Eventually, there are too many leaf pages for one page of signposts.
So we add another level. The root points to intermediate pages, which point to the leaves. Each step narrows the search.
Suppose each branching page points to 200 pages, and each leaf holds 200 entries. Three levels could then accommodate roughly eight million entries: 200 × 200 × 200.
That’s illustrative arithmetic, not a fixed capacity. Actual sizes vary.
And it doesn’t mean “eight million checks become three checks”. We’re counting visits to index pages, each involving its own comparisons. Fetching the table record may require further work.
Still, that’s a remarkably short route through a large amount of data.
B+ tree leaves are also linked in order. For a range of names, the database can find the starting point and follow neighbouring leaves.
There is a maintenance bill
When data changes, the database updates the relevant indexes. An insertion into a full page can cause a split and updates to the signposts above it. New rows don’t universally go to the “bottom” of a table.
Extra indexes therefore cost storage and write effort. A primary-key index won’t automatically help a search by name, and maintaining multiple indexes is normal, rather than inherently error-prone.
The database may also decide a scan is cheaper, particularly for a tiny table or a query returning much of it. PostgreSQL’s introduction covers that trade-off.
That’s what caught my attention. Behind a familiar query is a carefully maintained structure that helps the database work out how much it can avoid reading.
I’ve spent years asking for the result. It’s quite satisfying to understand a little more about the journey.