Transferred from: http://www.cnblogs.com/chenmh/p/3785285.html
Include column index
Overview
The Include column index is also a nonclustered index, and the index structure is the same as the clustered index structure, where the non-key column containing the column index is stored only on the leaf node, the columns that contain the column index are the key and nonkey columns, the so-called nonkey columns are the columns included in the include, and at least one key column is required. And the key and nonkey columns do not allow duplicates, the non-key column allows a maximum of 1023 columns (that is, the table's maximum column-1), because the index key columns (excluding non-keys) must adhere to the existing index size limit (the maximum number of key columns is 16, the total index key size is 900 bytes) The Inclusion column index is introduced
Body
- Create a include column index
----CREATE table creation table [dbo]. [Customers] ( [CustID] [int] IDENTITY () not NULL, [CompanyName] [nvarchar] (+) not NULL, [ContactName] [nvarchar] ( () not NULL, [contacttitle] [nvarchar] (+) not NULL, CONSTRAINT [pk_customers] PRIMARY KEY CLUSTERED ( [CustID] ASC) with (pad_index = off, statistics_norecompute = off, Ignore_dup_key = off, allow_row_locks = ON, allow_p Age_locks = on) on [PRIMARY]) on [PRIMARY]----Create the Include column index creates nonclustered index [ix1_customers] on [dbo]. [Customers] ([ CompanyName] ASC) INCLUDE ([ContactName])
With (Statistics_norecompute = off, sort_in_tempdb = off, Ignore_dup_key = off, drop_existing = off, ONLINE = off, AL Low_row_locks = on, allow_page_locks = on) on [Primary]go
The key column here is: CompanyName
Non-key columns are: ContactName
Non-key columns have the following advantages:
- They can be data types that are not allowed as index key columns.
- The database engine does not consider them when calculating the number of index key columns or index key sizes.
Indexes with inclusive nonkey columns can significantly improve query performance when all columns in the query are included as key or nonkey columns in the index. This enables performance gains because the query optimizer can find all column values in the index, and does not access table or clustered index data, thereby reducing disk I/O operations. (When an index contains all the columns referenced by a query, it is often referred to as an "overwrite query.") )
- Create an overwrite query
Overriding a query is when you create an indexed column that contains all the columns that the query references
- Query columns are set as key columns
When our select query is such a select [CompanyName] , [ContactName] , [ContactTitle] from [chenmh].[ DBO]. [Customers] Where companyname= ' good boy ' ---at this time we chose to include the index column in the index build column CREATE nonclustered index [ix2_customers] on [dbo].[ Customers] ( [CompanyName] ASC , [ContactName] ASC , [ContactTitle] ASC) with (Statistics_norecompute = off, sort_in_tempdb = off, Ignore_dup_key = off, drop_existing = off, ONLINE = off, allow_row_locks = ON, all Ow_page_locks = on) on [Primary]go
A warning will pop up: Warning! The maximum key length is 900 bytes. The maximum length of index ' ix2_customers ' is 940 bytes. For some large value combinations, the Insert/update operation will fail.
Since three fields are nvarchar field type, each character needs 2 bytes, (40+30+400) *2=940 bytes, greater than 900 bytes, we can then [ContactName], [ContactTitle] contained in non-key column
2. Set the big data type to a non-key column
CREATE nonclustered INDEX [ix3_customers] on [dbo]. [Customers] ( [CompanyName] ASC ) INCLUDE ([ContactName] , [ContactTitle]) with (Statistics_norecompute = OFF, sort_in_tempdb = off, Ignore_dup_key = off, drop_existing = off, ONLINE = off, allow_row_locks = ON, allow_page_locks< C8/>= on) on [Primary]go
At this point, the index key size of the character is only 40*2=80 bytes, and the index is also an overlay index, the index column contains the columns used by the query, when we query the data directly in the index page to find data, do not need to access the data page, reduce disk IO, improve performance
index criteria with included columns
When designing a nonclustered index with a containing column, consider the following guidelines:
-
- Defines a nonkey column in the INCLUDE clause of the CREATE INDEX statement.
- You can define non-key columns only for nonclustered indexes on a table or indexed view.
- All data types are allowed except text,ntext , and image .
- Deterministic computed columns that are precise or imprecise can be include columns. For more information, see Create an index on a computed column.
- As with key columns, computed columns derived from the image,ntext , and text data types can be used as non-key (inclusive) columns as long as the computed column data type is allowed as a non-key index column.
- You cannot specify a column name in both the INCLUDE list and the key column list.
- Column names in the INCLUDE list cannot be duplicated.
Column Size Guidelines
-
- At least one key column must be defined. The maximum number of non-key columns is 1023 columns. That is, the maximum number of table columns minus 1.
- Index key columns (excluding non-keys) must adhere to the existing index size limit (the maximum number of key columns is 16 and the total index key size is 900 bytes).
- The total size of all nonkey columns is limited only by the size of the column specified in the INCLUDE clause, for example, thevarchar (max) column is limited to 2 GB.
Column Modification Guidelines
When you modify a table column that is already defined as a containing column, you are constrained by the following:
-
- Non-key columns cannot be removed from the table unless the index is deleted first.
- You cannot make other changes to non-key columns except for the following changes:
- Changes the nullability of a column from NOT NULL to NULL.
- Increase the length of the varchar,nvarchar , or varbinary columns.
Precautions
- The size of the key column is as small as possible, with improved efficiency
- The column that will be used for search and lookup is the key column, and the key column should try not to include unnecessary columns. (for example, the above-established coverage query column, although Companyname+contactname Plus as a key column will not exceed 900 bytes, but the key size is larger, reducing the query efficiency)
- Avoid adding unnecessary columns. Adding too many indexed columns (key columns or nonkey columns) has the following effects on performance:
- Fewer index rows can be accommodated on a page. This increases the I/O and reduces cache efficiency.
- More disk space is required to store the index. In particular, adding varchar (max),nvarchar (max),varbinary ( max), or XML data types as Nonkey index columns can significantly increase disk space requirements. This is because the column values are copied to the index leaf level. Therefore, they reside both in the index and in the base table.
- Index maintenance may increase the time that is required to perform modifications, inserts, updates, or deletions on the underlying table or indexed view
Summarize
If you feel that the article is helpful to you, the activity activity your finger trouble to give a recommendation, this also is to me a kind of encouragement, here to express thanks.
SQL Server Index and table Architecture (iii)