Saturday, February 25, 2012

Query taking longer to run after adding only 2 fields and additional JOIN

why does my query take 2x as long to run after adding the Fees in?

SELECT m.customer,

c.name,

c.customer,

c.state,

m.Branch,

CASE WHEN ph.batchtype = 'PUR' OR ph.batchtype = 'PAR' OR ph.batchtype = 'PCR' Then

(ph.totalpaid - ph.ForwardeeFee)

ELSE

0.00

END AS [Posted Amount],

ph.systemmonth,

ph.datepaid,

ph.totalpaid,

ph.batchtype,

m.desk,

'' AS [New Old CC],

'In-House' AS Type,

'' AS Active,

ph.UID,

m.number,

dc.amount CC,

p.amount AS PDC,

m.original,

ph.OverPaidAmt,

fg.FeeGoal_AZ,

fg.FeeGoal_IL

FROM dbo.Master m LEFT JOIN dbo.payhistory ph ON m.number = ph.number

INNER JOIN dbo.DeC dc ON dc.number = m.number

INNER JOIN dbo.pdc p ON p.number = m.number

INNER JOIN dbo.Customer c ON c.Customer = m.Customer

INNER JOIN ReportingServer.dbo.FeeGoal fg ON fg.CustomerID = c.Customer

GROUP BY m.customer,

c.name,

c.customer,

c.state,

m.Branch,

ph.OverPaidAmt,

ph.systemmonth,

ph.datepaid,

ph.totalpaid,

ph.batchtype,

m.desk,

ph.UID,

m.number,

dc.amount,

p.amount,

m.original ,

ph.systemmonth,

ph.systemyear,

ph.ForwardeeFee,

fg.FeeGoal_AZ,

fg.FeeGoal_IL

HAVING ph.systemmonth = datepart(mm, getdate()) AND ph.batchtype <> 'DA' AND

ph.batchtype <> 'DAR' AND ph.systemyear = datepart(yy, getdate())

ORDER BY m.customer

I assume that you mean that you added the two columns from the FeeGoal table to the select list and this caused the query to run 2x as long.

My guess would be that these columns are not included in an index and you forced the optimizer to do a table scan on the FeeGoal table.

Have you looked the the execution plan for one versus the other? What indexes do you have on the FeeGoal table? How big is the FeeGoal table?

I would start by looking at the plan, but if you could provide a bit more about these tables and what indexes they have on them, I might be able to give some tips.

HTH

No comments:

Post a Comment