Basis Lab

How an Index Actually Works

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.

FIG 1The two halves of a lookup, as the number of matching rows grows
steprows the filter matchesstorage reads: the descent, then one a rowthe fetches as a share of those readstime, at 0.09 ms a scattered fetchwhat happened
113 for the descent, plus 1 fetch1 read in 41.2 ms + 0.09 ms = 1.3 msFor 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.
2103 for the descent, plus 10 fetches10 reads in 131.2 ms + 0.9 ms = 2.1 msThe crossover happens here, quietly, with no symptom anybody notices: the fetch has gone from a quarter of the reads to three quarters of them.
31,0003 for the descent, plus 1,000 fetches1,000 reads in 1,0031.2 ms + 90 ms = 91.2 msThe 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.
3 steps
The descent does not grow and the fetch does, so the third column goes one way only, and a thousand scattered reads against three cached ones is what a report feels like when it has gone slow for no apparent reason. Every fetch here is assumed to miss the cache, which for a table too large to hold in memory is near enough true. Note that the last column attacks the wrong thing: the lesson is not about making a thousand fetches faster, it is about there being none, which is 5.2 ms rather than 91.2 ms in the figure below.
FIG 2Query time against rows matched, with and without the fetch
0.0050.00100.00150.00200.000.0500.01000.01500.02000.0rows matched by the filter
seek, then fetch each rowanswered from the index alone
Both lines start at the same place, because both pay for the descent. They diverge at about twenty to one, which is the ratio between a scattered read into the table and reading the next entry in a page of the index that is already in hand. At a thousand rows the difference is ninety milliseconds against five, and the shape of the lower line is why people call this the biggest free win in index design.

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 contents

This 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

The rest of this course

  1. 01The Database Never Reads a Row
  2. 02Eighteen Reads Instead of a Quarter of a Millionopening only
  3. 03Three Reads, and Two of Them Were Already in Memoryopening only
  4. 04The Page Is Full, So Cut It in Halfopening only
  5. 05A Phone Book Sorted by Surname Then First Nameopening only
  6. 06The Index Already Knew the Answeryou are here
  7. 07One Is Faster and Cannot Do Ranges, the Other Is for Writingopening only
  8. 08The Index Is Right There and the Database Will Not Use Itopening only

Read alongside