开发者

How can I speed up this view which sums a load of quote line rows?

I am writing a view to show quote totals based on summing the values in a quote line table. I need to restrict the view to only show quotes for customers of a particular 'pricetype'. However when I do this 开发者_如何学运维the view slows down a lot.

SQL to sum the prices

SELECT dbo.quoteline.qid, SUM((dbo.pricelist.listprice - dbo.quoteline.voff) * dbo.quoteline.quantity) AS total
FROM dbo.quoteline LEFT OUTER JOIN dbo.pricelist ON dbo.quoteline.prodcode = dbo.pricelist.prodcode GROUP BY dbo.quoteline.qid

SQL once 'pricetype' constraint is added

SELECT     dbo.quoteline.qid, SUM((dbo.pricelist.listprice - dbo.quoteline.voff) * dbo.quoteline.quantity) AS total
FROM         dbo.pricelist RIGHT OUTER JOIN
                      dbo.client RIGHT OUTER JOIN
                      dbo.quote ON dbo.client.cid = dbo.quote.cid RIGHT OUTER JOIN
                      dbo.quoteline ON dbo.quote.qid = dbo.quoteline.qid ON dbo.pricelist.prodcode = dbo.quoteline.prodcode
WHERE     (dbo.client.pricetype = 'V')
GROUP BY dbo.quoteline.qid

Maybe its late and I am having a moment but any help here would be gratefully appreciated.


Two things: First, can you put an index on the dbo.client.pricetype column without it interfering with inserts/updates? Secondly, inner joins are generally faster than outer joins and since your results and where clause depend on the other tables, I suspect you will want to do inner joins anyways unless there are NULL records you need back from your view. Try this following query to see if it gets you the results you need:

SELECT dbo.quoteline.qid, SUM((dbo.pricelist.listprice - dbo.quoteline.voff) * dbo.quoteline.quantity) AS total
FROM dbo.quoteline 
INNER JOIN dbo.pricelist ON dbo.quoteline.prodcode = dbo.pricelist.prodcode 
INNER JOIN dbo.quote ON dbo.quote.qid = dbo.quoteline.qid 
INNER JOIN dbo.client ON dbo.client.cid = dbo.quote.cid 
WHERE     (dbo.client.pricetype = 'V')
GROUP BY dbo.quoteline.qid


What does happen if you do it like this :

SELECT     dbo.quoteline.qid, SUM((dbo.pricelist.listprice - dbo.quoteline.voff) * dbo.quoteline.quantity) AS total
FROM         dbo.pricelist RIGHT OUTER JOIN
                      dbo.client ON dbo.client.pricetype = 'V' RIGHT OUTER JOIN
                      dbo.quote ON dbo.client.cid = dbo.quote.cid RIGHT OUTER JOIN
                      dbo.quoteline ON dbo.quote.qid = dbo.quoteline.qid AND dbo.pricelist.prodcode = dbo.quoteline.prodcode
GROUP BY dbo.quoteline.qid
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜