今天准备在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,
解释如下:
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 themax_lengthcolumn of thesys.columnscatalog view for the specified columns in thePerson.Addresstable.
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. - 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.
| 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.
在SQL Server中创建索引时,遇到900字节的键长度限制。当包含多个或大尺寸列时,需确保不超过最大索引键大小。警告提示可能在插入/更新操作时因键值超过900字节而失败。可以考虑使用非聚集索引的INCLUDE子句来包含非键列,以避免键列大小限制。
2688

被折叠的 条评论
为什么被折叠?



