【问题标题】:SQL Server 2012 : filtered index with "or" conditionSQL Server 2012:带有“或”条件的过滤索引
【发布时间】:2014-02-22 05:39:15
【问题描述】:

我有 SQL Server 2012 Standard 和一个包含 [IS_Deleted] 列的表。

如何创建带有条件的过滤索引?

    where [IS_Deleted] is null or [IS_Deleted] = 0

【问题讨论】:

  • 过滤索引定义中不允许使用OR,我相信你必须创建2个索引来满足这个条件
  • 遗憾的是,这也不能通过计算列上的过滤索引来完成。

标签: tsql indexing sql-server-2012 filtered-index


【解决方案1】:

正如 cmets 所说,您不能这样做。详情请见TechNet

考虑使用索引视图,正如 Arshad Ali 在 MSSQLTips 上所讨论的那样

CREATE VIEW OnlyNotDeleted AS (    
 ...
 WHERE ISNULL(IS_Deleted,0)=0
)

然后索引视图上的适当字段。

【讨论】:

    【解决方案2】:

    作为另一种选择,您可以创建一个计算列和该列的索引。

    create table tst10 (
        id int,
        f int
    )
    
    insert into tst10(id, f) values (1, 0)  
    insert into tst10(id, f) values (2, 1)
    insert into tst10(id) values (3)
    
    select * from tst10
    
    alter table tst10
    add dummyColumn as ISNULL(f, 0)
    
    select * from tst10
    
    create index dummyColumnIdx on tst10(dummyColumn)
    

    【讨论】:

      猜你喜欢
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多