Suppose I cluster a table on (RetailerID, PurchaseDate, UserID
). That's the "clustering key", and clustering keys are always included in all non-clustered indexes.
https://stackoverflow.com/a/23057196/88409
https://stackoverflow.com/a/2747869/88409
Next, I create a non-clustered index "StorePurchasesIndex" keyed on (RetailerID, StoreID, PurchaseDate
), to make lookups that include just a subset of specific stores faster.
The first question is, do I need to explicitly include UserID
as an included column, or will it be there implicitly by virtue of the clustering key including it? I'm pretty sure I do NOT need to include UserID
explicitly in this case, but correct me if I'm wrong.
What I'm really interested in knowing is what happens if I do explicitly include UserID
as an include column. Will it be included in the index redundantly, once as part of the clustering key, and again as an included column? Or does SQL Server recognize the intent and avoid storing it twice since it's already included by virtue of the clustering key?
The second question is, if it's not included redundant, then is there a benefit to including it explicitly. For example, will it ensure UserID
is included in the future, even if the clustering key changes in such a way that it excludes UserID
and the index is rebuilt?