Basis Lab

How an Index Actually Works

Three Reads, and Two of Them Were Already in Memory

Last timeSorted Means Searchable

Put the keys in sorted pages and build pages of signposts above them. The depth is tiny because a page holds hundreds of keys, and the top of it never leaves memory.

The previous lesson ended with the design: leave the rows alone and build a separate structure holding the keys in order. This lesson builds it, and the whole of it follows from one observation about page sizes.

A page of pointers

The sorted file halved the search on every read because a page told you one thing: is the target above or below this page. That is a waste of a page. An eight kilobyte page can hold far more than one signpost.

Fill a page with key-and-pointer pairs instead. Each pair says: everything from this key onwards lives under this page. Now one read does not eliminate half the remaining file, it eliminates all but a few hundredth of it.

FIG 1How many children one page points at, out of the 8,000 usable bytes left after the header
stepwhat sits in each pairbytes a pair, key plus an eight-byte pointerchildren one page points atkeys three levels then coverwhat happened
1an eight-byte integer key8 + 8 = 16 bytes8,000 / 16 = 500500 x 500 x 500 = 125 millionThe arithmetic on its own, before any implementation has had its say. One read eliminates all but a five-hundredth of what is left, where the sorted file of the last lesson eliminated half.
2the same key, once the page keeps its own bookkeeping and the slack it leaves for later insertionsa little more than 16 bytesa few hundred, call it 300300 x 300 x 300 = 27 millionThree hundred is the number to carry around. The slack is deliberate rather than waste: a page with room left in it can take an insertion without splitting.
3a hundred-byte text key100 + 8 = 108 bytes8,000 / 108 = about 7474 x 74 x 74 = about 405,000The same structure, the same page size, a quarter of a million times less reach for three levels, bought entirely by the width of the key.
3 steps
Read the first column against the last. Nothing here is a property of the index: the page is 8,000 usable bytes either way, and what changes is how much of each pair is key. Wide keys branch less, and the last column is why that is worth caring about, because an index on a hundred-byte text column runs out of reach at 405,000 rows and needs a fourth level, while the eight-byte key is still on three levels at twenty-seven million. That is also the strongest argument in the course for narrow keys.

Depth from branching

With a branching factor of three hundred, count what each level can hold. The bottom level holds the keys themselves, three hundred to a page. One level up, each page points at three hundred bottom pages, so it covers ninety thousand keys. One level above that covers twenty-seven million.

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. 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 Memoryyou are here
  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 Answeropening only
  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