In this lesson · topic 4 of 7
Topic 4
Each index is a tax on every write
4 min read
The write bill
A table with no secondary index appends a new row to its last page. The lab counts 0.03measured page writes per insert, because a page holds many rows and is written once when full.
An insert into a table with indexes must also place an entry in every index. The entries land in random leaf pages, since the indexed values are not in arrival order. With 2 indexes the lab writes 0.36measured pages per insert. With 5 it writes 0.86measured. That is 26 timesmeasured the cost of no indexes.
The same happens in the log, because each index change is a log record, and in the cache, because the index pages compete with table pages for memory.
When an index hurts
An index pays off when queries use it often enough to cover the writes it slows. It hurts when:
- the table takes many writes and few reads by that column,
- the column is updated often, so each update moves an entry,
- the column has few distinct values, so the index narrows nothing,
- another index already serves the query.
Dropping an unused index is a free speed-up for every write. Finding which are unused is the hard part, and databases keep counters for that.
Predict