额外 "where column is not null" 会显着减慢查询执行时间

Additional "where column is not null" significantly slows down a query execution time

在SQL服务器我有一个查询:

select 
    a, b, year(c), max(convert(date,c)), d
from 
    some_view
where
    a is not null
    and b = 'str1'
    and (x in ('str2','str3', ...) or a = 'str4')
    --and c is not null
    --and d is not null
group by
    a, b, year(c), d

执行时间为6秒

如果我取消注释 --and c is not null 执行时间为 6 或 5 秒。

如果我取消注释 --and d is not null 我不知道执行时间,因为我还没有等待查询完成。

为什么取消注释 --and d is not null 会使执行时间飙升?

你检查过执行计划了吗?我的钱将用于指数发行。如果您的查询使用的索引将 d 作为包含列而不是索引列,那么它可能会强制扫描 table/index,这会降低您的性能。 运行 包含和不包含这些 IS NULL 语句的执行计划,这将为您指出问题所在。执行此操作时检查索引提示。