SQL Server 2012 的 900 字节索引限制的总字符数限制是多少。我创建了一个包含 varchar(2000)
的列,但我认为它超出了 SQL Server 限制的 900 字节?适合 900 字节索引列的最大 varchar(?)
是多少?
最佳答案
varchar 的存储大小是输入数据的实际长度 + 2 个字节。即使列本身有 2 个字节的开销,您最多可以输入 900 byte varchar 值放入已索引的列中。
实际上,您可以在大小超过 900 字节的列上创建索引,但如果您实际上尝试插入大于 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 获得索引的一些好处。它们在索引中。
关于sql-server - 字符长度的 900 字节索引大小限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12717317/