Skip to content

Commit 781d102

Browse files
authored
Merge pull request #1121 from BrentOzarULTD/sp_BQS_1120
Fixes plan costing for stored procs
2 parents 9163595 + 974fcc8 commit 781d102

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

sp_BlitzQueryStore.sql

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
21662167
RAISERROR(N'Gathering statement costs', 0, 1) WITH NOWAIT;
21672168
WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p)
21682169
INSERT #plan_cost WITH (TABLOCK)
2169-
( query_plan_cost, sql_handle )
2170+
( query_plan_cost, sql_handle, plan_id )
21702171
SELECT 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
21732175
FROM #statements s
21742176
OUTER APPLY s.statement.nodes('/p:StmtSimple') AS q(n)
21752177
WHERE s.statement.value('sum(/p:StmtSimple/@StatementSubTreeCost)', 'float') > 0
@@ -2178,15 +2180,16 @@ OPTION (RECOMPILE);
21782180

21792181
RAISERROR(N'Updating statement costs', 0, 1) WITH NOWAIT;
21802182
WITH 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
21902193
OPTION (RECOMPILE);
21912194

21922195

0 commit comments

Comments
 (0)