Given a database table example:
ID
Name
DateOfBirth
Say I wanted to create an index on date of birth, I might add the index on DateOfBirth ascending. If my code now queries on DOB ascending, it will run nice and fast.
However, are databases intelligent enough to use the index in reverse, i.e, if I queried orderby DateOfBirth descending would it still be able to utilise that index, or should I create another one specifically for descending ordering?
The index pages form a doubly linked list as shown below with pointers to both the next and previous page.
Thus SQL Server can traverse the index both backwards and forwards. The Scan direction is shown in the properties of the execution plan.
Where the
Asc/Descbecomes important is in composite indexes.If the index was defined as
ID ASC, Name ASC, DateOfBirth ASCthis would still require a sort for the queryORDER BY ID ASC, Name DESC, DateOfBirth ASCfor example.Note that you may have more composite indexes than you realise as well as a non unique non clustered index will have the clustered index key value added to its key.