SQL Server Index creation restrictions

Source: Internet
Author: User

I am going to index the fields of a table in SQL server2005 today. The creation fails. Check the table structure. The original field is nvarchar (max) and cannot be indexed.

 

Later, change this field to nvarchar (4000) and re-execute the create statement. The following prompt is displayed:

Warning! The maximum key length is 900 bytes. The index 'ix _ project_label 'has maximum length of 8000 bytes. For some combination of large values, the insert/update operation will fail.

 

Query msdn,

Explanation:

 

Maximum size of index keys

When you design an index that contains has key columns, or large-size columns, calculate the size of the index key to make sure that you do not exceed the maximum index key size. SQL Server retains the 900-byte limit for the maximum total size of all index key columns. this excludes nonkey columns that are stored in the definition of nonclustered indexes.

Calculating the size of an index key

To calculate the size of an index key, follow these steps.

  1. Display the properties of the table columns on which the index will be based. You can do this by usingSYS. ColumnsCatalog view.
  2. Sum the length of each column that will be defined in the index key.
    For example, the following statement aggregatesmax_lengthColumn ofsys.columnsCatalog view for the specified columns inPerson.AddressTable. Copy code
    USE AdventureWorks;GOSELECT SUM(max_length)AS TotalIndexKeySizeFROM sys.columnsWHERE name IN (N'AddressLine1', N'AddressLine2', N'City', N'StateProvinceID', N'PostalCode')AND object_id = OBJECT_ID(N'Person.Address');
    Note:
    If a table column is a unicode data type suchNcharOrNvarchar, The column length displayed is the storage length of the column. This is two times the number of characters specified in the create table statement. In the previous example,CityIs defined asNvarchar (30)Data Type; therefore, the storage length of the column is 60.

  3. If the total length is less than 900 bytes, the columns can participate as index key columns. If the total length exceeds 900 bytes, review the following information for options and additional considerations.
    The create index statement uses the following algorithms to calculate the index key size:
    • If the size of all fixed key columns plus the maximum size of all variable key columns specified in the create index statement is less than 900 bytes, the create index statement finishes successfully without warnings or errors.
    • If the size of all fixed key columns plus the maximum size of all variable key columns exceeds 900, but the size of all fixed key columns plus the minimum size of the variable key columns is less than 900, the create index statement succeeds with a warning that a subsequent insert or update statement may fail if it specifies values that generates a key value larger than 900 bytes. the create index statement fails when existing data rows in the table have values that generate a key larger than 900 bytes. A subsequent insert or update statement that specifies data values that generates a key value longer than 900 bytes fails.
    • If the size of all fixed key columns plus the minimum size of all variable columns specified in the create index statement exceeds 900 bytes, the create index statement fails.

    The following table summarizes the results of creating indexes that meet or exceed the maximum index key size restrictions.

Minimum Size of variable-length column (s) + size of the fixed-data column (s) Maximum size of variable-length column (s) + size of the fixed-data column (s) Max of the sum of the index key column lengths for existing rows * Index is created Message Type Insert or update run-time error caused by oversized index key value

& Gt; 900 bytes

Not relevant

Not relevant

No

Error

No index present to generate error.

<= 900 bytes

<= 900 bytes

Not relevant

Yes

None

No.

<= 900 bytes

& Gt; 900 bytes

<= 900 bytes

Yes

Warning

Only if the sum of current lengths of all index columns is greater than 900 bytes.

<= 900 bytes

& Gt; 900 bytes

& Gt; 900 bytes

No

Error

No index present to generate error.

* None of the rows in the table at time the create index statement is executed can have index key values whose total lengths exceed 900 bytes.

Using included columns to avoid size limits

You can include nonkey columns in a nonclustered index to avoid the current index size limitations of a maximum of 16 key columns and a maximum index key size of 900 bytes. the SQL Server database engine does not consider nonkey columns when calculating the number of index key columns or the total size of the index key columns. in a nonclustered index with included columns, the total size of the index key columns is restricted to 900 bytes. the total size of all nonkey columns is limited only by the size of the columns specified in the include clause; for example,Varchar (max)Columns are limited to 2 GB. the columns in the include clause can be of all data types, limit tText,Ntext, AndImage.

See also

Concepts

General Index Design Guidelines
Index with sorted ded Columns
Index Design Basics
Other resources

Create index (TRANSACT-SQL)
SYS. Columns (TRANSACT-SQL)
SYS. indexes (TRANSACT-SQL)
SYS. index_columns (TRANSACT-SQL)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.