Stop 11 · Indexes & Query PerformanceTopics · 3/9

Composite Indexes and Column Order

Part of the SQL Roadmap

In short

One index on two columns can do the work of several, but only if the columns are in the right order. Get the order wrong and the index sits there unused.

A composite index covers several columns, and column order matters: an index on (city, date) helps filters on city, or city and date, but usually not date alone.

The jaadu stands on a railway platform beside a long train, running one finger down a paper chart of little seat boxes pasted on a coach door, while other coaches stretch far into the distance.

Think of it like this

The reservation chart on the coach door

The chart pasted on a train is sorted by coach, then by seat. Finding 'coach S4, seat 32' is quick: go to S4, then run down to 32. Finding 'everyone in coach S4' is quick too. But finding 'seat 32 in every coach' means walking the whole train and checking each coach. A composite index works exactly like that chart.

Watch it happen

An index on (city, order_date)

idx (city, order_date)
Delhi
Mumbai
Pune
Delhi, 04 Jan
Delhi, 15 Mar
Mumbai, 15 Mar
Pune, 02 Mar
Pune, 18 Mar

1/5Entries are sorted by city first. Inside each city, they are sorted by order_date.

The big ideas

What you need to know

Leftmost prefix rule

An index on (a, b, c) helps filters on a, on a and b, or on a, b and c. Filters on b or c alone usually can't use it.

Equality first, range last

Put columns tested with = first and the column tested with >, < or BETWEEN last, like (city, order_date).

It can sort for you

WHERE city = 'Pune' ORDER BY order_date reads rows already in order, so the database skips the sort step.

One can replace two

An index on (city, order_date) makes a separate index on city alone redundant. You can drop the single one.

See it in code

Walk through the code

sql
1CREATE INDEX idx_orders_city_date ON orders (city, order_date);2 3-- Full use: equality on city, range on order_date4SELECT order_id, amount5FROM orders6WHERE city = 'Pune'7  AND order_date >= DATE '2026-03-01'8  AND order_date <  DATE '2026-04-01';9 10-- Leading column only: still uses the index11SELECT COUNT(*) FROM orders WHERE city = 'Pune';12 13-- Second column only: usually can't seek with this index14SELECT COUNT(*) FROM orders WHERE order_date = DATE '2026-03-15';

1/4City is first because most reports filter on one city with =. Date is second because it is filtered with a range.

PostgreSQL syntax, same idea in MySQL and SQL Server. MySQL 8 can sometimes do a 'skip scan' on the second column, but don't design around it.

Save yourself the pain

Common mistakes

Oops

Creating (order_date, city) when every query filters one city across a date range.

Do this instead

Put the equality column first: (city, order_date). The range column goes last so the index can read one tight slice.

Oops

Keeping an index on (city) and another on (city, order_date).

Do this instead

The composite already serves city-only filters. Drop the single-column one and save the write cost.

Your mission

Pick the column order

~20 min

Quick check

With an index on (city, order_date), which filter can NOT use it to jump straight to the rows?

Go deeper

  1. 1Create a composite index and test queries filtering on each column separately.
  2. 2Learn the leftmost-prefix rule.
  3. 3Put the most commonly filtered column first.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge