Skip to content

Composite indexes and column order

Working knowledgeUnder the hood

An index on (a, b) speeds up WHERE a and WHERE a AND b, and then does nothing for WHERE b. The reason is how the entries are sorted, and you can see it by walking the tree for each query.

01 One index, three queries

A people table has an index on two columns, last name and then first name. Three queries filter on those columns in different ways. Two of them are fast. The third ignores the index, even though first_name is right there in it.

schema.sql
CREATE INDEX people_last_first_idx
ON people (last_name, first_name);
queries.sql
-- 1. fast
SELECT * FROM people WHERE last_name = 'Kim';
-- 2. fast
SELECT * FROM people WHERE last_name = 'Kim' AND first_name = 'Jo';
-- 3. reads the whole table
SELECT * FROM people WHERE first_name = 'Jo';

02 Not two indexes in one

It is tempting to picture a two-column index as two lookups glued together: one by last name, one by first name. It is not. A composite B-tree is one sorted list of pairs. It is sorted by last_name, and only where two entries share a last name does first_name decide their order. A phone book works the same way.

Twelve index entries in order, across three leaf pages
Root · index page
→ p1 · below Kim
→ p2 · Kim to Lopez,Ray
→ p3 · Lopez,Ray on
Leaf page 1 · sorted by (last, first)
Adams · Jo
Adams · Mia
Brown · Al
Brown · Jo
Leaf page 2 · sorted by (last, first)
Kim · Ana
Kim · Jo
Kim · Lee
Lopez · Jo
Leaf page 3 · sorted by (last, first)
Lopez · Ray
Patel · Ivy
Patel · Jo
Wu · Jo

Every Kim is in one run. The Jos are spread across all three pages, wherever their last name puts them.

03 WHERE a: one contiguous run

When the query fixes the first column, the tree can find where that value starts and read forwards until it ends. Postgres walks down from the root, one page per level, like any B-tree lookup.

WHERE last_name = 'Kim'
Step 1 of 6
Entries checked 0
Root · index page
→ p1 · below Kim
→ p2 · Kim to Lopez,Ray
→ p3 · Lopez,Ray on
Leaf page 1 · sorted by (last, first)
Adams · Jo
Adams · Mia
Brown · Al
Brown · Jo
Leaf page 2 · sorted by (last, first)
Kim · Ana
Kim · Jo
Kim · Lee
Lopez · Jo
Leaf page 3 · sorted by (last, first)
Lopez · Ray
Patel · Ivy
Patel · Jo
Wu · Jo
  • reading now
  • compared, not a match
  • a match
  • path taken
  • not read

The query asks for every Kim. Nothing is read yet.

0 index pages read

04 WHERE a AND b: a narrower run

Fixing both columns gives the search a full pair to look for. Within the Kims, the entries are sorted by first name, so the search can skip straight to the right one.

WHERE last_name = 'Kim' AND first_name = 'Jo'
Step 1 of 4
Entries checked 0
Root · index page
→ p1 · below Kim
→ p2 · Kim to Lopez,Ray
→ p3 · Lopez,Ray on
Leaf page 1 · sorted by (last, first)
Adams · Jo
Adams · Mia
Brown · Al
Brown · Jo
Leaf page 2 · sorted by (last, first)
Kim · Ana
Kim · Jo
Kim · Lee
Lopez · Jo
Leaf page 3 · sorted by (last, first)
Lopez · Ray
Patel · Ivy
Patel · Jo
Wu · Jo
  • reading now
  • compared, not a match
  • a match
  • path taken
  • not read

Now the query fixes both columns. The search key is the pair (Kim, Jo). Nothing is read yet.

0 index pages read

05 WHERE b: no place to start

Fixing only the second column gives the tree nothing to search by. The Jos are not together anywhere, so there is no start and no stop.

WHERE first_name = 'Jo'
Step 1 of 5
Entries checked 0
Root · index page
→ p1 · below Kim
→ p2 · Kim to Lopez,Ray
→ p3 · Lopez,Ray on
Leaf page 1 · sorted by (last, first)
Adams · Jo
Adams · Mia
Brown · Al
Brown · Jo
Leaf page 2 · sorted by (last, first)
Kim · Ana
Kim · Jo
Kim · Lee
Lopez · Jo
Leaf page 3 · sorted by (last, first)
Lopez · Ray
Patel · Ivy
Patel · Jo
Wu · Jo
  • reading now
  • compared, not a match
  • a match
  • path taken
  • not read

The query asks only for first name Jo. The tree is sorted by last name first, and the query says nothing about last name. Nothing is read yet.

0 index pages read
WHERE last_name = 'Kim'One runStart at the first Kim, stop after the last.
WHERE first_name = 'Jo'Every entryNo start, no stop. Usually cheaper to scan the table.

Postgres 18 added skip scan for B-tree indexes. When the leading column has few distinct values, it can jump to (each last name, 'Jo') in turn instead of reading every entry. With many distinct leading values, as in a list of surnames, it is still close to a full scan. Earlier versions do not skip.

06 See it in EXPLAIN

On Postgres 16, with 200,000 people, the pair lookup is an index scan with both columns in Index Cond. The first-name-only query is a sequential scan with a Filter.

last AND first
Index Scan using people_last_first_idx on people
Index Cond: ((last_name = 'Kim'::text)
AND (first_name = 'Jo'::text))
Buffers: shared hit=5
first only
Seq Scan on people
Filter: (first_name = 'Jo'::text)
Rows Removed by Filter: 199500
Buffers: shared hit=1278

Five pages against 1,278. If queries by first name alone matter too, give them their own index on (first_name).

07 Choosing the column order

  • Equality columns first, the range column last. For WHERE last_name = 'Kim' AND born > 1990, index (last_name, born): the Kims are one run, and within it birth years are sorted, so the range is one smaller run.
  • The other way round, the second column only filters. With (born, last_name), every entry after 1990 is in range and is checked for Kim one by one. EXPLAIN still lists both columns under Index Cond, so read it with care.
  • One index can serve the column on its own. (last_name, first_name) already answers WHERE last_name = ..., so a separate index on (last_name) alone is usually wasted writes.
  • The order can save a sort. WHERE last_name = 'Kim' ORDER BY first_name can read the Kims already in first-name order. With a LIMIT, Postgres 16 used the index and skipped the Sort node.

08 When the composite index won't help

WHERE first_name = 'Jo'Skipping the leading columnThe entries are not grouped by the second column, so the tree has no place to start. Add an index that leads with it.
WHERE last_name > 'K' AND first_name = 'Jo'A range before an equalityOnly the range bounds the walk. Every entry from K to the end is read and checked for Jo.
WHERE last_name = 'Kim' OR first_name = 'Jo'OR across the columnsThe first_name half has no run to read in this index, so Postgres usually scans the table. With a second index on first_name, it can combine the two with a BitmapOr.
(a, b, c, d, e)Too many columnsEach extra column makes the index bigger and every write dearer. Add columns that real queries filter or sort on, in the order they use them.

09 Check yourself

Question

There is an index on (country, city). Which query can use it to find a narrow run of entries?

Question

Queries look like WHERE user_id = ? AND created_at > ?. Which index fits best?

Question

You have an index on (last_name, first_name). Should you also add one on (last_name)?

10 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?