Maximum Size of Index Keys

When you design an index that contains many 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 included 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 using the sys.columns catalog view.

  2. Sum the length of each column that will be defined in the index key.

    For example, the following statement aggregates the max_length column of the sys.columns catalog view for the specified columns in the Person.Address table.

    USE AdventureWorks2008R2;
    GO
    SELECT SUM(max_length)AS TotalIndexKeySize
    FROM sys.columns
    WHERE 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 such as nchar or nvarchar, 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, City is defined as an nvarchar(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

> 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

> 900 bytes

<= 900 bytes

Yes

Warning

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

<= 900 bytes

> 900 bytes

> 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, except text, ntext, and image.

Note

When tables are partitioned, if the partitioning key columns are not already present in a non-unique clustered index, they are added to the index by the Database Engine. The combined size of the indexed columns (not counting included columns), plus any added partitioning columns cannot exceed 1800 bytes in a non-unique clustered index.