bal*_*pha 20 indexing sql-server-2008 filtered-index
假设我正在运行一个显示有趣猫图片的网站.我有一个名为表CatPictures
与列Filename
,Awesomeness
以及DeletionDate
和以下指标:
create nonclustered index CatsByAwesomeness
on CatPictures (Awesomeness)
include (Filename)
where DeletionDate is null
Run Code Online (Sandbox Code Playgroud)
我的主要问题是:
select Filename from CatPictures where DeletionDate is null and Awesomeness > 10
Run Code Online (Sandbox Code Playgroud)
作为一个人,我知道上面的索引是SQL Server所需要的,因为索引过滤条件已经确保了该DeletionDate is null
部分.
但是SQL Server不知道这个; 我的查询的执行计划将不使用我的索引:
即使添加索引提示,它仍将DeletionDate
通过查看实际的表数据进行显式检查:
(并且还抱怨缺少的索引DeletionDate
).
我当然可以
include (Filename, DeletionDate)
Run Code Online (Sandbox Code Playgroud)
相反,它会工作:
但是包含该列似乎是浪费,因为这只会占用空间而不添加任何新信息.
有没有办法让SQL Server意识到过滤条件已经在检查工作DeletionDate
?
Mar*_*ith 18
不,不是.
请参阅此连接项.它是关闭的,因为无法修复.(或者这一项的IS NULL
情况下,具体地)
连接项确实提供了如下所示的解决方法.
发布者RichardB CFCU于29/09/2011于9:15 AM发表
解决方法是对
INCLUDE
要过滤的列进行处理.例:
Run Code Online (Sandbox Code Playgroud)CREATE NONCLUSTERED INDEX [idx_FilteredKey1] ON [dbo].[TABLE] ( [TABLE_ID] ASC, [TABLE_ID2] ASC ) INCLUDE ( [REMOVAL_TIMESTAMP]) --explicitly include the column here WHERE ([REMOVAL_TIMESTAMP] IS NULL)
归档时间: |
|
查看次数: |
2702 次 |
最近记录: |