具有LIKE和RIGHT函数的SQL Server过滤器索引?

时间:2013-04-16 04:41:10

标签: sql sql-server sql-server-2008 indexing

这不是在现场环境中的自学问题。

我一直在查询以A结尾的名字,所以我想我会在这些数据上创建一个过滤索引,所以它会搜索或扫描。

但它给了我错误。

--Usual Query 
SELECT c.contactname
FROM sales.Customers c where c.contactname like '%A'

--1st Tried failed
create NONCLUSTERED INDEX UCI_NameLikeA
on sales.customers(contactname)
where right(contactname,1)='A'

Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'LIKE'

--2nd method tried failed
create NONCLUSTERED INDEX UCI_NameLikeA
on sales.customers(contactname)
where right(contactname,1)='A'

Msg 10735, Level 15, State 1, Line 3
Incorrect WHERE clause for filtered index 'UCI_NameLikeA' on table 'sales.customers'.

为什么过滤器索引中不允许使用Like和RIGHT函数?我能用其他方法寻找而不是扫描吗?

1 个答案:

答案 0 :(得分:0)

关于手册http://msdn.microsoft.com/de-de/library/ms188783.aspx,只允许使用简单的运算符来过滤索引。

因此,您可以选择非计算列,例如= < >IN()

修改

你能做什么。 创建另一个存储第一个字母的列。 然后创建一个更新此列的索引,如下所示:

UPDATE [mytable]
SET [index_column] = RIGHT(contactname,1)

在那里你可以设置你的INDEX,这可能对你有所帮助。