ContentsThe library

How an Index Actually Works

One Is Faster and Cannot Do Ranges, the Other Is for Writing

Last timeAnswering Without Touching the Table

Two structures exist alongside the tree. A hash is unbeatable on exact lookups and useless on ranges; a write-ordered log is for data arriving faster than a tree can take it.

Six lessons have been about one structure. There are two others worth knowing,

and knowing them is mostly about knowing what each one gives up, because each

gives up something the tree has.

The hash index

A hash index computes a number from the key and uses it to pick a bucket. The

key is in that bucket or nowhere. One read, no descent, no depth growing with

the table.

FIG 1Five operations against three shapes
reads for an exact lookureads for the first row reads for a range of tencost of an insert, tree space used, tree equals
tree2111100100
hash1111195110
write-ordered log53525170
The two marked cells are the whole lesson. A hash needs as many reads for ten adjacent rows as for ten unrelated ones, because after hashing there is no such thing as adjacent, so the eleven in that cell is really a confession that it cannot do ranges. The log takes an insert at a quarter of the cost, which is what it is for. Everything else here is a modest difference and would not justify choosing a different structure.

The hash wins on exact lookups and the margin is smaller than people expect:

two or three reads against one, where the two or three are nearly always cached.

What it loses is total. Hashing scrambles the keys on purpose, so keys that were

next to each other are now in unrelated buckets. There is no range query, no

ordering, no sorted output, no prefix match, and no help with a composite key

unless the query supplies every column.

That is why hash indexes are rare as a general tool and common in specific

places: joining on an identifier inside a single query, caches keyed by exact

lookup, and partitioning rows across machines, where scattering is the goal

rather than a side effect.

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 Nameopening only
  6. 06The Index Already Knew the Answeropening only
  7. 07One Is Faster and Cannot Do Ranges, the Other Is for Writingyou are here
  8. 08The Index Is Right There and the Database Will Not Use Itopening only

Read alongside