SQL Server 创建索引的限制
2009-04-09 11:10
337 查看
今天准备在SQL Server2005的一个表的字段上间索引,结果创建失败。检查一下表结构,原来该字段是NVarchar(Max), 不能建索引。
后来将该字段改成nvarchar(4000),重新执行创建语句,出现如下提示:
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.
查询MSDN,
解释如下:
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.
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.
Sum the length of each column that will be defined in the index key.
For example, the following statement aggregates the
Copy Code
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.
* 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.
See Also
Index with Included Columns
Index Design Basics
sys.columns (Transact-SQL)
sys.indexes (Transact-SQL)
sys.index_columns (Transact-SQL)
后来将该字段改成nvarchar(4000),重新执行创建语句,出现如下提示:
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.
查询MSDN,
解释如下:
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.
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.
Sum the length of each column that will be defined in the index key.
For example, the following statement aggregates the
max_
lengthcolumn of the
sys.columnscatalog view for the specified columns in the
Person.Addresstable.
Copy Code
USE AdventureWorks; 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, Cityis defined as an nvarchar(30) data type; therefore, the storage length of the column is 60. |
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. |
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.
See Also
Concepts
General Index Design GuidelinesIndex with Included Columns
Index Design Basics
Other Resources
CREATE INDEX (Transact-SQL)sys.columns (Transact-SQL)
sys.indexes (Transact-SQL)
sys.index_columns (Transact-SQL)
相关文章推荐
- SQL Server创建索引
- SQL Server 查询性能优化——创建索引原则(一)
- SQL Server创建复合索引时,复合索引列顺序对查询的性能影响
- SQL Server的聚集索引和非聚集索引的的创建和区别以及相关问题
- SQL Server:创建索引视图
- Sql Server查询性能优化之创建合理的索引(下篇)[转]
- SQL Server表的创建及索引的控制
- SQL Server 查询性能优化——创建索引原则(一)
- SQL Server 查询性能优化——创建索引原则(一)
- SQL Server:创建索引视图
- SQL Server:创建索引视图
- SQL Server 查询性能优化——创建索引原则(二)
- Sql Server查询性能优化之创建合理的索引(下篇)
- SQL Server以Online模式创建索引
- SQL Server: 创建 XML 索引 提升查询效率
- SQL Server创建索引
- SQL Server查询性能优化——创建索引原则
- SQL Server创建索引
- SQL Server如何识别真实和自动创建的索引
- SQL Server 查询性能优化——创建索引原则(二)