Link to home
Start Free TrialLog in
Avatar of AnnaJames77
AnnaJames77

asked on

Slow Query

The query of SSRS report takes long time .

select a.InvoiceID,a.CustomerCode,a.CustName,a.dimension,
a.dimension2_,a.dimension3_ ,
a.SALES_AMOUNT,a.COST_AMOUNT,a.GROSS_PROFIT,a.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 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,CustName,dimension,dimension2_,dimension3_)a
where a.MARGIN_PER between @marginFrom and @marginTo
order by a.MARGIN_PER desc
SOLUTION
Avatar of deighton
deighton
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AnnaJames77
AnnaJames77

ASKER

In total there are 752000 rows in the table.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.com/2010/06/05/sql-server-convert-in-to-exists-performance-talk/
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

Open in new window

Hey rushShah,

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
Avatar of Nick67
Nick67
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>We haven't asked what 'long' and 'slow' mean in this context<<
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.