Sqlserver
 sql >> Baza danych >  >> RDS >> Sqlserver

900-bajtowy limit rozmiaru indeksu w długości znaków

Rozmiar pamięci dla varchar to rzeczywista długość wprowadzonych danych + 2 bajty. Nawet jeśli sama kolumna ma 2 bajty narzutu, możesz umieścić do 900 bajtowych wartości zmiennych w indeksowanej kolumnie.

W praktyce możesz tworzyć indeks na kolumnie o rozmiarze większym niż 900 bajtów, ale będziesz miał problem, jeśli rzeczywiście spróbujesz wstawić coś większego niż 900 bajtów:

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.

Należy pamiętać, że limit 900 bajtów obejmuje wszystkie kolumny danego klucza indeksu, jak pokazuje ten przykład:

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

W przypadku tych kolumn, które zwykle są za duże dla klucza indeksu, możesz uzyskać pewne korzyści z indeksowania, umieszczając je w indeksie.



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. OR Operator Zwarcie w SQL Server

  2. Połącz HP-UX Itanium z SQL Server

  3. Jak używać FILEGROUPPROPERTY() w SQL Server

  4. Co to jest SQL Server Management Studio (SSMS)?

  5. Jak dodać domyślne ograniczenie do istniejącej kolumny w SQL Server