Sub topic 1
Why indexes exist: pages and the cost of I/O
Prerequisites
This chapter assumes you know basic SQL (SELECT, WHERE, CREATE TABLE) and have a rough idea of binary search and trees. You should also know that a database stores its data on disk and reads it in blocks. Nothing else is needed.
The problem: finding one row among millions
Suppose an orders table holds 100 million rows and you run SELECT * FROM orders WHERE id = 42. Without an index, the database reads the table from start to finish and checks every row. This is a sequential scan, and its cost grows in direct proportion to the table size.
An index is a separate, sorted structure that maps key values to the location of matching rows. The database searches the index instead of the table. The question this tutorial answers is what shape that structure should have. The answer depends on how storage hardware actually behaves.
Pages: the unit of I/O
Disks do not hand out single bytes. Databases read and write fixed-size blocks called pages. PostgreSQL uses 8 KB pages by default, and MySQL's InnoDB uses 16 KB. Reading one byte costs the same I/O as reading the whole page that contains it.
I/O means input/output, here the transfer of data between disk and memory. It is far slower than work done on data already in memory:
| Operation | Rough latency |
|---|---|
| Read from CPU cache or RAM | nanoseconds to about 100 ns |
| Read a page from an SSD | tens to hundreds of microseconds |
| Read a page from a spinning disk | several milliseconds |
These figures vary by hardware, but the gap is consistent: a disk read is thousands of times slower than a memory access. Comparing two keys in memory is nearly free. Fetching a page is not. So the useful way to measure an index is by how many pages a lookup must read, not by how many comparisons it makes.
The database keeps recently used pages in a memory buffer, so hot pages are often free. A lookup that touches few distinct pages benefits most from this.
Fanout decides the cost
A good index packs as many keys as possible into each page. The number of child pointers a page holds is its fanout. A high fanout means each page read narrows the search by a large factor.
The arithmetic below assumes 16-byte entries: an 8-byte key and an 8-byte child pointer.
PAGE_SIZE = 8192 # bytes in one page
ENTRY_SIZE = 16 # assumed: 8-byte key + 8-byte child pointer
fanout = PAGE_SIZE // ENTRY_SIZE
print(fanout) # 512 entries per page, roughly
# Keys reachable with a tree of height h is on the order of fanout ** h
for height in (1, 2, 3):
print(height, fanout ** height)
# 1 512
# 2 262144
# 3 134217728With about 512 entries per page, three levels cover over 134 million keys. A lookup in the 100-million-row table needs roughly three or four page reads, compared with reading the entire table. The top levels are few and hot, so they usually sit in the buffer, and in practice only the last read or two reach the disk.
Common mistakes
- Counting comparisons instead of page reads. A structure with fewer comparisons can still lose if it touches more pages.
- Assuming an index is free. Each index takes disk space and must be updated on every write to the table.
- Treating the figures above as exact. Measure on your own hardware and page size.
The next chapters build on this idea. A binary tree has a fanout of two, so it is too tall for disk. A B-tree is designed around wide pages.
Exercise
A database uses 4096-byte pages and 16-byte index entries. Roughly what is the fanout, and how many keys can a tree of height 3 reach?
Show answer
Fanout is 4096 / 16 = 256. A tree of height 3 reaches about 256 ** 3 = 16,777,216 keys, so around 16.8 million.
Exercise
Why is the number of page reads a better cost measure than the number of key comparisons for a disk-based index?
Show answer
Comparing keys in memory takes nanoseconds, while reading a page from disk takes microseconds to milliseconds. Page reads therefore account for nearly all of the lookup time.