The Bill for Every Index
Last timeReading What the Database Tells You
An index makes some reads fast and makes every write to its table slower, takes space, competes for memory and has to be maintained forever. The bill is real and usually unexamined.
Indexes are discussed as though they were free, as a thing you add when a query is slow. Every one of them is a second copy of some of your data, maintained synchronously, forever, by every write that touches the table.
The write bill
An insert into a table with no indexes modifies one page. On a table with six indexes it modifies seven, in seven different places, and all seven have to be made durable.
Updates are more subtle. An update only has to touch the indexes that contain a column it changed, which means a table with many indexes can still have cheap updates if the frequently changed columns are not indexed. That is a design lever worth knowing about: keeping the hot column out of every index keeps the hot path cheap.
| step | indexes on the table | pages one insert modifies | the index share of them | against a table with none | what happened |
|---|---|---|---|---|---|
| 1 | none | 1 page, the table row itself | none of it | 1 time | The insert everybody pictures. One page with room on it, one row placed, one page to be made durable before the write is acknowledged. |
| 2 | one | 1 plus 1, so 2 pages | 1 of 2, so half of it | 2 times | The very first index doubles the work of every insert on the table, for as long as the index exists. That is why throughput falls most steeply at the start and never again as steeply. |
| 3 | six | 1 plus 6, so 7 pages | 6 of 7, so 86 per cent | 7 times | Seven pages in seven unrelated places, because an index entry belongs wherever its value sorts and not where the row went. All seven are durable before the user is told the insert worked. |
| 4 | nine | 1 plus 9, so 10 pages | 9 of 10, so 90 per cent | 10 times | Ten pages for one row, and nine tenths of that is bookkeeping. Two of these nine indexes have answered no query in a month, and every insert has been paying for them throughout. |
The memory bill
Indexes live in the same memory as the table data. A large index that is being read and written keeps its pages resident, and those pages displace table pages that other queries were depending on.
This is the index cost that is almost never attributed correctly. A query that does not use the new index gets slower, because the pages it used to find in memory now come from storage. Nothing about that query changed, nothing in its plan changed, and the cause is three steps away.
The lesson stops here
4 more paragraphs to go
You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.
The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.
See the planThe contentsThis is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents