简体   繁体   English

SQL Server-nvarchar字段上的索引

[英]Sql Server - Index on nvarchar field

What is the good approach to keep a nvarchar field unique. 保持nvarchar字段唯一的好方法是什么。 I have a field which is storing URLs of MP3 files. 我有一个字段,用于存储MP3文件的URL。 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. URL的长度可以是10个字符到4000个字符。我试图创建一个索引,它说它不能创建索引,因为总长度超过900个字节。

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. 我在前端使用C#,ASP.net MVC。

You could use CHECKSUM command and put index on column with checksum. 您可以使用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 这是文档: http : //technet.microsoft.com/zh-cn/library/ms189788.aspx

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 Collisions ),然后将索引应用于该列。

MD5 in SQL Server SQL Server中的MD5

You could create a hash code of the url and use this integer as a unique index on your db. 您可以创建url的哈希码,并将此整数用作数据库上的唯一索引。 Beware of converting all characters to lowercase first to ensure that all url are in the same format. 注意首先将所有字符都转换为小写,以确保所有url都采用相同的格式。 Same url will generate equal hash code. 相同的网址将生成相等的哈希码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM