优化程序将忽略筛选的索引条件 [英] Filtered index condition is ignored by optimizer

查看:134
本文介绍了优化程序将忽略筛选的索引条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在运行一个显示有趣猫图片的网站。我有一个名为 CatPictures 的表,其中列文件名 Awesomeness DeletionDate ,以及以下索引:

Assume I'm running a website that shows funny cat pictures. I have a table called CatPictures with the columns Filename, Awesomeness, and DeletionDate, and the following index:

create nonclustered index CatsByAwesomeness
on CatPictures (Awesomeness) 
include (Filename)
where DeletionDate is null

我的主要查询是:

select Filename from CatPictures where DeletionDate is null and Awesomeness > 10

作为一个人,我知道上述索引是SQL Server所需要的,因为索引过滤条件已经确保 DeletionDate为null part。

I, as a human being, know that the above index is all that SQL Server needs, because the index filter condition already ensures the DeletionDate is null part.

SQL Server然而不知道这一点;我的查询的执行计划不会使用我的索引:

SQL Server however doesn't know this; the execution plan for my query will not use my index:

即使添加索引提示,它仍会显式检查 DeletionDate 通过查看实际的表格数据:

Even if adding an index hint, it will still explicitly check DeletionDate by looking at the actual table data:

(并且还抱怨缺少的索引包括 DeletionDate )。

(and in addition complain about a missing index that would include DeletionDate).

当然我可以

include (Filename, DeletionDate)

相反,它会起作用:

但是包含该列似乎是浪费,因为这只会占用空间而不添加任何新信息。

But it seems a waste to include that column, since this just uses up space without adding any new information.

I有一种方法可以让SQL Server知道过滤条件已经在检查 DeletionDate

Is there a way to make SQL Server aware that the filter condition is already doing the job of checking DeletionDate?

推荐答案

不,不是。

查看此连接项。它是关闭的,因为无法修复。 (或这一个 IS NULL 具体情况下)

See this connect item. It is Closed as Won't Fix. (Or this one for the IS NULL case specifically)

连接项确实提供了一个解决方法如下所示。

The connect item does provide a workaround shown below.


RichardB CFCU发表于29/09/2011上午9:15

Posted by RichardB CFCU on 29/09/2011 at 9:15 AM

解决方法是 INCLUDE 正在过滤的列。

A workaround is to INCLUDE the column that is being filtered on.

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)


这篇关于优化程序将忽略筛选的索引条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆