What does INCLUDE in an unclustered index?
CREATE NONCLUSTERED INDEX [MyIndex] ON [dbo].[Individual]
(
[IndivID] ASC
)
INCLUDE ( [LastName], [FirstName])
WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY]
I know the first part is used for the WHERE clause, but what do the INCLUDE columns do? What’s the benefit of having them “added to the leaf level of the nonclustered index”?
edit Also if i already have a clustered PK index for IndivID, why does Tuning Advisor recommend this index?
INCLUDE columns include the associated fields WITH the index. They are not used FOR indexing, but they are placed in the leaf node of the B-tree that makes up the index.
In essence: The index is still ON [IndivID] and [IndivID] alone. However, if your query only needs a subset of [IndivID], [LastName] and [FirstName], SQL doesn’t need to go back to the table after it’s found the [IndivID] it’s searching for in the Index.
SEE: Covering Index
EDIT: B-tree assumes MS SQL Server. I’m not positive other implementations use the same data structure
Tuning Advisor (Speculation):: A clustered index places the entire data row at the leaf node of the index’s B-tree, and this takes up a lot of space. If the Tuning Advisor sees that you’re never accessing more than those three fields ([IndivID] + INCLUDEs), it will attempt to save you space (and insert/update time) by downgrading it to a non-clustered index with the only "important" fields present.