SQL Server:索引或主键的总大小不能超过900个字节

Luk*_*101 6 sql-server-2008

我试图在一个包含URL的列上放置一个索引.由于URL的最大长度超过2000个字符,我将数据类型设置为NVARCHAR(3000).当我这样做时,我得到了错误 The total size of an index or primary key cannot exceed 900 bytes.由于我可能需要通过URL搜索记录,因此我需要在我的URL列上添加索引.有没有解决这个限制的方法?

G M*_*ros 10

您可以为URL的校验和创建计算列,然后在查询中使用校验和.校验和不会是唯一的,但它会迅速缩小可能的匹配数量.

首先,向表中添加一个计算列,如下所示:

Alter Table YourTableName Add URL_HASH As CheckSum(URL)
Run Code Online (Sandbox Code Playgroud)

现在索引列如下:

Create Index idx_YourTableName_URL_HASH On YourTableName(URL_HASH)
Run Code Online (Sandbox Code Playgroud)

现在,您可以编写一个查询来执行索引查找以查找您要查找的行:

Select URL
From   YourTableName
Where  URL_HASH = CheckSum(N'google.com')
       And URL = 'google.com'
Run Code Online (Sandbox Code Playgroud)

对于完全匹配,此方法应该非常有效.如果您想要部分匹配,最好使用全文搜索功能.