I have created these two indexes based on how Index wizard suggested for different scenarios.
I am wondering if they are the same or are they different?
Index (1):
CREATE NONClustered Index [IX_Rec_RecoveryDate_RecoveryDateTypeKey_CaseId]
ON [Rec].[RecoveryDate] ([RecoveryDateTypeKey], [CurrentFlag])
INCLUDE (CaseId)
Index (2):
CREATE NONClustered Index [IX_Rec_RecoveryDate_currentFlag]
ON [Rec].[RecoveryDate] ([CurrentFlag])
INCLUDE (CaseId, RecoveryDateTypekey)
The question is, what queries are you trying to optimize?
These two indexes are very different. The first index would be great for a query like this:
The first one indexes the columns in the WHERE clause. SQL Server would be able to search for the specified RecoveryDateTypeKey and CurrentFlag. Since the CaseId is INCLUDEd in the index leaf nodes, SQL Server would not have to join back to the table to get it.
Using the same query, the second index would behave differently. If you are lucky, SQL Server would search for all records where CurrentFlag is 1. Then, it would traverse these leaf nodes looking for the matching RecoveryTypeKey. On the other hand, if there are a lot of records where the CurrentFlag is 1, SQL Server might choose to do an index scan instead.
Then again, if you are wanting to optimize a query like this:
The first index would be useless, because the CurrentFlag is the second column in the index. SQL Server wouldn’t be able to search it for CurrentFlag = 1, so it would probably do an index scan.