相关疑难解决方法(0)

使用声明的变量时估计行非常错误

我正在非常非常错误的估计聚集索引寻求此查询的步骤(估计行是8637530;实际为74723):

Declare @StartDate as date = '10/1/2014';
Declare @EndDate as date = '10/2/2014';

select Sum(Quantity)
from Daily_GC_Items
where SalesDate between @StartDate and @EndDate;
Run Code Online (Sandbox Code Playgroud)

如果我对这样的日期使用字符串文字,则估计值几乎是完美的(估计行数为 75,337.4;实际为 74,723):

select Sum(Quantity)
from Daily_GC_Items
where SalesDate between '10/1/2014' and '10/2/2014'
Run Code Online (Sandbox Code Playgroud)

表定义:

[SalesDate] [date] NOT NULL,
[Store] [varchar](10) NOT NULL,
[GuestCheckNumber] [smallint] NOT NULL,
[ItemSequence] [smallint] NOT NULL,
[Quantity] [smallint] NOT NULL,
(Several more columns)
Run Code Online (Sandbox Code Playgroud)

首要的关键:

[SalesDate] ASC,
[Store] ASC,
[GuestCheckNumber] ASC,
[ItemSequence] ASC
Run Code Online (Sandbox Code Playgroud)

这种行为的可能原因是什么?

performance sql-server execution-plan sql-server-2014 cardinality-estimates query-performance

3
推荐指数
1
解决办法
777
查看次数