ContentsThe library

How an Index Actually Works

Eighteen Reads Instead of a Quarter of a Million

Last timeThe Cost of Reading Everything

Sorting a file turns finding a row from a quarter of a million reads into eighteen. Then somebody inserts a row, and the whole of the rest of the course follows.

The previous lesson ended with a question: what is the cheapest change to the

layout of a table that makes finding a row cost less than reading all of it?

The answer is to put the rows in order, and it is worth seeing exactly how much

that buys before seeing what it costs.

Halving the search

Take the quarter of a million pages from the first lesson and suppose they are

in order by the column being searched. Read the middle page. Either the row is

on it, or the row is in the half above, or the row is in the half below. In one

read, half of the file has been eliminated.

Repeat. Each read halves what remains, so the question is how many times a

quarter of a million can be halved before one page is left.

FIG 1Reads to find a row in a sorted file
number of page reads a search costs
number of pages in the file
244,000 pages gives about eighteen reads. The scan read all 244,000. That is a factor of thirteen thousand, bought by nothing but the order the rows happen to sit in, and it is the single largest improvement available anywhere in this course.
FIG 2One search, over 244,000 sorted pages
stepreadpages still possiblepage examinedoutcomewhat happened
11244000122000target is aboveOne read has eliminated 122,000 pages. Nothing clever happened: the file was in order, so a single comparison settled an entire half.
2515250129625target is belowAfter five reads the field is down to fifteen thousand pages. The halving is relentless and it does not care how large the file was to begin with.
312119122440target is aboveTwelve reads in, a hundred and nineteen pages remain out of a quarter of a million.
4181122451row foundEighteen reads. Doubling the size of the table would make it nineteen, which is the property that makes this worth building a course around.
4 steps
Note the shape of the progress. The first read does more work than the last ten thousand reads of a scan would. Each subsequent read is worth half as much as the one before, which is why a structure like this degrades so gracefully as data grows.
FIG 3Reads to find a row, against the size of the file
0.006.2512.5018.7525.001000.0250750.0500500.0750250.01000000.0pages in the file
halving a sorted filedividing by 300 instead
The upper curve is almost flat across three orders of magnitude of file size: ten pages to twenty, for a thousandfold increase in data. The lower curve is the whole of the next lesson in one line. Nothing says the file has to be cut in half at each step, and a page of a few thousand bytes can hold several hundred keys, so each read can eliminate all but a three-hundredth.

Eighteen reads is still eighteen scattered reads, each with its own latency, so

it is not eighteen times nothing. It is around two milliseconds against a

couple of seconds, which is the comparison that matters.

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 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 Millionyou are here
  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 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