Today I want to leave a short note on include section of SQL queries. Imagine that you see SQL Query similar to this:
CREATE NONCLUSTERED INDEX [tt_CSAttributeGroup] ON [dbo].[CSAttributeGroup] ([CompanyID], [EntityClassID], [EntityType], [IsActive]) INCLUDE ([AttributeCategory], [CreatedByID], [CreatedByScreenID], [CreatedDateTime], [DefaultValue], [LastModifiedByID]) WITH (ONLINE = ON)
And while part in the first brackets is clear, then second part Include is not very clear from the first point of view.
What is difference between index which has INCLUDE section and index which doesn't have INCLUDE section?
Include section means, that values will be stored with the key itself, but will not be part of the key.
In case of presented SQL query, which is used by Acumatica, key values will be created based on CompanyID, EntityClassID, EntityType, IsActive columns. They will be used for building nodes of the tree. And also each node of the tree besides having Key calculated will also have those columns: AttributeCategory, CreatedByID, CreatedByScreenID, CreatedDateTime, DefaultValue, LastModifiedByID .
How this fact may be used later?
Once SQL server will search for some field, based on CompanyID, EntityClassID, EntityType, IsActive columns in it's query, and will find them, SQL will not need to go to the data itself for columns AttributeCategory, CreatedByID, CreatedByScreenID, CreatedDateTime, DefaultValue, LastModifiedByID. But will read values of those columns from the tree itself.
If to go further with illustration, then at storage level B+tree structure without include section will look like this:
but with include section it will look like this:
If to compare indexes with INCLUDE and indexes without INCLUDE section, you can make following conclusion: indexes with include section will make faster reading from database. But they will cause longer insert/update operations, as each change will cause copy/paste overhead. And if you have a lot of columns included, then each insert/update will have double loading on your system.