我有一个查询,只要稍微修改一下,它的运行时间就会从大约 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 行)