I have a SELECT statement (NOT a Stored Procedure) that I am using to create a report in SSRS (Visual Studio 2010).
Parameter @ClassCode is the one that causing a trouble. But in Development it works fine, but when I deploy it to Production it renders forever.
I am assuming it a Parameter Sniffing, and I read about how to fix it inside the Stored Procedure. But I dont have a SP, I am using a SELECT statement.
What would be the workaround for SELECT statement? And what is the difference between environments? Production is much much more powerful. My query below:
;WITH cte1
AS
(
SELECT QuoteID,
AccidentDate,
PolicyNumber,
SUM(PaidLosses) as PaidLosses
FROM tblLossesPlazaCommercialAuto
WHERE InsuredState IN (@State) AND AccidentDate BETWEEN @StartDate AND @EndDate AND TransactionDate <= @EndDate AND Coverage = 'VehicleComprehensive'
GROUP BY QuoteID,
AccidentDate,
PolicyNumber
),
cte3
AS
(
SELECT
cte1.Quoteid,
cte1.PolicyNumber,
cte1.AccidentDate,
cc.TransactionEffectiveDate,
cc.ClassCode,
CASE
WHEN ROW_NUMBER() OVER (PARTITION BY cte1.QuoteID, cte1.PolicyNumber,cc.AccidentDate ORDER BY (SELECT 0))=1 THEN cte1.PaidLosses
ELSE 0
END as PaidLosses--,
FROM cte1 inner join tblClassCodesPlazaCommercial cc
on cte1.PolicyNumber=cc.PolicyNumber
AND cte1.AccidentDate=cc.AccidentDate
AND cc.AccidentDate IS NOT NULL
/* This is the one that gives me problem */
WHERE cc.ClassCode IN (@ClassCode)
)
SELECT SUM(PaidLosses) as PaidLosses, c.YearNum, c.MonthNum
FROM cte3 RIGHT JOIN tblCalendar c ON c.YearNum = YEAR(cte3.AccidentDate) AND c.MonthNum = MONTH(cte3.AccidentDate)
WHERE c.YearNum <>2017
GROUP BY c.YearNum, c.MonthNum
ORDER BY c.YearNum, c.MonthNum
Used Tuning Advisor to see what indexes and statistics needed the workload. After creating those - everything works fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.