I've never really understood the difference between these two indexes, can someone please explain what the difference is (performance-wise, how the index structure will look like in db, storage-wise etc)?
Included index
CREATE NONCLUSTERED INDEX IX_Address_PostalCode ON Person.Address (PostalCode) INCLUDE (AddressLine1, AddressLine2, City, StateProvinceID);
'Normal' index
CREATE NONCLUSTERED INDEX IX_Address_PostalCode ON Person.Address (PostalCode, AddressLine1, AddressLine2, City, StateProvinceID);
Index key columns are part of the b-tree of the index. Included columns are not. In the first query, index1 provides a mechanism for quickly identifying the rows of interest. The query will (probably) execute as an index seek, followed by a bookmark lookup to retrieve the full row(s).
Indexes with included columns provide the greatest benefit when covering the query. This means that the index includes all columns referenced by your query, as you can add columns with data types, number or size not allowed as index key columns.
To put it simply: yes, it does. The order of the columns matters when it comes to improving performance of queries in your SQL Server. In this post we'll be going through a few short examples and observing how different column orders behave with the same query.
The Include Clause. The include clause allows us to make a distinction between columns we would like to have in the entire index (key columns) and columns we only need in the leaf nodes ( include columns). That means it allows us to remove columns from the non-leaf nodes if we don't need them there.
The internal storage of indexes uses a B-Tree structure and consists of "index pages" (the root and all intermediate pages) and "index data pages" (the leaf pages only).
Note do not confuse "index data pages" with the "data pages" (leaf pages of clustered indexes) which store most of the columns of actual data.
INCLUDE
section, less data per index key is stored on each page.When an index is used, the index key is used to navigate through the index pages to the correct index data page.
INCLUDE
columns, that data is immediately available should the query need it.INCLUDE
columns, then an additional "bookmark lookup" is required to the correct row in the clustered index (or heap if no clustered index defined).Some things to note that hopefully addresses some of your confusion:
INCLUDE
columns).INCLUDE
columns as well.)It's worth noting that before INCLUDE
columns were added as a feature:
INCLUDE
columns basically allow the same benefit more efficiently.NB Something very important to point out. You generally get zero benefit out of
INCLUDE
columns in your indexes if you're in the lazy habit of always writing your queries asSELECT * ...
. By returning all columns you're basically ensuring a bookmark lookup is required in any case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With