Composite Indexes and the Leftmost Prefix Rule
How multi-column indexes work, the leftmost prefix rule, choosing column order (equality first, then range), and composite indexes for ORDER BY.
Published November 27, 2026
A composite index (also called a multi-column or concatenated index) covers several columns at once, for example (customer_id, status, created_at). Used well, one composite index can replace three single-column indexes and make filtering and sorting nearly free. Used badly, the database ignores it. The difference comes down to column order and the leftmost prefix rule.
The concept
A composite B-tree index is sorted by the first column, then by the second column within equal values of the first, then by the third, like a phone book sorted by surname, then first name. That ordering means the index can efficiently answer queries that constrain a leading prefix of its columns: (a), (a, b) or (a, b, c), but not (b) or (c) alone, because rows with the same b are scattered throughout the index.
Column order matters in a second way. Once the database hits a range condition (>, <, BETWEEN, LIKE 'abc%') on a column, it can still use that column to narrow the scan, but columns after it can no longer be used for seeking, only for filtering inside the scanned range. The usual rule of thumb is: equality columns first, then the range or sort column.
The leftmost prefix rule
| Query WHERE clause | Uses index (a, b, c)? | Why |
|---|---|---|
| a = 1 | Yes | Leading column |
| a = 1 AND b = 2 | Yes | Leading prefix (a, b) |
| a = 1 AND b = 2 AND c > 5 | Yes, fully | Equality on a, b then range on c |
| a = 1 AND c = 3 | Partly | Seek on a; c only filters (b is skipped) |
| b = 2 | No (usually) | Not a leftmost prefix (skip scan aside) |
| a > 1 AND b = 2 | Partly | Range on a stops b from being used for seeking |
How entries are ordered
Indexes that serve ORDER BY too
If the equality columns come first and the ORDER BY column follows, the database reads rows already in order and can stop after LIMIT rows. No sort step, no reading the whole result set. This is one of the most valuable patterns for paginated lists.
CREATE INDEX idx_orders_cust_status_created ON orders (customer_id, status, created_at DESC);
SELECT id, total, created_at
FROM orders
WHERE customer_id = 42 AND status = 'paid'
ORDER BY created_at DESC
LIMIT 20; -- index seek + read 20 entries, no filesortChoosing column order
- Columns compared with = go first
- Among them, order rarely matters for seeks; consider reuse by other queries
- Then the single range or ORDER BY column
- Optionally append columns only selected (to make it covering)
- Check EXPLAIN for "Using filesort" or a separate Sort node
Practical examples
One composite index replaces several
(a, b) also serves queries on a alone, so a separate index on a is redundant.
-- Redundant: idx_orders_customer (customer_id) when you have:
CREATE INDEX idx_orders_customer_created ON orders (customer_id, created_at);
DROP INDEX idx_orders_customer ON orders; -- MySQL syntaxRange in the middle breaks the rest
Put the range column last.
-- Slower: range on created_at prevents seeking on status
CREATE INDEX bad ON orders (customer_id, created_at, status);
-- Better for WHERE customer_id = ? AND status = ? AND created_at > ?
CREATE INDEX good ON orders (customer_id, status, created_at);Check with EXPLAIN in MySQL
key_len shows how many index columns were used for the seek.
EXPLAIN SELECT * FROM orders WHERE customer_id = 42 AND status = 'paid' AND created_at > '2026-01-01';
-- key: good key_len: 8+82+6 type: range Extra: Using index conditionCommon mistakes
- Creating separate single-column indexes on a, b and c and expecting them to combine as well as one composite index.
- Putting a range column before equality columns.
- Ordering columns by "most selective first" blindly; query patterns matter more than selectivity alone.
- Duplicating indexes that are prefixes of other indexes.
- Mixing sort directions (ORDER BY a ASC, b DESC) without a matching index definition on databases that need it.
Key takeaways
- Composite indexes are sorted by column 1, then 2, then 3.
- Queries must constrain a leftmost prefix to seek efficiently.
- Equality columns first, then one range or ORDER BY column.
- A good composite index can also eliminate sorting for paginated queries.
FAQ
What is an index skip scan?
Some databases (Oracle, MySQL 8.0.13+, PostgreSQL 18) can use an index without its leading column by jumping between distinct leading values. It helps when the leading column has few distinct values, but do not design around it.
Can the database combine two single-column indexes?
Yes, via index merge (MySQL) or bitmap index scans (PostgreSQL), but a well-ordered composite index is usually faster.
How many columns can a composite index have?
MySQL allows 16 and PostgreSQL 32 by default, but more than 3-5 columns is rarely useful.