Sub topic 9
Composite indexes and column order
This chapter assumes you know basic SQL, understand that a B+tree keeps entries sorted, and know that a lookup descends from the root to a leaf. It covers how a multi-column index is ordered and how to choose the column order.
How a composite index is sorted
A composite index is an index on more than one column. Its entries are sorted by the first column. Entries with equal first values are then sorted by the second column, and so on. This is the same ordering as sorting a phone book by last name and then by first name.
CREATE TABLE orders (
id bigint PRIMARY KEY,
customer_id integer NOT NULL,
created_at timestamptz NOT NULL,
total numeric(10, 2) NOT NULL
);
-- PostgreSQL creates a B-tree index by default.
CREATE INDEX orders_customer_created_idx
ON orders (customer_id, created_at);In this index, all entries for customer 42 sit next to each other in the leaf pages. Inside that group they are in created_at order. One tree descent finds the start of the group, and the linked leaf pages let the database read the rest in order.
The leftmost-prefix rule
The index can seek only when the query constrains a leading run of its columns. This is the leftmost-prefix rule.
-- Equality on the first column, range on the second: the index can seek and then scan.
EXPLAIN
SELECT id, total
FROM orders
WHERE customer_id = 42
AND created_at >= '2026-01-01';
-- Filtering only on the second column cannot seek into the index,
-- because entries are sorted by customer_id first.
EXPLAIN
SELECT id, total
FROM orders
WHERE created_at >= '2026-01-01';In the first query, the descent finds (42, 2026-01-01) and the scan reads forward until customer_id changes. In the second query, matching created_at values are scattered across every customer. No single position in the tree marks where they start. The planner will usually pick a sequential scan or another index.
Choosing the column order
Use these rules as a starting point:
- Put columns tested with equality first.
- Put the column with a range condition (
>=,BETWEEN,LIKE 'abc%') after the equality columns. Columns after a range column cannot narrow the seek, because the matching entries are no longer contiguous in the later column. - Among equality columns, favor columns that your queries filter on most often. This lets one index serve several queries through its prefixes.
- Consider selectivity, which is how few rows a value matches, as a tie-breaker. It matters less than the equality-before-range rule.
The same index can serve ORDER BY too. An index on (customer_id, created_at) returns rows for one customer already sorted by created_at, so the planner can skip a separate sort step.
| Query filter | Can seek on `(customer_id, created_at)`? |
|---|---|
| `customer_id = 42` | Yes, prefix of one column |
| `customer_id = 42 AND created_at >= ...` | Yes, uses both columns |
| `created_at >= ...` only | No, skips the leading column |
| `customer_id >= 40 AND created_at = ...` | Seeks on `customer_id` only. The `created_at` test is applied as a filter inside the scanned range. |
Common errors
- Creating an index on
(created_at, customer_id)because "date is more selective", then querying by customer. The leading column is never constrained, so the index goes unused. - Adding a separate index for each prefix. An index on
(a, b)already serves queries onaalone, so a second index on(a)is usually redundant and adds write cost. - Assuming the index works for any column order in the
WHEREclause. The order you write conditions in does not matter, but the order of the index columns does.
Exercise
A query filters with WHERE status = 'open' AND created_at >= '2026-01-01'. Which index column order is better, (status, created_at) or (created_at, status), and why?
Show answer
(status, created_at). The equality column goes first, so all open entries are contiguous and sorted by created_at. The index seeks to the start date within that group and scans forward. With (created_at, status), the range on the first column spans many entries, and status can only filter them one by one.