Basis Lab

Why a Query Is Slow

What an Index Actually Is

Last timeWhere the Time Actually Goes

An index is a second copy of a few columns, kept in order, with a pointer back to each row. Everything it can and cannot do follows from that one sentence.

The word index is borrowed from books, and the borrowing is exact. The index at the back of a book is a second, shorter document, listing terms in alphabetical order, each followed by a page number. It does not contain the book. It contains enough to find the book's parts quickly, and it is kept in an order the book itself is not in.

A database index is the same object. It holds the values of one or a few columns, sorted, each paired with a pointer to the row it came from. That sentence contains the whole subject, and everything else in this lesson is a consequence of it.

FIG 1What happens on an indexed lookup
The descent is cheap and almost size-independent. The expensive part, when there is one, is the box near the bottom, and that box exists only because the index did not hold everything the query asked for.

Why the depth stays small

The index is not a sorted list, because inserting into the middle of a sorted list is ruinous. It is a tree. The bottom level holds the sorted entries. Above it sits a level holding one entry per bottom page, saying what range that page covers. Above that, the same again, until a single page covers everything.

The fan-out ff, meaning how many entries one page holds, is large, because the entries are small: two hundred to five hundred of them is ordinary. So a tree over nn rows comes out very wide and very shallow, and its depth dd is just the number of times ff has to be multiplied by itself to reach nn.

FIG 2How many levels a tree needs
the number of rows indexed
the fan-out, or how many entries fit on one page
the number of levels, which is the number of page reads to reach the bottom
Multiply by and goes up by exactly one, so two hundred times the rows costs one more page read. That is the entire reason indexed lookups scale: the work grows with the logarithm of the data, and the logarithm of a very large number is a small number.
FIG 3Levels in the tree against rows indexed
1.002.003.004.005.001000.0250000750.0500000500.0750000250.01000000000.0rows indexed
levels needed, at two hundred entries a pagefour levels, for reference
A thousand rows and a billion rows differ by a factor of a million, and by about two page reads. This is why nobody worries about index lookups getting slower as a table grows, and why they should worry a great deal about the things that stop the index being used at all.
FIG 4Finding one customer in a table of forty million
steplevelentries this page coverspages read so farmicroseconds so farwhat happened
114000000011The top page. Always in memory, so this read is effectively free.
2220000022One comparison narrowed forty million candidates to two hundred thousand.
33100033Again. The search is now within a thousand rows.
4414110The bottom level, and then the row itself, which was not in memory and cost a hundred microseconds on its own.
4 steps
Three of the four reads cost nothing, because the upper levels of a busy index are permanently in memory. The entire cost of this lookup was the last step, fetching the row the index pointed at.

The second fetch, and how to avoid it

An index entry knows where a row is. It does not know what is in it. If a query needs any column not held in the index, the database must go to the table and fetch the row, and those fetches happen in whatever order the index happened to produce, which is to say scattered.

For one row this does not matter. For fifty thousand matching rows it is the whole cost of the query, and it is why an index on a column that matches a quarter of the table is often not used at all.

The remedy is to put everything the query needs into the index itself. Then the answer can be assembled from the index without touching the table once, and the scattered fetches simply do not occur. This is usually worth more than any other single change available, and it is the reason indexes sometimes carry columns nobody would ever search on.

The lesson stops here

3 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. 01What a Query Is Actually Spending
  2. 02What an Index Actually Isyou are here
  3. 03The Conditions No Index Can Rescueopening only
  4. 04Every Plan Rests on an Estimateopening only
  5. 05The Three Ways to Joinopening only
  6. 06How to Read a Planopening only
  7. 07The Bill for Every Indexopening only
  8. 08A Method Instead of a Guessopening only

Read alongside