What Is an Index? (B-Tree)
Overview
An index is an extra structure a database keeps so it can find rows quickly without reading the whole table. It works like the index at the back of a textbook: you look up a word and jump straight to the right page.
Most slow queries in real systems come from a missing index, so this is one of the most useful things to understand.
Detailed Notes
What is an index
An index is a separate, sorted structure that maps a column value (like an email) to the location of the matching row. The table is still the source of truth. The index is just a shortcut that the database keeps up to date for you.
The problem it solves
Imagine a users table with 200 million rows, and the login page asks: "find the user with email ann@example.com". With no index, the database must check every row one by one. This is called a full table scan. It reads gigabytes from disk and can take seconds or minutes, and it slows down every other query too.
With an index on email, the database finds the row in a few steps, usually in about a millisecond.
How it works
Databases read data from disk in blocks called pages. Reading a page is slow compared to memory, so the goal is to read as few pages as possible.
The most common index is the B-tree. Think of it as a sorted tree where each node is one page holding many keys:
- Start at the root page.
- Compare your search value with the keys in the page to pick the right child page.
- Repeat until you reach a leaf page, which holds the key and a pointer to the row.
Each page holds hundreds of keys, so the tree is very short. Even with a billion rows, a B-tree needs only about 3 to 5 page reads. Compare that with reading every row.
Because a B-tree keeps keys sorted, it supports more than exact matches:
- Equality:
email = 'ann@example.com' - Ranges:
age BETWEEN 20 AND 30 - Sorting:
ORDER BY created_at - Prefix search: names starting with "Jo"
When rows are added or deleted, the database splits or merges pages to keep the tree balanced. B-trees are the default index in most relational databases, and a table's primary key is normally indexed automatically.
A simple example
A library with 1 million unsorted books: finding a title means walking every shelf. Add a card catalogue sorted by title and you walk straight to the right shelf. The catalogue costs space and must be updated for new books, but searching is fast.
System design example
A shop's orders table has 500 million rows. The "my orders" page runs:
SELECT * FROM orders WHERE user_id = 42 ORDER BY created_at DESC LIMIT 20;
Without an index the database scans everything and the page times out. With an index on user_id, the database jumps to that user's rows and returns them in milliseconds. In an interview, whenever you say "we store X and look it up by Y", add "and index Y".
Summary
- An index is a sorted shortcut from column values to rows.
- Without one, the database does a full table scan that grows with table size.
- A B-tree finds a row in a few page reads, even among billions.
- B-trees support equality, ranges and sorting.
- The table stays the source of truth; the index must be kept in step.
Architecture Diagram
The index narrows the search to a few pages.
- 1The query asks for one email.
- 2The database walks the index from root to leaf.
- 3The leaf gives the location of the row.
- 4Only that one row is read from the table.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

