What is a composite index, and does column order matter?
Quick Answer
A composite index covers multiple columns in a defined order. Order matters a lot: the index is only useful for queries that filter on a leading prefix of its columns. General rule — put equality-predicate columns first (most selective first), then range columns, then columns only needed for output as INCLUDE.
A composite index spans several columns in a set order, and that order is critical because the index only helps queries that filter on a leading prefix of it. An index on (CustomerId, OrderDate) is great for filtering by customer, or customer plus date, but useless for filtering by date alone. The design rule is: equality columns first, most selective first, then a range column, and columns you only need in the output go in the INCLUDE list so they don't widen the key.
- An index on
(A, B, C)helps queries filtering onA, onA, B, or onA, B, C— but not a query filtering only onBorC. WHERE A = @a AND B > @b— index(A, B)seeks onAthen ranges onB. Reversing to(B, A)loses the seek.- A range column mid-index “stops” the seek — columns after it can only filter within the range, not seek.
- Use
INCLUDEfor columns you onlySELECT, so the index covers the query without bloating the key.