I have a query that when modified ever so slightly will reduce its runtime from approx 37 seconds down to 4 seconds. There is no change to the joins, or the columns being returned.
SLOW query (37 seconds):
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 Statistics for the above:
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.
Now for the query that returns in 4 seconds:
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 statistics for the above:
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.
My question is: how can there be such a difference in execution time between the two, when the only change that is made is replacing IsHidden = 0 with (IsHidden = 0 or IsHidden IS NULL) ? (3 lines from the bottom)
When you are adding the additional clause to include ISNULL, then you are likely to be returning a greater number of rows from AccountRecievable. In turn, you are then joining with more rows in the view. This means that is having to read more rows from the views’s base tables, and thus increasing the time of the query.
The logic here comes from looking at the IO stats you provided. There are additional reads for the tables that are “invisible” to the query you provided. Consider reviewing your index strategy on the view’s base tables.