Most real queries filter on several columns, so most useful indexes cover several columns. The order of those columns is the single most important design decision, and it follows directly from how the B+Tree is sorted.
How a composite index is ordered
An index on (a, b, c) is sorted by a first. Rows with the same a are sorted by b, and rows with the same a and b are sorted by c. Think of a phone book sorted by last name, then first name, then city. You can find all the people called Nguyen quickly, and all the people called Nguyen Phat, but you cannot find everyone called Phat without reading the whole book.
The leftmost prefix rule
A query can use the index to seek only on a leftmost prefix of its columns, and only while the conditions are equalities.
a = 1: usesa.a = 1 AND b = 2: usesaandb.a = 1 AND b = 2 AND c = 3: uses all three.b = 2,c = 3orb = 2 AND c = 3: cannot seek, because the leading column is missing.a = 1 AND c = 3: seeks ona;cis checked on the entries found, possibly pushed down into the index scan by index condition pushdown, but it does not narrow the seek.a = 1 AND b > 5 AND c = 3: seeks onaand the range onb; after a range condition, later columns cannot be used for seeking.
MySQL 8 can sometimes use a feature called skip scan to handle a missing leading column when it has very few distinct values, but treat that as a bonus, not a design tool.
Equality, then range, then sort
This gives a practical rule for ordering columns: put the columns compared with equality first, then the column used for a range, and the column you sort by last. Within equality columns, put the most selective first only if other queries benefit; the rule above matters more.
-- common query: latest payments of one wallet in a status
SELECT id, amount, created_at
FROM payments
WHERE wallet_id = 7 AND status = 'SUCCESS'
ORDER BY created_at DESC
LIMIT 20;
CREATE INDEX idx_wallet_status_created ON payments (wallet_id, status, created_at);With this index MySQL seeks directly to wallet_id = 7 and status = 'SUCCESS', and the entries are already sorted by created_at, so it reads the last 20 in reverse and stops. No sort step is needed.
When ORDER BY still needs a sort
An index delivers sorted output only when the preceding columns are fixed by equality. Two common ways to lose that:
status IN ('SUCCESS', 'PENDING')is treated as a range over two groups. The entries come back sorted bycreated_atwithin each status, not across both, so MySQL adds aUsing filesortstep. Run one query per status and merge, or choose an index that does not need the filter.- A range condition in the middle, such as
created_at > ?with anORDER BY amount, cannot be sorted by an index whose next column isamount.
Use EXPLAIN to confirm. Using filesort in Extra means MySQL sorted the rows itself. It is not always slow, but on a large result with LIMIT it is usually the thing to remove.
Cardinality, redundancy and cost
- A column with very few distinct values, such as a boolean, is rarely worth indexing alone. It can still be useful as a trailing or leading column of a composite index when queries always filter on it.
(a, b)already serves queries ona, so a separate index onais redundant. Drop it to save space and write cost.- Every index slows inserts, updates and deletes and uses memory. Add an index for a query that matters, measure it, and remove indexes that nothing uses.
Takeaway
A composite index is a sorted list with a fixed column order. Queries can seek on its leftmost columns while they use equality, one range stops the seek, and sorting works only when the earlier columns are pinned. Design the order as equality, then range, then sort, and verify the plan with EXPLAIN.
Comments