I wrote a query that has 5 CTEs for my SSRS Report. In SSMS, it takes only 33 seconds, but it takes 5 minutes to render in the report. I tried to use Stored Procedure, indexes in the table, but did not improve. How can I speed up the rendering in the report? Will changing to Temp Tables will help? If so, how to make this into temp tables? Here is the part of my query:
SELECT
a1.Total_Pat,
a1.Total_Ord,
a1.Total_Order_Lines,
a1.Total_Bill_Amt,
a1.Extract_Date,
a2.Total_Unique_Pat_Monthly,
a2.Total_Ord_Monthly,
a2.Total_Order_Lines_Monthly,
a2.Total_Bill_Amt_Monthly,
a2.Trns_Acc_Period,
a3.Tot_Unique_Pat_Row,
a3.Tot_Order_Row,
a3.Total_Order_Lines_Row,
a3.Tot_Billed_Amt_Row
FROM
(select Count(Distinct Patient_Number) AS Total_Pat,
Count(Distinct Order_No) AS Total_Ord,
Count(Line_No) AS Total_Order_Lines,
Sum(Billed_Amt) AS Total_Bill_Amt,
Extract_Date
from Main table1
INNER JOIN Code AS cc ON table1.Cov_Cd_Prim = cc.Code
WHERE (table1.Trns_Acc_Period IN (@AccountYear)) AND (cc.Group1 IN (@CoverageCode))
AND (table1.Prod_Grp IN (@ProductGroup)) AND (table1.L_ACCT IN (@AccountNumber))
AND (table1.Bill_Flag IN (@BillFlag))
AND (table1.HCPCS IN (@HCPCSCode) OR '<ALL>' IN (@HCPCSCode))
GROUP BY table1.Extract_Date
) AS a1,
(SELECT
sum(Total_Unique_Pat_Monthly) AS Total_Unique_Pat_Monthly,
sum(Total_Ord_Monthly) AS Total_Ord_Monthly,
Sum(Total_Order_Lines_Monthly) AS Total_Order_Lines_Monthly,
Sum(Total_Bill_Amt_Monthly) AS Total_Bill_Amt_Monthly,
tmp.Group1,
tmp.Trns_Acc_Period
FROM
(
SELECT cc.Group1,
a.Trns_Acc_Period,
Count(Distinct Patient_Number) AS Total_Unique_Pat_Monthly,
Count(Distinct Order_No) AS Total_Ord_Monthly,
Count(Line_No) AS Total_Order_Lines_Monthly,
Sum(Billed_Amt) AS Total_Bill_Amt_Monthly
FROM Main ord
INNER JOIN Code AS cc ON ord.Cov_Cd_Prim = cc.Code
INNER JOIN Acc_Period_201210_201309 AS a ON a.Trns_Acc_Period = ord.Trns_Acc_Period
AND a.Group1 = cc.Group1
AND a.Cov_Cd_Prim = cc.Code
WHERE (ord.Trns_Acc_Period IN (@AccountYear)) AND (cc.Group1 IN (@CoverageCode))
AND (ord.Prod_Grp IN (@ProductGroup)) AND (ord.L_ACCT IN (@AccountNumber))
AND (ord.Bill_Flag IN (@BillFlag))
AND (ord.HCPCS IN (@HCPCSCode) OR '<ALL>' IN (@HCPCSCode))
GROUP BY a.Trns_Acc_Period, cc.Group1
UNION
SELECT a.Group1, a.Trns_Acc_Period, 0, 0, 0, 0
FROM Code c
INNER JOIN Acc_Period_201210_201309 a ON a.Group1 = c.Group1
) AS tmp
WHERE (tmp.Group1 IN (@CoverageCode))
GROUP BY tmp.Group1, tmp.Trns_Acc_Period
) AS a2,
(select Count(Distinct Patient_Number) AS Tot_Unique_Pat_Row,
Count(Distinct Order_No) AS Tot_Order_Row,
Count(Line_No) AS Total_Order_Lines_Row,
Sum(Billed_Amt) AS Tot_Billed_Amt_Row,
cc.Group1
from Main ord3
INNER JOIN Code AS cc ON ord3.Cov_Cd_Prim = cc.Code
WHERE (ord3.Trns_Acc_Period IN (@AccountYear)) AND (cc.Group1 IN (@CoverageCode))
AND (ord3.Prod_Grp IN (@ProductGroup)) AND (ord3.L_ACCT IN (@AccountNumber))
AND (ord3.Bill_Flag IN (@BillFlag))
AND (ord3.HCPCS IN (@HCPCSCode) OR '<ALL>' IN (@HCPCSCode))
GROUP BY cc.Group1
) AS a3,
Thanks,
Jkrish