February 27, 2013 at 8:28 pm
Hi,
Can someone help me in optimizing below query, is there any other way where I can improve this query
SELECT A.Col1, COUNT(B.POST_DT) AS OD_NO
FROM (SELECT Col1, col3, Col2,
SUM(CASE WHEN Col2> 0 THEN GROSS_AMOUNT ELSE DUEAMNT END) AS DUEAMNT,
SUM(CRAMNT) AS CRAMNT FROM myTbl
WHERE POST_DT< someDate AND Col2=0
GROUP BY Col1, col3, Col2
HAVING SUM(DUEAMNT) > SUM(CRAMNT)
ORDER BY Col1) A,
myTbl B
WHERE A.col3=B.col3
AND (CASE WHEN B.Col2> 0 THEN B.GROSS_AMOUNT ELSE B.DUEAMNT END)>0
GROUP BY A.Col1
TIA
February 27, 2013 at 8:50 pm
have you tried running it through DTA?
http://msdn.microsoft.com/en-us/library/ms162812.aspx
FYI, don't implement everything it recommends look on the reports for new indexes that have the most bang.
February 27, 2013 at 8:51 pm
you could read "SUM(CRAMNT) AS CRAMNT FROM myTbl" into a variable and then just select the variable instead of running sum for each row.
February 27, 2013 at 8:56 pm
Jon.Morisi (2/27/2013)
you could read "SUM(CRAMNT) AS CRAMNT FROM myTbl" into a variable and then just select the variable instead of running sum for each row.
Thanks but can you please guide me how to do that, thanks
February 27, 2013 at 11:09 pm
Please read the first article I reference below in my signature block. Follow the instructions in that article regarding what you should post and how to post it to get the best answers quickly. We really do need the DDL, sample data, and expected results if you want good, quality answers and tested code to boot.
February 28, 2013 at 1:45 am
surindersinghthakur (2/27/2013)
Hi,Can someone help me in optimizing below query, is there any other way where I can improve this query
SELECT A.Col1, COUNT(B.POST_DT) AS OD_NO
FROM (SELECT Col1, col3, Col2,
SUM(CASE WHEN Col2> 0 THEN GROSS_AMOUNT ELSE DUEAMNT END) AS DUEAMNT,
SUM(CRAMNT) AS CRAMNT FROM myTbl
WHERE POST_DT< someDate AND Col2=0
GROUP BY Col1, col3, Col2
HAVING SUM(DUEAMNT) > SUM(CRAMNT)
ORDER BY Col1) A,
myTbl B
WHERE A.col3=B.col3
AND (CASE WHEN B.Col2> 0 THEN B.GROSS_AMOUNT ELSE B.DUEAMNT END)>0
GROUP BY A.Col1
TIA
No, because this won't run - the inner select has ORDER BY without TOP etc. At a guess, this is a cut-down version of the real query. Any suggestions you might get for improving the performance would be guesses too. Follow the instructions Lynn posted, and attach the actual execution plan as a .sqlplan file.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply