Create indexes with included columns
Applies to:
SQL Server
Azure SQL Database
Azure SQL Managed Instance
This topic describes how to add included (or nonkey) columns to extend the functionality of nonclustered indexes in SQL Server by using SQL Server Management Studio or Transact-SQL. By including nonkey columns, you can create nonclustered indexes that cover more queries. This is because the nonkey columns have the following benefits:
- They can be data types not allowed as index key columns.
- They are not considered by the Database Engine when calculating the number of index key columns or index key size.
An index with nonkey columns can significantly improve query performance when all columns in the query are included in the index either as key or nonkey columns. Performance gains are achieved because the query optimizer can locate all the column values within the index; table or clustered index data is not accessed resulting in fewer disk I/O operations.
Note
When an index contains all the columns referenced by a query it is typically referred to as covering the query.
Before You Begin
Design Recommendations
Redesign nonclustered indexes that have a large index key size so that only columns used for searching and lookups are key columns. Make all other columns that cover the query into nonkey columns. In this way, you will have all columns needed to cover the query, but the index key itself is small and efficient.
Include nonkey columns in a nonclustered index to avoid exceeding the current index size limitations of a maximum of 32 key columns and a maximum index key size of 1,700 bytes (16 key columns and 900 bytes prior to SQL Server 2016 (13.x)). The Database Engine does not consider nonkey columns when calculating the number of index key columns or index key size.
The order of nonkey columns in the index definition does not impact the performance of queries that use the index.
Avoid very wide nonclustered indexes where the included columns don't represent a narrow enough subset of the underlying table columns. If adding wide indexes, always verify if the cost of updating one extra wide index offsets the cost of reading directly from the table.
Limitations and restrictions
Nonkey columns can only be defined on nonclustered indexes.
All data types except text, ntext, and image can be used as nonkey columns.
Computed columns that are deterministic and either precise or imprecise can be nonkey columns. For more information, see Indexes on Computed Columns.
Computed columns derived from image, ntext, and text data types can be nonkey columns as long as the computed column data type is allowed as a nonkey index column.
Nonkey columns cannot be dropped from a table unless that table's index is dropped first.
Nonkey columns cannot be changed, except to do the following:
Change the nullability of the column from NOT NULL to NULL.
Increase the length of varchar, nvarchar, or varbinary columns.
Security
Permissions
Requires ALTER permission on the table or view. User must be a member of the sysadmin fixed server role or the db_ddladmin and db_owner fixed database roles.
Using SQL Server Management Studio
To create an index with nonkey columns
In Object Explorer, click the plus sign to expand the database that contains the table on which you want to create an index with nonkey columns.
Click the plus sign to expand the Tables folder.
Click the plus sign to expand the table on which you want to create an index with nonkey columns.
Right-click the Indexes folder, point to New Index, and select Non-Clustered Index....
In the New Index dialog box, on the General page, enter the name of the new index in the Index name box.
Under the Index key columns tab, click Add....
In the Select Columns from table_name dialog box, select the check box or check boxes of the table column or columns to be added to the index.
Click OK.
Under the Included columns tab, click Add....
In the Select Columns fromtable_name dialog box, select the check box or check boxes of the table column or columns to be added to the index as nonkey columns.
Click OK.
In the New Index dialog box, click OK.
Using Transact-SQL
To create an index with nonkey columns
In Object Explorer, connect to an instance of Database Engine.
On the Standard bar, click New Query.
Copy and paste the following example into the query window and click Execute.
USE AdventureWorks2022; GO -- Creates a nonclustered index on the Person.Address table with four included (nonkey) columns. -- index key column is PostalCode and the nonkey columns are -- AddressLine1, AddressLine2, City, and StateProvinceID. CREATE NONCLUSTERED INDEX IX_Address_PostalCode ON Person.Address (PostalCode) INCLUDE (AddressLine1, AddressLine2, City, StateProvinceID); GO