Sub topic 7
Composite Indexes and the Leftmost-Prefix Rule
Prerequisites and the main idea
This chapter assumes you know SELECT, WHERE, ORDER BY, tables, rows, columns, primary keys, and the difference between linear and logarithmic work. It also assumes PostgreSQL-style SQL.
A composite index is an index built from two or more columns. For example:
CREATE INDEX orders_customer_created_idx
ON orders (customer_id, created_at);This index is ordered first by customer_id, then by created_at within each customer. The column order is part of the index's meaning.
The leftmost-prefix rule says that a composite B-tree index is most useful when a query constrains the index's leading columns. For (customer_id, created_at), the useful prefixes are:
customer_idcustomer_id, created_at
A query that uses only created_at skips the first column, so the database usually cannot seek efficiently through this index.
Which queries can use the index?
Assume the table contains orders and the index is (customer_id, created_at).
| Query condition | Efficient use of the index? | Reason |
|---|---|---|
| `customer_id = 42` | Yes | Uses the first column |
| `customer_id = 42 AND created_at >= ...` | Yes | Uses the first column and then a range |
| `customer_id = 42 AND status = 'open'` | Yes, partly | Seeks by `customer_id`; filters `status` separately |
| `created_at >= ...` | Usually no | Skips the leading column |
| `customer_id IN (1, 2) AND created_at < ...` | Often yes | Searches several leading-column ranges |
A range condition usually ends the most precise part of the index search. For example, with (customer_id, created_at), the database can find one customer and then scan that customer's date range. A later indexed column may still help with filtering or ordering, but it cannot usually narrow the search as precisely as equality columns before the range.
-- Efficient for the composite index.
SELECT *
FROM orders
WHERE customer_id = 42
AND created_at >= '2026-01-01'
AND created_at < '2026-02-01';
-- The index does not provide an efficient seek by created_at alone.
SELECT *
FROM orders
WHERE created_at >= '2026-01-01';Visualizing the ordering
Designing and checking one
Start with the query workload, not with individual columns. If users commonly request one customer's recent orders, (customer_id, created_at) supports both the customer lookup and the date ordering:
CREATE INDEX orders_customer_created_idx
ON orders (customer_id, created_at);
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC
LIMIT 20;EXPLAIN shows the plan chosen by the query planner. Look for an index scan or an index-only scan using orders_customer_created_idx. The actual choice depends on table size, statistics, estimated matching rows, and the cost of fetching table rows.
Exercise
You have an index on (tenantid, status, createdat). Which query best matches its leftmost prefix: (A) WHERE createdat >= '2026-01-01', (B) WHERE status = 'open' AND createdat >= '2026-01-01', or (C) WHERE tenantid = 9 AND status = 'open' AND createdat >= '2026-01-01'?
Show answer
C. It constrains the leading columns in order, allowing the index to narrow the search by tenant, then status, then date.