You do everything right. You look at your slow query, you add the index that covers its columns, and the query planner thanks you by running a sequential scan anyway.
CREATE INDEX idx_orders_status_created
ON orders (status, created_at);
SELECT * FROM orders WHERE created_at = '2025-03-14'; -- still a seq scan?!
The index is not broken, and the planner is not confused. The index simply cannot answer that query — because a composite index on (status, created_at) is not two indexes glued together. It is one index, and it is sorted status-first. That single fact explains every surprise in this article.
A composite index is one sorted list of pairs
A B-tree index on (status, created_at) stores keys that are pairs — ('shipped', '2025-03-14') — and the whole tree is ordered by the first element of the pair, then the second. Think of a phone book sorted by last name, then first name. Finding “everyone whose last name is Chen” is fast: all the Chens sit together. Finding “everyone whose first name is John” means reading the entire book, because Johns are scattered across every last name.
That is exactly what happens inside the B-tree. Each descent from the root compares the leading column first; without a constraint on it, the tree has no idea which branch to take:
The leftmost-prefix rule
Here is the whole rule in one sentence: a B-tree index on (a, b) can drive a lookup only from a leftmost prefix of its columns — (a), or (a, b). A predicate on b alone cannot descend the tree.
This is not folklore; both major databases document the identical rule. PostgreSQL’s docs say a multicolumn B-tree index is most efficient with constraints on the leading columns, and spell out the exact mechanics for mixing equality and range predicates. MySQL’s docs are blunter: “any leftmost prefix of the index can be used by the optimizer to look up rows” — and WHERE first_name = 'John' on an index of (last_name, first_name) does not use the index. Two vendors, same tree, same rule.
The four query shapes worth memorizing:
Notice the third row: WHERE a = ? AND b >= ? fully uses the index. Equality on the leading column picks the subtree, and a range on the next column still trims the scan. Ranges only stop later columns from helping — more on that below.
The laboratory: two indexes, six queries
Enough theory. Let’s make it concrete with a table you have definitely seen in the wild:
CREATE TABLE orders (
order_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
status TEXT NOT NULL, -- 'shipped' | 'pending' | 'cancelled'
created_at DATE NOT NULL,
total NUMERIC(10, 2) NOT NULL
);
(Shown in PostgreSQL spelling; in MySQL swap the identity column for AUTO_INCREMENT. The indexing behavior is identical.)
Our table holds 10,000 rows: 7,000 shipped, 2,000 pending, 1,000 cancelled, with created_at spread over two years. We get to create exactly one composite index, and there are two candidates:
CREATE INDEX idx_orders_sc ON orders (status, created_at);
-- or --
CREATE INDEX idx_orders_cs ON orders (created_at, status);
Same columns, opposite order. Here is the full scorecard before you touch anything — then the lab lets you verify every cell:
Three things to notice while you play:
Q1 and Q2 are mirror images. WHERE status = 'shipped' loves the (status, created_at) index and is invisible to the other; WHERE created_at = '2025-03-14' is the exact reverse. Each query can only see the index that leads with its column. This is the phone book again: you cannot look up Johns by first name in a last-name-first book, no matter how good the book is.
Q3 is the peace treaty. With equality on both columns, either index order serves the query — about 10 rows visited either way. When your queries always constrain both columns, the order barely matters. The order matters precisely when your queries constrain different subsets.
Q4 is the trap. WHERE status = 'shipped' AND created_at >= '2025-01-01' fully uses (status, created_at) — 3,500 entries scanned — but only partially uses (created_at, status). That asymmetry deserves its own section.
The range subtlety: what sits right of a range is just a filter
PostgreSQL’s docs give the exact rule: equality constraints on the leading columns, plus any inequality on the first column without an equality constraint, limit the portion of the index scanned. Constraints to the right of that point are still checked inside the index — which saves trips to the table — but they do not shrink the scanned portion.
Walk through Q4 on (created_at, status) with that rule. The range on created_at is the first column without an equality, so it bounds the scan: all ~5,000 entries from 2025 onward are visited. The equality on status sits right of the range, so it becomes a filter applied during the walk — it discards non-shipped entries without visiting the heap, but the tree walk still covers the full 5,000-entry range. Compare with (status, created_at): the equality on status picks the 7,000-entry shipped subtree, and the range on created_at — now the first column without an equality — trims it to 3,500.
Practical upshot: equality columns go first, then at most one range column. Everything after a range column is along for the ride. If you remember one design heuristic from this article, make it that one.
Predicate order is irrelevant; index order is everything
Q4 and Q5 are the same query with the predicates written in opposite order:
WHERE status = 'shipped' AND created_at >= '2025-01-01';
WHERE created_at >= '2025-01-01' AND status = 'shipped';
The lab gives identical verdicts for both, because the optimizer normalizes AND predicates before planning — it does not care how you typed them. Only the index’s column order decides. So write your WHERE clauses in whatever order reads best, and spend your design energy on the CREATE INDEX statement instead.
Choosing the order
Given all of this, the decision procedure is short:
- List the queries the index must serve, not the columns in the table.
- Put equality-filtered columns first, then at most one range-filtered column. That is the order in which the tree can actually use them.
- If two genuinely different query shapes both matter — say, lookups by
statusand lookups bycreated_at— that is what two indexes are for. Each index is cheap to read and costs you on every write, so this is a real tradeoff, not a free lunch.
One myth to retire: “put the most selective column first.” Selectivity matters for how much the index narrows the scan, but predicate shape matters for whether it can narrow it at all. A low-cardinality leading column is perfectly fine if your queries pin it with =: our status column has exactly three distinct values, and WHERE status = 'shipped' still turns a 10,000-row scan into a 7,000-entry index walk. Order for your WHERE clauses, not for cardinality.
The rules, taped to your monitor
INDEX (a, b)serves queries on(a)and(a, b)— never on(b)alone.- Equality columns first, then at most one range column.
- A range ends the narrowing; everything right of it is a filter.
- Predicate order in SQL doesn’t matter; index column order does.
- Two access patterns, two indexes — at the cost of write overhead.
The next time a well-built composite index gets ignored by the planner, don’t reach for query hints. Read the index definition left to right, read your WHERE clause for its leftmost prefix, and check whether the two actually meet.
Sources
- PostgreSQL documentation, Multicolumn Indexes — the leftmost-column efficiency rule and the exact equality/range mechanics.
- MySQL 8.4 Reference Manual, Multiple-Column Indexes — “any leftmost prefix of the index can be used by the optimizer to look up rows.”