How do I choose the right composite index column order in PostgreSQL?
Question
I have an `orders` table with millions of rows, and my most frequent query is `WHERE user_id = X AND status = 'completed' ORDER BY created_at DESC`. It's painfully slow right now; the table has separate single-column indexes on `user_id`, `status`, and `created_at`, and when the DB tries to combine them with a Bitmap Index Scan the cost blows up. What's the most efficient composite index column order? And how does each column's selectivity (cardinality) affect that order?
Answer
Short answer: build one composite index for this query — (user_id, status, created_at DESC). Equality columns first, the ORDER BY column last; everything else falls out of that.
Short answer
The slowness isn’t a missing index, it’s the wrong index shape: with three separate single-column indexes the DB has to merge them via a Bitmap Index Scan and then run a separate Sort step — both expensive. I covered the tenant-scoped version of the same “which column should the hot table be organised by” question in the multi-tenant isolation answer.
Why
-
Equality columns belong in front, the sort column last. After applying the equality filter the B-tree already hands rows back in order, so no separate Sort step is needed. Break that order and the planner has to sort by itself.
-
Cardinality is secondary here. Both columns are equality predicates, so the real win is moving the sort into the index. Among equality columns the order does not change how much of the index is scanned (the PostgreSQL manual: equality constraints on leading columns limit the scanned portion);
user_idgoes first because it also serves queries that filter onuser_idalone. -
Redundant indexes aren’t free. The single-column
user_idindex is now a redundant prefix of the composite, yet it is still updated on every write.
What to do
-
Build the
(user_id, status, created_at DESC)composite. Equality columns first, the ORDER BY column last. -
DESCin the index definition is optional here. PostgreSQL can scan a B-tree backward, so(user_id, status, created_at)also satisfiesORDER BY created_at DESConce the two leading columns are pinned by equality. Spell the direction out when you want the index to document the query, and use it for real when the ORDER BY mixes directions. -
Confirm with
EXPLAIN (ANALYZE, BUFFERS). The plan you want is a single Index Scan — notBitmap Index Scan+Sort. If you still see a Sort, check the column order: the sort column has to come after the equality columns. -
Drop the single-column
user_idindex. The composite already serves every lookup on its leading column. Thestatusandcreated_atindexes are not prefixes of the composite, so keep them only if other queries use them; checkpg_stat_user_indexesbefore dropping. -
Consider a partial index if
completeddominates. A partial index withWHERE status = 'completed'shrinks the index, may keep more of it in RAM, and lowers write cost.
Bottom line: I’d build the (user_id, status, created_at DESC) composite index, confirm with EXPLAIN (ANALYZE, BUFFERS) that it drops to an Index Scan, and then drop the redundant user_id index. If your traffic piles onto one status, tighten it further with a partial index. For a deeper take on where indexing meets native SQL, see the sade.dev piece.
Related Reading
Comments
Sign in with your GitHub account to join the discussion. Comments are stored in GitHub Discussions.