42

SQLServer2012の900バイトのインデックス制限の合計文字数制限はいくつですか。を含む列を作成しましたがvarchar(2000)、SQL Serverが制限した900バイトを超えていると思いますか?varchar(?)900バイトのインデックス列内に収まる最大値はどれくらいですか?

4

3 に答える 3

53

varcharのストレージサイズは、入力されたデータの実際の長さ+2バイトです。列自体には2バイトのオーバーヘッドがありますが、インデックスが付けられた列に最大900バイトの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

通常はインデックスキーに対して大きすぎるこれらの列の場合、それらをインデックスに含めることで、インデックス作成の利点を得ることができる場合があります。

于 2012-10-03T21:53:28.653 に答える
9

関連するメモとして、幅の広い列のインデックスを取得するために試すことができる別のオプションの概要がhttp://www.brentozar.com/archive/2013/05/indexing-wide-keys-in-sql-serverにあります。 /ここで、ハッシュ列がテーブルに追加され、インデックスが作成されてクエリで使用されます。

于 2013-12-11T16:11:08.570 に答える
6

SQLServer 2016のユーザーの場合、インデックスキーのサイズが1700バイトに増加しました。データベースエンジンの新機能-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)
于 2018-02-22T13:14:33.507 に答える