Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

900 byte index size limit in character length

What is the total character limit for a 900 byte index limit that SQL Server 2012 has. I created a column that has varchar(2000), but I think that it exceeding the 900 byte that SQL Server limited? What would be a max varchar(?) to fit inside the 900 byte index column?

like image 635
iefpw Avatar asked Oct 03 '12 21:10

iefpw


People also ask

What is the maximum possible length of an index?

The maximum key length for a nonclustered index is 1700 bytes.

How big is varchar MAX?

The VARCHAR(MAX) data type is similar to the VARCHAR data type in that it supports variable-length character data. VARCHAR(MAX) is different from VARCHAR because it supports character strings up to 2 GB (2,147,483,647 bytes) in length.

How many bytes an index key can have at the maximum in SQL Server 2016?

The maximum number of bytes in a clustered index key can't exceed 900. For a nonclustered index key, the maximum is 1,700 bytes.

What is the maximum size of a row in SQL Server?

SQL Server's Row Byte size is limited somewhere right around 8k bytes for physical data stored in the database. This means if you have a few large varchar or nvarchar fields it's actually quite easy to outrun the size of a row.


2 Answers

The storage size for varchar is the actual length of the data entered + 2 bytes. Even though the column itself has that 2 byte overhead, you can put up to 900 byte varchar values into a column which is indexed.

In practice, you can create an index on a column larger than 900 bytes in size, but you will have a problem if you actually try to insert something larger than 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. 

Be aware that the 900-byte limit includes all columns of a given index key, as this example shows:

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 

For these columns that are normally too large for an index key, you may be able to gain some benefits of indexing by including them in an index.

like image 181
Tim Lehner Avatar answered Sep 28 '22 13:09

Tim Lehner


On a related note, another option you can try, to get an index on a wide column, is outlined at http://www.brentozar.com/archive/2013/05/indexing-wide-keys-in-sql-server/ where a hash column is added to the table then indexed and used in your queries.

like image 38
lmingle Avatar answered Sep 28 '22 13:09

lmingle