Want the details?
What is a database index?
A database keeps each table as rows, more or less in the order they were added. If you ask for “the user with the email [email protected]”, the database has to look at the rows one by one until it finds it. This is called a full table scan. With a few rows it is fast. With ten million rows it is very slow.
An index is like the index at the back of a book. It keeps the values of one column in sorted order, and next to each value it notes where the row is. Because it is sorted, the database can find a value in a few steps and then go straight to the row.
How to read the bench
| Part on the bench | What it means in a real system |
|---|---|
| Box at the left | The query: the number the current job looks for or adds. |
| Cards on the front tray | The table: one card is one row, in the order it was added. |
| Rack behind the table | The index: the same numbers, sorted. A cover lies on it while the index is off. |
| Bridge and reader | The database reading data. Each stop of the reader is one check: one row or one index entry read. A card it has already checked turns pale. |
| Meters | Checks for the last job, the average of the last 20 jobs, and a bar for each of the last 20 jobs. |
Why halving is so strong
Each check in the sorted index throws away half of what is left. 24 rows need at most 5 checks. 1,000 rows need 10. A million rows need only 20. Without an index, a million rows need up to a million checks.
That is the whole trick: a scan grows as fast as the table, but a search in a sorted index grows very slowly.
The price of an index
- Writes get slower. Every insert, update or delete must also change every index on the table.
- It takes space. An index is a copy of a column, kept on disk and in memory.
- So you add indexes for the columns you often search or sort by, not for every column.
The numbers
- 8 rows, no index: about 4.5 checks to find a row, 8 when it is missing.
- 24 rows, no index: about 12.5 checks, 24 when missing.
- 24 rows with the index: at most 5 checks in the index, plus 1 to read the row.
- Adding a row: 1 step without an index, about 7 with it.
In real systems
- B-trees. Real indexes are not one long sorted list. They are usually a B-tree: a short tree of pages, each with hundreds of sorted entries. The idea is the same as halving, but each step chooses among hundreds, so even a billion rows need only 4 or 5 page reads.
- Primary key. Most databases build an index on the primary key (for example
id) by themselves. - Creating one. In SQL:
CREATE INDEX users_email ON users (email); - Seeing the plan.
EXPLAINshows whether a query uses an index or scans the whole table. “Seq Scan” in PostgreSQL means a full scan. - When a scan is fine. For a tiny table, or when a query needs most of the rows anyway, a scan can be faster than an index.
Words to know
- Row
- One record in a table, for example one user.
- Full table scan
- Reading every row to find the ones you want.
- Index
- A sorted copy of one or more columns, with a pointer to each row.
- Binary search
- Searching a sorted list by checking the middle and throwing away half each time.
- B-tree
- The tree structure most databases use for indexes.
- Query plan
- The steps the database chooses to answer a query.