Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

limite de tamanho de índice de 900 bytes em comprimento de caractere


O tamanho de armazenamento para varchar é o comprimento real dos dados inseridos + 2 bytes. Mesmo que a coluna em si tenha essa sobrecarga de 2 bytes, você pode colocar valores varchar de até 900 bytes em uma coluna indexada.

Na prática, você pode criar um índice em uma coluna maior que 900 bytes de tamanho, mas você terá um problema se tentar inserir algo maior que 900 bytes:
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.

Esteja ciente de que o limite de 900 bytes inclui todas as colunas de uma determinada chave de índice, como mostra este exemplo:
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

Para essas colunas que normalmente são muito grandes para uma chave de índice, você pode obter alguns benefícios da indexação incluindo-as em um índice.