【问题标题】:SQL server filter index with LIKE and RIGHT function?具有 LIKE 和 RIGHT 功能的 SQL 服务器过滤器索引?
【发布时间】:2013-04-16 04:41:09
【问题描述】:

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

我一直在查询以 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 函数?有什么其他方法可以代替扫描吗?

【问题讨论】:

  • 您可以使用where c.contactname like '%A' 创建索引视图,因为过滤后的索引非常有限。然后从中选择with(noexpand)

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


【解决方案1】:

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

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

编辑:

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

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

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

【讨论】:

  • @VishwanathDalvi :他可以创建另一个列,将contactName 存储在reverse Order 中,然后在其上创建Index。所以查询Select c.contactname FROM sales.Customers c where c.contactname like 'A%' 将产生Index Seek 或者他可以使用Full text Search
  • 如果您正在创建这些列(最右边的字母或反向字符串)以进行索引,最好将其作为计算列来进行,这样就不需要进行其他维护。
  • @Damien_The_Unbeliever - 但是您不能在计算列上创建过滤索引。
  • @MartinSmith - 我的理解是,建议有一个包含最右边字母或反转字符串的替代列将允许创建一个(未过滤的)索引来帮助这个查询。
猜你喜欢
  • 2013-01-28
  • 2018-02-19
  • 1970-01-01
  • 1970-01-01
  • 2020-11-09
  • 2020-05-01
  • 2013-09-18
  • 2014-09-08
  • 1970-01-01
相关资源
最近更新 更多