AnnaJames77
asked on
Slow Query
The query of SSRS report takes long time .
select a.InvoiceID,a.CustomerCode ,a.CustNam e,a.dimens ion,
a.dimension2_,a.dimension3 _ ,
a.SALES_AMOUNT,a.COST_AMOU NT,a.GROSS _PROFIT,a. MARGIN_PER
from (
select InvoiceID,CustomerCode,Cus tName,dime nsion,dime nsion2_,di mension3_,
sum(LineAmountMST) SALES_AMOUNT,
SUM(linecostAmount) COST_AMOUNT,
sum(LineAmountMST- linecostAmount) GROSS_PROFIT,
case
when SUM(lineamountmst)=0 and sum(linecostamount)> 0 then -100
when SUM(lineamountmst)=0 and sum(linecostamount)< 0 then 100
when SUM(lineamountmst)=0 and sum(linecostamount)= 0 then 0
else
sum(LineAmountMST- linecostAmount)/sum(LineAm ountMST)*1 00 END MARGIN_PER
FROM salesdim
where invoicedate between @startdate and @enddate
and itembuyergroupid in (@itembuyergroupid)
and itemgroupid in (@itemgroupid)
and salesoriginid in (@salesoriginid)
and CustomerCode in (@Cust)
and dimension in (@Dept)
and dimension2_ in (@CC)
and dimension3_ in (@Purpose)
GROUP BY InvoiceID,CustomerCode,Cus tName,dime nsion,dime nsion2_,di mension3_) a
where a.MARGIN_PER between @marginFrom and @marginTo
order by a.MARGIN_PER desc
select a.InvoiceID,a.CustomerCode
a.dimension2_,a.dimension3
a.SALES_AMOUNT,a.COST_AMOU
from (
select InvoiceID,CustomerCode,Cus
sum(LineAmountMST) SALES_AMOUNT,
SUM(linecostAmount) COST_AMOUNT,
sum(LineAmountMST- linecostAmount) GROSS_PROFIT,
case
when SUM(lineamountmst)=0 and sum(linecostamount)> 0 then -100
when SUM(lineamountmst)=0 and sum(linecostamount)< 0 then 100
when SUM(lineamountmst)=0 and sum(linecostamount)= 0 then 0
else
sum(LineAmountMST- linecostAmount)/sum(LineAm
FROM salesdim
where invoicedate between @startdate and @enddate
and itembuyergroupid in (@itembuyergroupid)
and itemgroupid in (@itemgroupid)
and salesoriginid in (@salesoriginid)
and CustomerCode in (@Cust)
and dimension in (@Dept)
and dimension2_ in (@CC)
and dimension3_ in (@Purpose)
GROUP BY InvoiceID,CustomerCode,Cus
where a.MARGIN_PER between @marginFrom and @marginTo
order by a.MARGIN_PER desc
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In the following lines
and itembuyergroupid in (@itembuyergroupid)
and itemgroupid in (@itemgroupid)
and salesoriginid in (@salesoriginid)
and CustomerCode in (@Cust)
and dimension in (@Dept)
and dimension2_ in (@CC)
and dimension3_ in (@Purpose)
I understand @itembuyergroupid etc all come from variables/parameters passed to SSRS. Is it possible for you to tweak them to replace IN with exists and then check if that gives a better execution plan? Often, subquery with IN is inferior to exists in performance but not always. You may check this article for more details - http://blog.sqlauthority.c om/2010/06 /05/sql-se rver-conve rt-in-to-e xists-perf ormance-ta lk/
and itembuyergroupid in (@itembuyergroupid)
and itemgroupid in (@itemgroupid)
and salesoriginid in (@salesoriginid)
and CustomerCode in (@Cust)
and dimension in (@Dept)
and dimension2_ in (@CC)
and dimension3_ in (@Purpose)
I understand @itembuyergroupid etc all come from variables/parameters passed to SSRS. Is it possible for you to tweak them to replace IN with exists and then check if that gives a better execution plan? Often, subquery with IN is inferior to exists in performance but not always. You may check this article for more details - http://blog.sqlauthority.c
try this,
select * from (
select
a.InvoiceID,
a.CustomerCode,
a.CustName,
a.dimension,
a.dimension2_,
a.dimension3_ ,
a.SALES_AMOUNT,
a.COST_AMOUNT,
a.GROSS_PROFIT,
case
when SALES_AMOUNT =0 and COST_AMOUNT > 0 then -100
when SALES_AMOUNT =0 and COST_AMOUNT < 0 then 100
when SALES_AMOUNT =0 and COST_AMOUNT = 0 then 0
else GROSS_PROFIT/LineAmountMST * 100
END MARGIN_PER
from
(
select
InvoiceID,
CustomerCode,
CustName,
dimension,
dimension2_,
dimension3_,
sum(LineAmountMST) SALES_AMOUNT,
SUM(linecostAmount) COST_AMOUNT,
sum(LineAmountMST- linecostAmount) GROSS_PROFIT
--case
-- when SUM(lineamountmst)=0 and sum(linecostamount)> 0 then -100
-- when SUM(lineamountmst)=0 and sum(linecostamount)< 0 then 100
-- when SUM(lineamountmst)=0 and sum(linecostamount)= 0 then 0
-- else sum(LineAmountMST- linecostAmount)/sum(LineAmountMST)*100
--END MARGIN_PER
FROM
salesdim
where
invoicedate between @startdate and @enddate
and itembuyergroupid = @itembuyergroupid
and itemgroupid = @itemgroupid
and salesoriginid = @salesoriginid
and CustomerCode = @Cust
and dimension = @Dept
and dimension2_ = @CC
and dimension3_ = @Purpose
GROUP BY InvoiceID,CustomerCode,CustName,dimension,dimension2_,dimension3_
) a ) t
where
t.MARGIN_PER between @marginFrom and @marginTo
order by t.MARGIN_PER desc
Hey rushShah,
Isn't that the same as my solution?
You just added select * from (....)
Isn't that the same as my solution?
You just added select * from (....)
I assume salesdim is properly indexed. Have you checked the estimated execution plan to see if SQL Server thinks there is a missing index? Also, I have noticed in my SSRS reports, they all run SIGNIFICANTLY faster if the query processing is in a stored procedure and I just call the SP from the report.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all for your replies. I tried creating stored procedure and creating temp table and summing up outside. But i cant find any difference in performance. The view is not indexed.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi roma, should i use a stored procedure for the query or can i use in the dataset of ssrs report
You will have to use it in Stored Procedure
>>In total there are 752000 rows in the table. <<
That may explain why it is taking so long.
That may explain why it is taking so long.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>We haven't asked what 'long' and 'slow' mean in this context<<
Right, I was hoping that the author would get the hint.
Right, I was hoping that the author would get the hint.
If you want to get some help in finding out if there are missing indexes, you may try the DMVs - http://blogs.msdn.com/b/queryoptteam/archive/2006/04/06/570176.aspx
http://msdn.microsoft.com/en-us/library/ms345434.aspx
Haven't used in SQL 2008 - there might be more improvements to DMVs in there.
http://msdn.microsoft.com/en-us/library/ms345434.aspx
Haven't used in SQL 2008 - there might be more improvements to DMVs in there.
ASKER