Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Batas ukuran indeks 900 byte dalam panjang karakter

Ukuran penyimpanan untuk varchar adalah panjang sebenarnya dari data yang dimasukkan + 2 byte. Meskipun kolom itu sendiri memiliki overhead 2 byte, Anda dapat memasukkan hingga 900 byte nilai varchar ke dalam kolom yang diindeks.

Dalam praktiknya, Anda dapat membuat indeks pada kolom yang berukuran lebih dari 900 byte, tetapi Anda akan mendapat masalah jika Anda benar-benar mencoba menyisipkan sesuatu yang lebih besar dari 900 byte:

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.

Ketahuilah bahwa batas 900 byte mencakup semua kolom dari kunci indeks yang diberikan, seperti yang ditunjukkan contoh ini:

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

Untuk kolom ini yang biasanya terlalu besar untuk sebuah kunci indeks, Anda mungkin dapat memperoleh beberapa manfaat dari pengindeksan dengan memasukkannya ke dalam indeks.



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. OPENJSON "Sintaks salah di dekat kata kunci 'dengan'." di SQL Server (ASK)

  2. Gunakan OBJECT_NAME() untuk Mendapatkan Nama Objek dari object_id-nya di SQL Server

  3. Konversi 'datetime2' menjadi 'date' di SQL Server (Contoh T-SQL)

  4. Gabungkan interval tanggal yang tumpang tindih

  5. LPAD di SQL Server 2008