Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: The total size of an index or primary key cannot exceed 900 bytes

I am trying to put an index on a column that will hold URLs. Since the maximum length of a URL an be over 2000 characters long I set the data type to NVARCHAR(3000). When I did this I got the error The total size of an index or primary key cannot exceed 900 bytes. Since I may need to search for records by URL i will need an index on my URL Column. Is there a way around this limitation?

like image 820
Luke101 Avatar asked Oct 18 '11 00:10

Luke101


People also ask

What is the maximum possible length of an index?

Warning! The maximum key length for a nonclustered index is 1700 bytes. The index 'IDX_EStrat' has maximum length of 5000 bytes. For some combination of large values, the insert/update operation will fail.

What is the maximum number of indexed columns that may be created by the users in a standard table?

In SQL Server you can include non-key columns in a nonclustered index, to avoid the limitation of a maximum of 32 key columns. For more information, see Create Indexes with Included Columns.

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

The total amount of space used to store an individual row of data in SQL Server cannot exceed 8,060 bytes. The reason for the 8,060-byte limit is simple.

How many columns can exist together per table?

No, you may have upto 1,000 columns in a table these days.


2 Answers

You could create a computed column for the checksum of the URL and then use the checksum in the query. Checksums will not be unique, but it will quickly narrow down the number of possible matches.

First, add a computed column to your table, like this:

Alter Table YourTableName Add URL_HASH As CheckSum(URL)

Now index the column like this:

Create Index idx_YourTableName_URL_HASH On YourTableName(URL_HASH)

Now you can write a query that will do an index seek to find the row you are looking for:

Select URL
From   YourTableName
Where  URL_HASH = CheckSum(N'google.com')
       And URL = 'google.com'

This method should work very well for exact matches. If you want partial matches, you're better off using the full text search functionality.

like image 127
George Mastros Avatar answered Sep 21 '22 14:09

George Mastros


SQL Server Full Text search is what you'll probably want.

http://msdn.microsoft.com/en-us/library/ms142571.aspx

You have to jump through some minor hoops with setting it up vs a simple index but it shouldn't be super hard.

like image 36
Grant Lammi Avatar answered Sep 25 '22 14:09

Grant Lammi