Composite index column order: equality first, then range, then sort
A B-tree index is sorted by its first column, then by the second within equal first values, and so on. That structure is why a query can seek only when it constrains a prefix of the columns. An index on (status, created_at) can find all rows with status = 'paid' and then walk them in created_at order. It cannot efficiently find rows by created_at alone, because within the index they are grouped by status first.
The practical rule is equality columns first, then a range or sort column. If a query filters status = 'paid' and created_at > '2026-01-01' and orders by created_at, the index (status, created_at) gives a seek on status and then a range scan in the right order. The reverse order, (created_at, status), forces a scan of every created_at after the date and a filter on status.
| Query | Good index | Why |
|---|---|---|
| status = ? ORDER BY created_at | (status, created_at) | Equality then order |
| status = ? AND created_at > ? | (status, created_at) | Equality then range |
| created_at > ? only | (created_at) | The first column must be constrained |
| status = ? AND city = ? | (status, city) or (city, status) | Both equality, either order works |
| city = ? ORDER BY created_at | (city, created_at) | Equality then order |
Leftmost prefix in practice
A three-column index (a, b, c) can serve queries on a, on a and b, and on a, b and c. It generally cannot serve a query on b alone or on b and c, because those columns are not a prefix. This is the leftmost-prefix rule, and it is why adding a column to an index is not free: the index becomes larger and its usefulness for other queries does not grow.
A query with equality on a and c but not b can still use the index, but c is not a usable prefix after a gap. PostgreSQL can sometimes use the index for a and then filter c, which may still be better than a table scan. The important case is the common query pattern: design the index for the queries that run most often, not for every combination of columns.
Ordering for a filter and a sort
When a query has an equality filter and an ORDER BY, the index order should be equality columns then the sort columns. The scan seeks to the equality value and returns rows already in sort order, so the Sort node disappears. With a LIMIT, the scan stops after N rows. This is one of the highest-value index shapes in an application, because it turns a sort of the whole result into a short read.
If the ORDER BY mixes directions, for example created_at DESC, id DESC, a plain B-tree on (created_at, id) can be scanned backwards to give both descending. A mix of ASC and DESC in the same index requires a matching index definition in PostgreSQL, which supports per-column direction. Check the exact ORDER BY before assuming a backward scan works.
When one wide index is not enough
Indexes are not free. Each one adds write cost on insert, update and delete, consumes storage and cache, and must be vacuumed. A table with six indexes writes six index entries per row. If two query patterns need different leading columns, two narrower indexes are often better than one wide index that neither query can use fully. Measure the write load before adding another.
A common mistake is to index every column that appears in a WHERE clause separately. The planner can use only one index per table in many cases, or combine them with a bitmap AND, which is less efficient than a well-ordered composite index for the main query. Start from the queries that matter, not from the columns.
A design checklist
- List the equality predicates in the query; those columns go first.
- Add the range or ORDER BY column next, choosing the direction that matches.
- Check whether the index prefix covers other frequent queries.
- Avoid adding columns that are only used in SELECT unless the index is meant to cover.
- Measure the write cost and index size before keeping it.
- Re-run EXPLAIN ANALYZE and confirm the plan uses the index as intended.
The analyzer proposes an index from the filter columns of a sequential scan, ordering equality columns before range columns. That ordering is a starting point based on one plan. A production index should be designed from the query workload, with the most frequent and most expensive queries first, and reviewed when the workload changes.
An index is read left to right. If the first column is not constrained, the rest of the index is mostly along for the ride.
Sources
- PostgreSQL documentation — Multicolumn indexes
- PostgreSQL documentation — Indexes and ORDER BY
- Use The Index, Luke — Concatenated keys
Questions
Does column order matter in a composite index?
Yes. A B-tree can seek efficiently using a prefix of the columns. An index on (status, created_at) serves a query filtering on status, or on status and created_at, but not a query filtering only on created_at. Equality columns should come first so the range or sort column can use the rest of the index in order.
What about a range on two columns?
A B-tree can use the first range column for a seek and then filter the second. An index on (a, b) with a > 10 and b = 5 uses a for the range and checks b on the index entries. If both conditions are selective, two indexes or a bitmap AND may be better.
How many columns should an index have?
As few as the queries need. Each additional column increases index size and write cost. Three or four columns is common for a filter-plus-sort index; beyond that, check whether a narrower index plus a different plan would do. Covering indexes with INCLUDE are a separate tool for avoiding heap visits.
The analyzer proposes column orders from the filters it finds in a sequential scan.
Open the analyzerWritten against the PostgreSQL and MySQL documentation and the standard cost model; every plan in the examples can be pasted into the analyzer linked above.
Back to the guides