Sitemap

Member-only story

How a Database Index Works — And What a B-Tree Actually Does

3 min readApr 25, 2026

--

Why one missing index can bring an entire app to its knees

Imagine your database has 50 million users. Someone runs this query:

SELECT * FROM users WHERE email = 'sara@example.com';

Without an index, the database reads every single row — all 50 million of them — until it finds Sara. This is called a full table scan, and it’s exactly as slow as it sounds.

Add one index on the email column, and the same query returns in milliseconds.

Here’s what actually changed.

Press enter or click to view image in full size

An Index Is a Separate Data Structure

An index isn’t magic baked into the table. It’s a completely separate object the database maintains alongside your data — a sorted, optimized structure built specifically for fast lookups.

Think of it like the index at the back of a textbook. You don’t re-read the whole book to find “photosynthesis.” You jump to the index, find the page number, and go directly there. A database index works the same way: it stores the indexed column’s values in sorted order, each one pointing to the exact row on disk.

The most common structure used to build that index is a B-tree.

--

--