Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql Server - Index on nvarchar field

What is the good approach to keep a nvarchar field unique. I have a field which is storing URLs of MP3 files. The URL length can be anything from 10 characters to 4000. I tried to create an index and it says it cannot create the index as the total length exceeds 900 bytes.

If the field is not indexed, it's going to be slow to search anything. I am using C#, ASP.net MVC for the front end.

like image 762
Parminder Avatar asked Feb 07 '14 09:02

Parminder


People also ask

What is nvarchar data type in SQL?

Overview of SQL Server NVARCHAR data type. SQL Server NVARCHAR data type is used to store variable-length, Unicode string data. The following shows the syntax of NVARCHAR: NVARCHAR (n) Code language: SQL (Structured Query Language) (sql) In this syntax, n defines the string length that ranges from 1 to 4,000.

Can varchar and varbinary columns be part of an index key?

varchar (max) and varbinary (max) columns cannot be part of an index key. In SQL Server (Starting with SQL Server 2012 (11.x)) and Azure SQL Database, when a table contains varchar (max) or varbinary (max) columns, a clustered index containing other columns can be built or rebuilt using the ONLINE option.

Is it possible to index A varchar column with max value?

varchar(max) columns cannot be indexed, as you have found out. You will need to index something else. It could be a shortened version of the data (only you can tell whether this is acceptable or not) or a hashed version of the data.

What is the default length of a nvarchar column?

In this syntax, n defines the string length that ranges from 1 to 4,000. If you don’t specify the string length, its default value is 1. Another way to declare a NVARCHAR column is to use the following syntax:


3 Answers

You could use CHECKSUM command and put index on column with checksum.

--*** Add extra column to your table that will hold checksum
ALTER TABLE Production.Product
ADD cs_Pname AS CHECKSUM(Name);
GO

--*** Create index on new column
CREATE INDEX Pname_index ON Production.Product (cs_Pname);
GO

Then you can retrieve data fast using following query:

SELECT * 
FROM Production.Product
WHERE CHECKSUM(N'Bearing Ball') = cs_Pname
AND Name = N'Bearing Ball';

Here is the documentation: http://technet.microsoft.com/en-us/library/ms189788.aspx

like image 96
Kaspars Ozols Avatar answered Sep 27 '22 23:09

Kaspars Ozols


You can use a hash function (although theoretically it doesn't guarantee that two different titles will have different hashes, but should be good enough: MD5 Collisions) and then apply the index on that column.

MD5 in SQL Server

like image 42
Joao Leal Avatar answered Sep 27 '22 23:09

Joao Leal


You could create a hash code of the url and use this integer as a unique index on your db. Beware of converting all characters to lowercase first to ensure that all url are in the same format. Same url will generate equal hash code.

like image 38
Oscar Avatar answered Sep 27 '22 21:09

Oscar