【问题标题】:900 byte index size limit in character length字符长度的 900 字节索引大小限制
【发布时间】:2012-09-24 21:38:45
【问题描述】:

SQL Server 2012 的 900 字节索引限制的总字符数限制是多少。我创建了一个包含varchar(2000) 的列,但我认为它超过了 SQL Server 限制的 900 字节?适合 900 字节索引列的最大 varchar(?) 是多少?

【问题讨论】:

  • 可能取决于您的表格使用的字符集
  • 我用过的最小的字符集每个字符有 1 个字节(Latin-8859?)。猜你的至少有那么大。
  • 这是一个 SQL Server 2012 64 位软件。字符集没什么特别的。开箱即用运行 SQL Server 2012 64 位的 Windows 8 64 位。美式英语 Windows。

标签: sql-server indexing sql-server-2012


【解决方案1】:

对于那些在 SQLServer 2016 上的,索引键大小增加到 1700 字节..What's new in Database Engine - SQL Server 2016

NONCLUSTERED 索引的最大索引键大小已增加到 1700 字节。

演示:

create table test
(
id varchar(800),
id1 varchar(900)
)

insert into test
select replicate('a',800),replicate('b',900)

create index nci on test(id,id1)

【讨论】:

    【解决方案2】:

    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 对它们进行索引来获得一些好处。

    【讨论】:

      【解决方案3】:

      在相关说明中,您可以尝试的另一个选项,即在宽列上获取索引,在 http://www.brentozar.com/archive/2013/05/indexing-wide-keys-in-sql-server/ 处进行了概述,其中哈希列被添加到表中,然后被索引并在您的查询中使用。

      【讨论】:

      • 我使用HASHBYTE 功能效果很好,谢谢! OP 正在讨论 SQLServer2012,但请注意 SHA2_512 算法仅在 SQLServer2012 中引入,因此如果您使用的是早期版本,则必须使用不同的算法,因为为早期版本指定 SHA2_512 只会返回 null!这是docs for 2008。例如:select HASHBYTES('SHA2_512', 'The quick brown fox') as sha2_512, HASHBYTES('MD5', 'The quick brown fox') as md5, HASHBYTES('SHA1', 'The quick brown fox') as sha1.
      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多