SQL Server 建立索引的限制

來源:互聯網
上載者:User

今天準備在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.

  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. 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 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.

 See Also

Concepts

General Index Design Guidelines
Index 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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.