sql-server – 字符长度为900字节的索引大小限制

SQL Server 2012具有的900字节索引限制的总字符数限制是多少。我创建了一个具有varchar(2000)的列,但我认为它超过了SQL Server限制的900字节?什么是max varchar(?)以适合900字节索引列?
varchar的存储大小是输入的数据的实际长度2个字节。即使列本身有2字节开销,您可以将最多900 byte个varchar值放入索引的列中。

在实践中,您可以在大小大于900字节的列上创建索引,但如果实际尝试插入大于900字节的内容,则会出现问题:

create table test (
    col varchar(1000)
);
create index test_index on test (col);
-- Warning! The maximum key length is 900 bytes. The index 'test_index' has maximum length of 1000 bytes. For some combination of large values, the insert/update operation will fail.
insert into test select cast(replicate('x', 899) as varchar(1000)); -- Success
insert into test select cast(replicate('y', 900) as varchar(1000)); -- Success
insert into test select cast(replicate('z', 901) as varchar(1000)); -- Fail
-- Msg 1946, Level 16, State 3, Line 8
-- Operation failed. The index entry of length 901 bytes for the index 'test_index' exceeds the maximum length of 900 bytes.

请注意,900字节的限制包括给定索引键的所有列,如下例所示:

create table test (
      col varchar(1000)
    , otherCol bit -- This column will take a byte out of the index below, pun intended
);
create index test_index on test (col, otherCol);
insert into test select cast(replicate('x', 899) as varchar(1000)), 0; -- Success
insert into test select cast(replicate('y', 900) as varchar(1000)), 0; -- Fail
insert into test select cast(replicate('z', 901) as varchar(1000)), 0; -- Fail

对于通常对于索引键而言过大的列,您可以通过在索引中索引including来获得一些好处。

http://stackoverflow.com/questions/12717317/900-byte-index-size-limit-in-character-length

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:sql-server – 字符长度为900字节的索引大小限制