Vivcre Learn learn it · write it · retain it

← All posts

Database #indexes#composite-index#leftmost-prefix#query-planning#b-tree

Composite Index Column Order: Equality First, Range Last

25 Jun 2026

A composite index on (country, age) isn’t two indexes — it’s one B-tree sorted by country first, and by age within each country. That single fact decides everything about which queries it can serve. (If the single-column story isn’t solid yet, start with how an index finds a row.)

The leftmost-prefix rule

An index on (a, b) can serve a filter on a, or on a AND b — but not on b alone. Walk three queries against an index on (country, age):

That’s the leftmost-prefix rule: an index on (a, b) serves a and a AND b, never b by itself.

The real ordering rule: equality first, range last

“Put the most-queried column first” points the right way but isn’t the rule. The rule comes straight from the sort order:

So (country, age) serves country = 'IN' AND age > 30 perfectly — equality pins the block, range slices it. Flip it to (age, country) and the same query gets much worse: age > 30 spreads you across dozens of ages, and country is scattered inside every one. Same two columns, opposite performance, decided entirely by order.

The heuristic

Order composite index columns as: equality columns first (most selective among them earliest as a tiebreaker), then a single range column, then any columns you only want along for the ride. And the mirror image is the number-one reason people say “the planner ignored my index”: the query filters on a column that isn’t a leftmost prefix of any index, so there’s nothing for the planner to seek into.


Practise these questions →

Spaced-repetition MCQs for this post, on practise.vivcre.com.