2013-04-16 37 views
2

这是一个不在现场环境中的自学问题。具有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函数?我有任何其他方法可以寻找而不是扫描?

+0

您可以使用'where c.contactname like'%A''创建一个索引视图,因为筛选索引是相当有限的。然后选择那个'(noexpand)' –

回答

0

关于手册http://msdn.microsoft.com/de-de/library/ms188783.aspx只允许简单的操作员过滤索引。

所以你可以选择非计算列和像=<>IN()

编辑:

你可以做什么。 创建第一个字母存储的另一列。 然后创建一个更新此列这样的指标:

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

在那里,你可以设置你的索引,可能是这有助于你的方法。

+0

任何替代解决方案? –

+1

@VishwanathDalvi:他可以创建另一个列,在'reverse order'中存储'contactName',然后在其上创建'Index'。因此,查询'选择c.contactname FROM sales.Customers c,其中c.contactname如'A%''将产生'索引搜索'或他可以使用'全文搜索' – praveen

+0

如果您正在创建这些列(最右边的字母或反转字符串)进行索引,最好将其作为计算列,以便不需要其他维护。 –