@@ -705,7 +705,8 @@ CREATE TABLE #plan_cost
705705(
706706 query_plan_cost DECIMAL (38 ,2 ),
707707 sql_handle VARBINARY (64 ),
708- INDEX px_ix_ids CLUSTERED (sql_handle )
708+ plan_id INT ,
709+ INDEX px_ix_ids CLUSTERED (sql_handle , plan_id)
709710);
710711
711712
@@ -2166,10 +2167,11 @@ WHERE c.n.exist('/p:StmtSimple[@StatementEstRows > 0]') = 1
21662167RAISERROR (N ' Gathering statement costs' , 0 , 1 ) WITH NOWAIT ;
21672168WITH XMLNAMESPACES(' http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p)
21682169INSERT #plan_cost WITH (TABLOCK )
2169- ( query_plan_cost, sql_handle )
2170+ ( query_plan_cost, sql_handle , plan_id )
21702171SELECT DISTINCT
21712172 s .statement .value (' sum(/p:StmtSimple/@StatementSubTreeCost)' , ' float' ) query_plan_cost,
2172- s .sql_handle
2173+ s .sql_handle ,
2174+ s .plan_id
21732175FROM #statements s
21742176OUTER APPLY s .statement .nodes (' /p:StmtSimple' ) AS q(n)
21752177WHERE s .statement .value (' sum(/p:StmtSimple/@StatementSubTreeCost)' , ' float' ) > 0
@@ -2178,15 +2180,16 @@ OPTION (RECOMPILE);
21782180
21792181RAISERROR (N ' Updating statement costs' , 0 , 1 ) WITH NOWAIT ;
21802182WITH pc AS (
2181- SELECT SUM (DISTINCT pc .query_plan_cost ) AS queryplancostsum, pc .sql_handle
2183+ SELECT SUM (DISTINCT pc .query_plan_cost ) AS queryplancostsum, pc .sql_handle , pc . plan_id
21822184 FROM #plan_cost AS pc
2183- GROUP BY pc .sql_handle
2185+ GROUP BY pc .sql_handle , pc . plan_id
21842186 )
21852187 UPDATE b
21862188 SET b .query_cost = ISNULL (pc .queryplancostsum , 0 )
21872189 FROM #working_warnings AS b
21882190 JOIN pc
21892191 ON pc .sql_handle = b .sql_handle
2192+ AND pc .plan_id = b .plan_id
21902193OPTION (RECOMPILE );
21912194
21922195
0 commit comments