ContentsThe library

How an Index Actually Works

A Phone Book Sorted by Surname Then First Name

Last timeWhat Happens on a Write

An index on two columns sorts by the first and breaks ties with the second. Every rule about which queries it serves follows from that one sentence.

Real queries filter on more than one column, and an index can hold more than

one. Which queries a composite index serves is the single most useful thing in

this course to be able to work out in your head, and it all comes from how the

keys are ordered.

The key is a pair

An index on customer and then date sorts by customer. Where two rows have the

same customer, it sorts those by date. That is the entire definition.

The phone book is the right picture and not a loose analogy: a book sorted by

surname, then by first name within each surname, has exactly this structure and

exactly these limitations.

FIG 1Eight rows, as an index on customer then date lays them out
steppositioncustomerdatewhat it is next towhat happened
11A-1142024-01-03the other A-114 rowsAll rows of one customer are adjacent, because customer is the first thing sorted on.
22A-1142024-06-21the other A-114 rowsWithin that customer, rows are in date order, because date breaks the tie.
33A-1142024-11-02the other A-114 rowsThree rows of one customer, contiguous and internally ordered by date. A query for this customer in that date range reads exactly this run.
44B-2072024-01-09the other B-207 rowsA new customer starts. Note what has happened to the dates: January follows November, because the date ordering only holds within a customer.
57C-0032024-01-15the other C-003 rowsJanuary again. Every January row in the table is scattered across the index, once per customer, which is why a filter on date alone cannot use this index at all.
5 steps
Reading down the date column shows the whole lesson. Dates are in order inside each customer and in no order whatsoever across the index as a whole. An index on two columns gives you a sorted first column and a sorted second column only within each value of the first.

The leftmost rule

From that layout, the rule follows without memorisation. A filter can narrow

the search only if it constrains the columns from the left, with no gaps.

The lesson stops here

5 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 Nameyou are here
  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