
Column Order in a Composite Index Decides Which Queries It Helps
- كاتب موثّق
محتويات المقال
(CustomerId, CreatedAt) and became instant. A month later, the operations dashboard that lists the orders placed on one day is slow, even though its execution plan shows that same index being used. In a test on SQL Server 2022 with 1,000,000 orders, the customer's query read 3 pages. The dashboard's query read 2,507: the whole index, from the first entry to the last.
The common misconception
(A, B) indexes both columns, so it helps any query that filters on A, on B, or on both.
What actually happens
Sorted by the first column, then the second
LastName, FirstName helps WHERE LastName = 'Smith', but not a query that searches only on FirstName.
A seek needs a prefix
(CustomerId, CreatedAt), the seek goes to that customer and that date and reads 9 entries, for 3 pages. With (CreatedAt, CustomerId), the seek starts at the date and reads every order placed after it, checking CustomerId on each: 1,190 pages for the same 9 rows. When both conditions are equalities, the order doesn't matter for that query: an exact match on both columns took 3 reads with either index. What decides the order then is which other queries the index must also serve.
The order also serves ORDER BY
WHERE CustomerId = @c ORDER BY CreatedAt DESC with TOP (10) read 3 pages on (CustomerId, CreatedAt), with no sort step. On (CreatedAt, CustomerId) it scanned backward through all recent orders until it had found 10 of that customer: 1,200 pages. PostgreSQL's docs add a detail for mixed directions: a plain (x, y) index can't produce ORDER BY x ASC, y DESC, but one defined as (x ASC, y DESC) can.
PostgreSQL 18's skip scan is narrower than it sounds
(x, y) and WHERE y = 7700, it can run one search per value of x. The docs are clear about when that pays off: only when x has so few distinct values that most of the index can be skipped. With many distinct values, the whole index has to be read, and the planner usually prefers a sequential scan of the table. A CustomerId with 50,000 values is the case it doesn't fix.
(CustomerId, CreatedAt) | (CreatedAt, CustomerId) | |
|---|---|---|
CustomerId = @c | ||
CustomerId = @c AND CreatedAt >= @since | ||
CustomerId = @c AND CreatedAt = @t | ||
CreatedAt | ||
CustomerId = @c, newest 10 |
One index per leading column you filter on
-- One index, expected to serve both screensCREATE INDEX IX_Orders_Customer_Created ON dbo.Orders (CustomerId, CreatedAt);-- Dashboard: the orders placed on one day.-- Tested: Index Scan of the whole index, 2,507 readsSELECT Id, CustomerId FROM dbo.OrdersWHERE CreatedAt >= @day AND CreatedAt < DATEADD(day, 1, @day);-- Equality column first, range second: serves "my orders"CREATE INDEX IX_Orders_Customer_Created ON dbo.Orders (CustomerId, CreatedAt);-- The dashboard filters on CreatedAt alone,-- so it needs an index that starts with CreatedAtCREATE INDEX IX_Orders_Created_Customer ON dbo.Orders (CreatedAt, CustomerId);-- Tested: dashboard query becomes an Index Seek, 7 reads(CustomerId) alone is usually redundant next to (CustomerId, CreatedAt), since any seek it can do, the wider index can do too.
How to apply it in your project
List the query shapes per table For each frequent query, write down which columns it compares with =, which one with a range, and what it sorts by.Order the key columns Put the equality columns first and the one range or sort column after them. Columns compared only for equality can go in any order, so pick the one that also lets other queries seek. Check the plan for seeks Run each query shape and confirm an Index Seek whose SeekPredicatescover the columns you expect, not an Index Scan with aPredicate.Remove redundant prefixes An index whose columns are a leading prefix of another index, such as (CustomerId)next to(CustomerId, CreatedAt), usually adds write cost without making any read faster.
Key takeaways
A composite index is sorted by its first column, then the next, so it can seek only through a prefix of its columns. A filter on the second column alone reads the whole index: 2,507 pages instead of 7 in the test, even though the plan named the index. Index for equality first, then for ranges: the same 9 rows cost 3 reads with (CustomerId, CreatedAt)and 1,190 with the reverse.Column order also decides whether ORDER BYwithTOPcan stop early or must scan.PostgreSQL 18's skip scan helps only when the leading column has few distinct values; it doesn't replace a correctly ordered index.
التعليقات (0)