Databases / Hash indexes

Hash indexes

A four-row table, a toy hash function and eight buckets: how a hash index finds one row in a single jump, and why it cannot help with ranges or sorting.

Beginner7 steps~6 min
Key — the value being looked up
Hash function — turns a key into a bucket number
Bucket — a numbered slot holding entries
Table — the rows the index points at
// THE SHORT VERSION

In 30 seconds

A hash index finds rows by exact value. It runs the value through a hash function, which turns it into a bucket number, and keeps a pointer to the row in that bucket.

A lookup repeats the calculation and jumps straight to the right bucket, so the cost stays about the same however large the table grows.

The price is order. Similar values land in unrelated buckets, so a hash index cannot answer “between”, “greater than” or “sorted by”. A B-tree index can, which is why it is the usual default.

Hash indexes, step by step

STEPS01 / 07

Real hash functions mix bits far more thoroughly than a letter sum, and real indexes add buckets as they fill. Some databases keep only the hash code in the bucket and check the key against the row.

STEP 01 / 07 Without an index: scan
THE MATH
SELECT * FROM users WHERE name = 'Ben' scan: 3 rows read
  1. Step 1: Without an index: scan. To find Ben, the database has no better option than reading the rows one by one and comparing each name. Three rows mean three checks here; a table with a million rows could mean a million.
  2. Step 2: Hash the key. A hash function turns a key into a number in a fixed range. This toy one adds the letters’ positions in the alphabet and keeps the remainder after dividing by 8. Ben gives 2 + 5 + 14 = 21, and 21 mod 8 = 5.
  3. Step 3: Build the index. The index is an array of eight buckets. For every row, the database hashes the name and stores an entry in that bucket: the key, plus a pointer to where the row lives. The table itself is left as it was.
  4. Step 4: Lookup: one jump. To find Ben now, hash the key again, go straight to bucket 5, and follow the pointer to row 3. The work is the same whether the table has three rows or three million: constant time, written O(1), on average.
  5. Step 5: Collisions. Eve joins, and 5 + 22 + 5 = 32 gives bucket 0, where Ana already is. Two keys in one bucket is a collision. The bucket simply holds both entries, and a lookup compares the keys inside it to pick the right one.
  6. Step 6: No order, no ranges. Now ask for every name from B to D. Sorted, Ben and Cy are neighbours, but the hash put them in buckets 5 and 4, in no meaningful order. Hashing “B” says nothing about where “Ben” is, so the database must scan all four rows.
  7. Step 7: Hash or B-tree?. A B-tree index keeps its keys sorted, so it answers equality, ranges, sorting and prefix searches in a few steps each. A hash index answers equality only, though very directly. That is why B-tree is the usual default, and hash a special case.
VALUES SO FAR
scan3 rows readcost
h('Ben')····hash
h('Ana')····hash
h('Cy')····hash
lookup····cost
h('Eve')····collision
range····cost
default····rule

Spotted a mistake on this page?