너무 작게 수정하면 런타임이 약 37 초에서 4 초로 단축됩니다. 조인 또는 리턴되는 컬럼은 변경되지 않습니다.약간의 코드 변경으로 SQL Server 쿼리 응답 시간이 크게 변경됨
SLOW 쿼리 (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.
:
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
위의 대한 3,691,363,210
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 줄)
데이터를 필터링 할 때 조건을 제거하려고 할 때 테이블의 대부분 장소에서 'IsHidden'필드가 null 일 수 있습니다. – Buzz
물리적 읽기를 살펴보면 두 번째로 쿼리를 실행할 때 테이블이 처음부터 메모리에 이미 있음을 알 수 있습니다. DBCC DROPCLEANBUFFERS를 실행하여 버퍼 캐시를 지우려면 첫 번째 쿼리를 실행하고 버퍼를 다시 삭제 한 다음 두 번째 쿼리를 실행하십시오. –
버즈에 감사드립니다. 빠른 쿼리에서 실제로 ' IsHidden '은 null이 아니며이를 제거하지 않습니다. – AshMcG