The Database Engine Tuning Advisor has finally given up the ghost and can’t help me any more, so I’m having to learn a little bit more about indexes (shouldn’t it be indices?).
I think I’m more or less there. I know when to use composite indexes, what to include with my indexes, the difference between clustered and non-clustered indexes etc.
However… One thing still confuses me. When creating an index, there is a sort order to the index. I understand what this means, but I am struggling to think of a scenario where a reverse order index might be useful. My best guess is to speed up queries that retrieve rows that occur at the end of the forward sorted index, such as the most chronologically recent rows, but frankly, I’m more or less clueless.
Can anyone enlighten me?
The sort order of an index matters only for a multi-column index. For a single column, Sql Sever can just use the index in reverse order, if it wants DESC where the index is ASC.
For a multi-column search, the index sorting does matter. Say you have an index on:
This would be useful for this query:
And for this query, where the index can be used in reverse:
But for this query, Sql Server would need an additional in-memory sort: