SQL server Date = @date 但 executionplan xml 显示为不等式

SQL server Date = @date but executionplan xml show it in inequality

所以我试图猜测 SQL 服务器将根据我编写的查询建议哪个缺失索引。请帮助我理解为什么这不被执行计划XML视为“平等”?我正在使用 SQL Server 2017。

我原以为这会被认为是“平等”,但显然对缺少索引创建语法的顺序做出了错误的猜测。

数据库名称 - Whosebug2013

查询:

set statistics io on
declare   @year int = 2008
declare @date nvarchar(10)

while @year <= 2015
begin
    set @date =  cast (@year as nvarchar(6))+ cast('-05-27' as nvarchar(6))
    select
        title,
        cast (CreationDate as date)
    from
        posts
    where
        cast (CreationDate as date) = @date
        and title = 'Tool to diagonalize large matrices'

    set @year = @year + 1
end

缺少索引建议:

/*
create NONCLUSTERED INDEX [NonClusteredIndex_creationdate]
ON [dbo].[Posts] ([Title],[CreationDate])
*/

执行计划XML:

<MissingIndexes>
  <MissingIndexGroup Impact="99.9676">
    <MissingIndex Database="[Whosebug2013]" Schema="[dbo]" Table="[Posts]">
      <ColumnGroup Usage="EQUALITY">
        <Column Name="[Title]" ColumnId="19" />
      </ColumnGroup>
      <ColumnGroup Usage="INEQUALITY">
        <Column Name="[CreationDate]" ColumnId="8" />
      </ColumnGroup>
    </MissingIndex>
  </MissingIndexGroup>
</MissingIndexes>

假设 CreationDate 是一个日期时间,您已将 CreationDate 包装在消除时间部分的转换中:

where
    cast (CreationDate as date) = @date

查询只能在标题上匹配,然后必须全天扫描该标题的所有创建日期。有对这种特殊模式的特殊支持(解释 here),并且表达式在内部被转换为 CreationDate 上的范围谓词,因此不等式。

这种方法的一些注意事项是 here,查询的形式可能更好:

where CreationDate >= @date
  and CreationDate < dateadd(day,1,@date)