The Index Already Knew the Answer
Last timeIndexes on More Than One Column
A lookup ends by fetching the row, and that fetch is most of the cost. If every column the query mentions is in the index, the fetch never happens.
Every lookup in this course has ended the same way: the index gives you a pointer, and then you go and get the row. This lesson is about not doing that, and it is the largest single saving available from index design.
The fetch you are paying for
Separate the two halves of a lookup. Finding the matching entries in the index costs the depth of the tree, which the third lesson put at two or three reads with the upper levels cached. Fetching the rows costs one read per row, each one landing somewhere unpredictable in a table far too large to cache.
| step | rows the filter matches | storage reads: the descent, then one a row | the fetches as a share of those reads | time, at 0.09 ms a scattered fetch | what happened |
|---|---|---|---|---|---|
| 1 | 1 | 3 for the descent, plus 1 fetch | 1 read in 4 | 1.2 ms + 0.09 ms = 1.3 ms | For a single matching row the descent is the whole query, and nothing in this lesson is worth doing. This is the case everybody has in mind when they say an index made it fast. |
| 2 | 10 | 3 for the descent, plus 10 fetches | 10 reads in 13 | 1.2 ms + 0.9 ms = 2.1 ms | The crossover happens here, quietly, with no symptom anybody notices: the fetch has gone from a quarter of the reads to three quarters of them. |
| 3 | 1,000 | 3 for the descent, plus 1,000 fetches | 1,000 reads in 1,003 | 1.2 ms + 90 ms = 91.2 ms | The descent is now three reads in a thousand. The index is doing almost nothing and the query is almost entirely the fetch, which is the condition this lesson exists to remove. |
Note what the ratio is made of. The fetches are scattered, so each one is a separate request the storage device cannot anticipate, which is exactly the distinction from the first lesson. The index entries are adjacent, so reading the next one usually costs nothing at all because it is in the page already read.
The lesson stops here
6 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