Databases / Composite indexes

Composite indexes

Four users, one index on (city, age): which queries it speeds up, which it cannot, why the order of the columns decides, and when the table is not needed at all.

Intermediate7 steps~6 min
Query — what is being asked for
Index — entries sorted by city, then age
Table — the full rows, in no useful order
Pointer — leads from an entry to its row
// THE SHORT VERSION

In 30 seconds

A composite index is an index on two or more columns. Its entries are sorted by the first column, then by the second among equal firsts, like a phone book sorted by surname and then first name.

It helps queries that filter on the first column, or on the first and second together. A query on the second column alone cannot use it: that is the leftmost-prefix rule, and it is why column order matters.

If the index holds every column a query needs, the table is skipped entirely: a covering index.

Composite indexes, step by step

STEPS01 / 07

The index is drawn as a sorted list; in practice it is the leaf level of a B-tree. Some databases can “skip scan” on a later column when the first has few distinct values.

STEP 01 / 07 Two filters, no index
THE MATH
SELECT * FROM users WHERE city = 'Oslo' AND age = 22
  1. Step 1: Two filters, no index. The query wants users who live in Oslo and are 22. With no index the database reads every row and tests both conditions on each. One row matches, but all four had to be read to be sure.
  2. Step 2: Sort by city, then age. A composite index is built on several columns at once. Its entries are sorted by the first column, city, and rows with the same city are sorted by the second, age. Each entry also carries a pointer back to its row.
  3. Step 3: Both columns: one seek. Because the entries are sorted, the database can go straight to (Oslo, 22), the way you find a full name in a phone book, and follow its pointer to row 3. One entry read instead of four rows.
  4. Step 4: The leftmost prefix works. A query on city alone can use the same index. All the Lima entries sit next to each other, so the database jumps to the first one and reads on until the city changes. The index helps whenever the query uses its leading column or columns.
  5. Step 5: Skipping the first column. A query on age alone gets no such help. Read down the age column of the index: 25, 40, 22, 31. Ages are sorted only inside each city, so there is no place to jump to, and the database falls back to reading everything.
  6. Step 6: Column order matters. Swap the columns and you get a different index: sorted by age first, city second. Now age alone is a prefix and city alone is not. Choose the order from the queries you run: columns you always filter on go first.
  7. Step 7: A covering index. This query asks only for age, and filters only on city. Both columns are already in the index entries, so the answer is read from the index and the table is never visited. For this query the index is a covering index.
VALUES SO FAR
no index4 rows readcost
index····key
both····seek
city····prefix
age only····rule
swapped····key
covering····result

Spotted a mistake on this page?