这是一个不在现场环境中的自学问题。具有LIKE和RIGHT功能的SQL Server过滤器索引?
我在查询名称以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'.
为什么在过滤器索引中不允许使用RIGHT函数?我有任何其他方法可以寻找而不是扫描?
您可以使用'where c.contactname like'%A''创建一个索引视图,因为筛选索引是相当有限的。然后选择那个'(noexpand)' –