System design lab · Episode 5

How a database index finds a row fast

A database table keeps rows in the order they arrive. To find one row, the database may have to look at every row. An index is a sorted copy of one column that lets it jump almost straight to the row. Here a reader on a bridge checks one card at a time. Follow the lessons under the picture.

  1. Episode 1Load balancer
  2. Episode 2Cache
  3. Episode 3Rate limiter
  4. Episode 4Message queue
  5. Episode 5Database index
  6. Episode 6Replication and consensus
  7. Episode 7Circuit breaker
  8. Episode 8Backpressure
  9. Episode 9Retries and jitter
  10. Episode 10Consistent hashing
  11. Episode 11Partitioned log
  12. Episode 12Write-ahead log
  13. Episode 13Quorums
  14. Episode 14Unique IDs
  15. Episode 15The last ticket
  16. Episode 16Saga
  17. Episode 17Cache problems
  18. Episode 18Bloom filter
101010101010101010101010101010101010101010101010
The row or index entry the reader checks nowThe table: rows in the order they were addedThe index rack behind it: the same numbers, sorted

Controls

Index
Rows
Work
Time

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 benchWhat it means in a real system
Box at the leftThe query: the number the current job looks for or adds.
Cards on the front trayThe table: one card is one row, in the order it was added.
Rack behind the tableThe index: the same numbers, sorted. A cover lies on it while the index is off.
Bridge and readerThe 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.
MetersChecks 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. EXPLAIN shows 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.