1

我有一个查询,只要稍微修改一下,它的运行时间就会从大约 37 秒减少到 4 秒。连接或返回的列没有变化。

慢查询(37 秒):

declare @PeriodFrom DateTime
declare @PeriodTo DateTime
Set @PeriodFrom = '2012-06-01'
Set @PeriodTo = '2012-06-30'

Select
  0 as PrimaryAccount,
  0 as PrintOrder,
  Cast(Null as integer) as ID,
  Sum(IsNull(MT.Amount, 0)) as Amount,
  Cast(0 as Money) as NetAmount,
  Cast(0 As Money) as TaxAmount,
  Cast(0 as Money) as AmountOutstanding,
  Cast(0 as Money) as AmountPaid,
  'Balance brought forward' as Description
From
  db_site4.dbo.AccountReceivable P
Join
  db_site4.dbo.ARType ART on ART.ARTypeID = P.ARTypeID and ART.ARTypeID = 24
left Join
  db_site4.dbo.vw_MemberTransactions MT
on
  P.AccountReceivableID = MT.AccountReceivableID
where 
  (MT.AccountingDate <= @PeriodFrom or MT.AccountingDate is null)
and
  (Authorised = 1 or Authorised is Null)
and
  IsHidden = 0
and
  P.MemberID = 123

以上的 SQL I/O 统计信息:

Table 'Payment'. Scan count 16, logical reads 23558, physical reads 19, read-ahead reads 5448.
Table 'InvoiceItemPayment'. Scan count 4, logical reads 22237, physical reads 51, read-ahead reads 13432.
Table 'UnallocatedPayment'. Scan count 12, logical reads 431, physical reads 1, read-ahead reads 80.
Table 'AccountReceivable'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0.
Table 'ARType'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0.
Table 'Invoice'. Scan count 11116, logical reads 116984, physical reads 190, read-ahead reads 30910.
Table 'InvoiceItem'. Scan count 5122, logical reads 99786, physical reads 316, read-ahead reads 46236.

现在对于在 4 秒内返回的查询:

declare @PeriodFrom DateTime
declare @PeriodTo DateTime
Set @PeriodFrom = '2012-06-01'
Set @PeriodTo = '2012-06-30'

Select
  0 as PrimaryAccount,
  0 as PrintOrder,
  Cast(Null as integer) as ID,
  Sum(IsNull(MT.Amount, 0)) as Amount,
  Cast(0 as Money) as NetAmount,
  Cast(0 As Money) as TaxAmount,
  Cast(0 as Money) as AmountOutstanding,
  Cast(0 as Money) as AmountPaid,
  'Balance brought forward' as Description
From
  db_site4.dbo.AccountReceivable P
Join
  db_site4.dbo.ARType ART on ART.ARTypeID = P.ARTypeID and ART.ARTypeID = 24
left Join
  db_site4.dbo.vw_MemberTransactions MT
on
  P.AccountReceivableID = MT.AccountReceivableID
where 
  (MT.AccountingDate <= @PeriodFrom or MT.AccountingDate is null)
and
  (Authorised = 1 or Authorised is Null)
and
  (IsHidden = 0 or IsHidden is null)
and
  P.MemberID = 123

上面的 SQL I/O 统计信息:

Table 'Payment'. Scan count 6271, logical reads 19857, physical reads 0, read-ahead reads 0.
Table 'UnallocatedPayment'. Scan count 2, logical reads 4, physical reads 0, read-ahead reads 0.
Table 'InvoiceItemPayment'. Scan count 4399, logical reads 33400, physical reads 0, read-ahead reads 0.
Table 'InvoiceItem'. Scan count 10581, logical reads 60682, physical reads 4, read-ahead reads 0.
Table 'Invoice'. Scan count 3, logical reads 22102, physical reads 3, read-ahead reads 0.
Table 'AccountReceivable'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0.
Table 'ARType'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0.

我的问题是:当唯一的更改是替换为时,两者之间的执行时间怎么会有这样的IsHidden = 0差异(IsHidden = 0 or IsHidden IS NULL)?(从底部开始 3 行)

4

2 回答 2

0

取决于您在 IsHidden 和索引中拥有的数据。

如果您有很多空值和良好的索引,则可能是执行计划首先将它们放在“IsHidden = 0”之前,因此它比仅扫描“IsHidden = 0”更快。

于 2012-10-11T06:27:00.050 回答
0

当您添加附加子句以包含 ISNULL 时,您可能会从 AccountRecievable 返回更多的行。然后,您将加入视图中的更多行。这意味着必须从视图的基表中读取更多行,从而增加查询时间。

这里的逻辑来自查看您提供的 IO 统计信息。对于您提供的查询“不可见”的表,还有其他读取。考虑查看视图基表上的索引策略。

于 2012-10-11T07:01:58.507 回答