Problem With Where Claus
Feb 21, 2008I have a rather long and ugly query that joins seven or eight tables, does some aggreagaions and a union.
It takes about a minute to return 300,000 rows. When I add a where condition which uses a non-key integer field it pares the processing down to 20 seconds to get 2,000 rows. However if I add a second non-key integer field to my where claus it takes just under 30 MINUTES to produce a similar result set.
This is so far out of whak that i don't even know where to start to look.
Here is the query (and I just got this this morning so I haven't worked on it to tune it - I already see a few areas I want to change):
SELECT AMTRANHDR.SMBNKNumber, AMTRANHDR.AMACTNumber, AMTRANHDR.AMALTNumber,
POORDERHDR.POORHNumber, AMTRANHDR.AMTRHNumber, AMTRANHDR.AMTRHType, AMTRANHDR.AMTRHSubType,
AMTRANHDR.AMTRHCode, AMTRANHDR.AMTRHDesc, AMTRANHDR.AMTRHDate, AMTRANHDR.AMTRHAmt, AMTRANHDR.AMTRHDueDate,
AMTRANHDR.AMTRHDiscAllowed, AMTRANHDR.AMTRHDiscDate, AMTRANHDR.SMBCHNumber, AMTRANHDR.AMTRHMasterCode,
isnull(TotalBalance.BalDue,0) BalDue, isnull(TotalAmtNoDiscAdj.PaidAmt,0) as PaidAmt,
isnull(TotalDisc.DiscTaken,0) as DiscTaken, isnull(TotalAdj.AdjTaken,0) as AdjTaken,
AMTRANHDR.AMTRHRecvShip, CASE WHEN AMTRANHDR.AMTRHStatus = 'P' THEN 'Paid' WHEN AMTRANHDR.AMTRHStatus = ' ' THEN 'Open'
WHEN AMTRANHDR.AMTRHStatus = 'S' THEN 'Select' WHEN AMTRANHDR.AMTRHStatus = 'H' THEN 'Hold' WHEN AMTRANHDR.AMTRHStatus = 'C' THEN 'Conversion' end as AMTRHStatus,
AMTRANHDR.POORRNumber, POORDERREL.POORDNumber, POORDERDTL.POORHNumber, POORDERHDR.POORHCode, POORDERHDR.POORHCode + '-'
+ POORDERDTL.POORDSeq + '-' + POORDERREL.POORRSeq AS ReleaseNumber, Payments.LastPaymentDate,
Payments.PaymentCount, SMCODEBCH.SMBCHStatus, AMALTERNATE.AMALTCode, AMALTERNATE.AMALTName
FROM [dbo].AMTRANHDR
LEFT OUTER JOIN [dbo].AMALTERNATE ON AMTRANHDR.AMALTNumber = AMALTERNATE.AMALTNumber
LEFT OUTER JOIN [dbo].SMCODEBCH ON AMTRANHDR.SMBCHNumber = SMCODEBCH.SMBCHNumber
LEFT OUTER JOIN [dbo].POORDERREL ON AMTRANHDR.POORRNumber = POORDERREL.POORRNumber
LEFT OUTER JOIN [dbo].POORDERDTL ON POORDERREL.POORDNumber = POORDERDTL.POORDNumber
LEFT OUTER JOIN [dbo].POORDERHDR ON POORDERDTL.POORHNumber = POORDERHDR.POORHNumber
LEFT OUTER JOIN [dbo].ICINVENTORY ON AMTRANHDR.AMTRHNumber = ICINVENTORY.AMTRHNumber
LEFT OUTER JOIN (select amtrhnumber, 0 as BalDue from AMTRANHDR
where AMTRHType = 'P'
UNION select AMTRANHDR.AMTRHNumber, AMTRHAmt - ISNULL(PaidTran.BalDue,0) as BalDue FROM AMTRANHDR LEFT OUTER JOIN
(SELECT AMPMTCROSS.AMPMCItem, sum(isnull(AMPMTCROSS.AMPMCAmount,0) +
isnull(AMPMTCROSS.AMPMCDiscount,0) + isnull(AMPMTCROSS.AMPMCAdjust,0)) as BalDue
FROM [dbo].AMPMTCROSS GROUP BY AMPMTCROSS.AMPMCItem) as PaidTran ON PaidTran.AMPMCItem = AMTRANHDR.AMTRHNumber
where AMTRHType = 'T')
AS TotalBalance ON AMTRANHDR.AMTRHNumber = TotalBalance.Amtrhnumber
LEFT OUTER JOIN (SELECT AMPMTCROSS.AMPMCItem, sum(isnull(AMPMTCROSS.AMPMCAmount,0)) as PaidAmt
FROM [dbo].AMPMTCROSS
INNER JOIN [dbo].AMTRANHDR ON AMPMTCROSS.AMPMCCheck = AMTRANHDR.AMTRHNumber
GROUP BY AMPMTCROSS.AMPMCItem) AS TotalAmtNoDiscAdj ON TotalAmtNoDiscAdj.AMPMCItem = AMTRANHDR.AMTRHNumber
LEFT OUTER JOIN (SELECT Count(0) AS PaymentCount, AMPMCItem, MAX(AMTRHDate) AS LastPaymentDate
FROM [dbo].AMPMTCROSS
INNER JOIN [dbo].AMTRANHDR ON AMPMTCROSS.AMPMCCheck = AMTRANHDR.AMTRHNumber
GROUP BY AMPMTCROSS.AMPMCItem) AS Payments ON AMTRANHDR.AMTRHNumber = Payments.AMPMCItem
LEFT OUTER JOIN (SELECT AMPMTCROSS.AMPMCItem, sum(isnull(AMPMTCROSS.AMPMCDiscount,0)) as DiscTaken
FROM [dbo].AMPMTCROSS
INNER JOIN [dbo].AMTRANHDR ON AMPMTCROSS.AMPMCCheck = AMTRANHDR.AMTRHNumber
GROUP BY AMPMTCROSS.AMPMCItem) AS TotalDisc ON AMTRANHDR.AMTRHNumber = TotalDisc.AMPMCItem
LEFT OUTER JOIN (SELECT AMPMTCROSS.AMPMCItem, sum(isnull(AMPMTCROSS.AMPMCAdjust,0)) as AdjTaken
FROM [dbo].AMPMTCROSS
INNER JOIN [dbo].AMTRANHDR ON AMPMTCROSS.AMPMCCheck = AMTRANHDR.AMTRHNumber
GROUP BY AMPMTCROSS.AMPMCItem) AS TotalAdj ON AMTRANHDR.AMTRHNumber = TotalAdj.AMPMCItem
where AMTRANHDR.AMTRHDeletedOn is null and AMTRANHDR.ADDIVNumber = 8
and AMTRANHDR.AMACTNumber = 21179
and AMTRANHDR.AMALTNumber = 23195 --THIS IS WHERE I RUN INTO TROUBLE (either alone take 20 seconds, together 30 minutes)