Skip to content

Commit bed50ea

Browse files
authored
Merge pull request #1080 from BrentOzarULTD/sp_BlitzCache_Multi
sp_BlitzCache -- row estimate mismatch and forced serial issues
2 parents 860cd76 + 8ce3d3f commit bed50ea

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

sp_BlitzCache.sql

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,11 @@ BEGIN
380380
SELECT N'@OnlySqlHandles',
381381
N'VARCHAR(MAX)',
382382
N'One or more sql_handles to use for filtering results.'
383+
384+
UNION ALL
385+
SELECT N'@IgnoreSqlHandles',
386+
N'VARCHAR(MAX)',
387+
N'One or more sql_handles to ignore.'
383388

384389
UNION ALL
385390
SELECT N'@DatabaseName',
@@ -2170,18 +2175,20 @@ table_dml AS (
21702175

21712176

21722177
RAISERROR(N'Gathering row estimates', 0, 1) WITH NOWAIT;
2173-
WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ),
2174-
est_rows AS(
2175-
SELECT s.SqlHandle, c.n.value('(/p:StmtSimple/@StatementEstRows)[1]', 'FLOAT') AS estimated_rows
2178+
WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p )
2179+
SELECT DISTINCT
2180+
CONVERT(BINARY(8), RIGHT('0000000000000000' + SUBSTRING(c.n.value('@QueryHash', 'VARCHAR(18)'), 3, 18), 16), 2) AS QueryHash,
2181+
c.n.value('(/p:StmtSimple/@StatementEstRows)[1]', 'FLOAT') AS estimated_rows
2182+
INTO #est_rows
21762183
FROM #statements AS s
2177-
CROSS APPLY s.statement.nodes('//p:StmtSimple') AS c(n)
2184+
CROSS APPLY s.statement.nodes('/p:StmtSimple') AS c(n)
21782185
WHERE c.n.exist('/p:StmtSimple[@StatementEstRows > 0]') = 1
2179-
)
2186+
21802187
UPDATE b
21812188
SET b.estimated_rows = er.estimated_rows
21822189
FROM ##bou_BlitzCacheProcs AS b
2183-
JOIN est_rows er
2184-
ON er.SqlHandle = b.SqlHandle
2190+
JOIN #est_rows er
2191+
ON er.QueryHash = b.QueryHash
21852192
WHERE b.SPID = @@SPID
21862193
AND b.QueryType = 'Statement'
21872194
OPTION (RECOMPILE);
@@ -2605,7 +2612,7 @@ BEGIN
26052612
WHERE qp.SqlHandle = ##bou_BlitzCacheProcs.SqlHandle
26062613
AND SPID = @@SPID
26072614
AND query_plan.exist('/p:QueryPlan/@NonParallelPlanReason') = 1
2608-
AND ##bou_BlitzCacheProcs.is_parallel IS NULL
2615+
AND (##bou_BlitzCacheProcs.is_parallel = 0 OR ##bou_BlitzCacheProcs.is_parallel IS NULL)
26092616
OPTION (RECOMPILE);
26102617

26112618

sp_BlitzQueryStore.sql

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,16 @@ CREATE TABLE #plan_cost
708708
);
709709

710710

711+
DROP TABLE IF EXISTS #est_rows;
712+
713+
CREATE TABLE #est_rows
714+
(
715+
estimated_rows DECIMAL(38,2),
716+
query_hash BINARY(8),
717+
INDEX px_ix_ids CLUSTERED (query_hash)
718+
);
719+
720+
711721
DROP TABLE IF EXISTS #stats_agg;
712722

713723
CREATE TABLE #stats_agg
@@ -2134,21 +2144,23 @@ table_dml AS (
21342144
OPTION (RECOMPILE);
21352145

21362146
RAISERROR(N'Gathering row estimates', 0, 1) WITH NOWAIT;
2137-
WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ),
2138-
est_rows AS(
2139-
SELECT s.sql_handle, c.n.value('(/p:StmtSimple/@StatementEstRows)[1]', 'FLOAT') AS estimated_rows
2147+
WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p )
2148+
INSERT #est_rows (query_hash, estimated_rows)
2149+
SELECT DISTINCT
2150+
CONVERT(BINARY(8), RIGHT('0000000000000000' + SUBSTRING(c.n.value('@QueryHash', 'VARCHAR(18)'), 3, 18), 16), 2) AS query_hash,
2151+
c.n.value('(/p:StmtSimple/@StatementEstRows)[1]', 'FLOAT') AS estimated_rows
21402152
FROM #statements AS s
2141-
CROSS APPLY s.statement.nodes('//p:StmtSimple') AS c(n)
2153+
CROSS APPLY s.statement.nodes('/p:StmtSimple') AS c(n)
21422154
WHERE c.n.exist('/p:StmtSimple[@StatementEstRows > 0]') = 1
2143-
)
2155+
21442156
UPDATE b
21452157
SET b.estimated_rows = er.estimated_rows
21462158
FROM #working_warnings AS b
2147-
JOIN est_rows er
2148-
ON er.sql_handle = b.sql_handle
2149-
WHERE b.proc_or_function_name = 'Statement'
2159+
JOIN #est_rows er
2160+
ON er.query_hash = b.query_hash
21502161
OPTION (RECOMPILE);
21512162

2163+
21522164
/*Begin plan cost calculations*/
21532165
RAISERROR(N'Gathering statement costs', 0, 1) WITH NOWAIT;
21542166
WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p)
@@ -2835,7 +2847,7 @@ WITH x AS (
28352847
SELECT wpt.database_name, ww.query_cost, wm.plan_id, wm.query_id, wpt.query_sql_text, wm.proc_or_function_name, wpt.query_plan_xml, ww.warnings, wpt.pattern,
28362848
wm.parameter_sniffing_symptoms, wpt.top_three_waits, wm.count_executions, wm.count_compiles, wm.total_cpu_time, wm.avg_cpu_time,
28372849
wm.total_duration, wm.avg_duration, wm.total_logical_io_reads, wm.avg_logical_io_reads,
2838-
wm.total_physical_io_reads, wm.avg_physical_io_reads, wm.total_logical_io_writes, wm.avg_logical_io_writes,
2850+
wm.total_physical_io_reads, wm.avg_physical_io_reads, wm.total_logical_io_writes, wm.avg_logical_io_writes, wm.total_rowcount, wm.avg_rowcount,
28392851
wm.total_query_max_used_memory, wm.avg_query_max_used_memory, wm.total_tempdb_space_used, wm.avg_tempdb_space_used,
28402852
wm.total_log_bytes_used, wm.avg_log_bytes_used, wm.total_num_physical_io_reads, wm.avg_num_physical_io_reads,
28412853
wm.first_execution_time, wm.last_execution_time, wpt.last_force_failure_reason_desc, wpt.context_settings, ROW_NUMBER() OVER (PARTITION BY wm.plan_id, wm.query_id, wm.last_execution_time ORDER BY wm.plan_id) AS rn
@@ -2864,7 +2876,7 @@ WITH x AS (
28642876
SELECT wpt.database_name, ww.query_cost, wm.plan_id, wm.query_id, wpt.query_sql_text, wm.proc_or_function_name, wpt.query_plan_xml, ww.warnings, wpt.pattern,
28652877
wm.parameter_sniffing_symptoms, wpt.last_force_failure_reason_desc, wpt.top_three_waits, wm.count_executions, wm.count_compiles, wm.total_cpu_time, wm.avg_cpu_time,
28662878
wm.total_duration, wm.avg_duration, wm.total_logical_io_reads, wm.avg_logical_io_reads,
2867-
wm.total_physical_io_reads, wm.avg_physical_io_reads, wm.total_logical_io_writes, wm.avg_logical_io_writes,
2879+
wm.total_physical_io_reads, wm.avg_physical_io_reads, wm.total_logical_io_writes, wm.avg_logical_io_writes, wm.total_rowcount, wm.avg_rowcount,
28682880
wm.total_query_max_used_memory, wm.avg_query_max_used_memory, wm.total_tempdb_space_used, wm.avg_tempdb_space_used,
28692881
wm.total_log_bytes_used, wm.avg_log_bytes_used, wm.total_num_physical_io_reads, wm.avg_num_physical_io_reads,
28702882
wm.first_execution_time, wm.last_execution_time, wpt.context_settings, ROW_NUMBER() OVER (PARTITION BY wm.plan_id, wm.query_id, wm.last_execution_time ORDER BY wm.plan_id) AS rn
@@ -2897,7 +2909,7 @@ WITH x AS (
28972909
SELECT wpt.database_name, ww.query_cost, wm.plan_id, wm.query_id, wpt.query_sql_text, wm.proc_or_function_name, ww.warnings, wpt.pattern,
28982910
wm.parameter_sniffing_symptoms, wpt.last_force_failure_reason_desc, wpt.top_three_waits, wm.count_executions, wm.count_compiles, wm.total_cpu_time, wm.avg_cpu_time,
28992911
wm.total_duration, wm.avg_duration, wm.total_logical_io_reads, wm.avg_logical_io_reads,
2900-
wm.total_physical_io_reads, wm.avg_physical_io_reads, wm.total_logical_io_writes, wm.avg_logical_io_writes,
2912+
wm.total_physical_io_reads, wm.avg_physical_io_reads, wm.total_logical_io_writes, wm.avg_logical_io_writes, wm.total_rowcount, wm.avg_rowcount,
29012913
wm.total_query_max_used_memory, wm.avg_query_max_used_memory, wm.total_tempdb_space_used, wm.avg_tempdb_space_used,
29022914
wm.total_log_bytes_used, wm.avg_log_bytes_used, wm.total_num_physical_io_reads, wm.avg_num_physical_io_reads,
29032915
wm.first_execution_time, wm.last_execution_time, wpt.context_settings, ROW_NUMBER() OVER (PARTITION BY wm.plan_id, wm.query_id, wm.last_execution_time ORDER BY wm.plan_id) AS rn
@@ -2926,7 +2938,7 @@ WITH x AS (
29262938
SELECT wpt.database_name, wm.plan_id, wm.query_id, wpt.query_sql_text, wpt.query_plan_xml, wpt.pattern,
29272939
wm.parameter_sniffing_symptoms, wpt.top_three_waits, wm.count_executions, wm.count_compiles, wm.total_cpu_time, wm.avg_cpu_time,
29282940
wm.total_duration, wm.avg_duration, wm.total_logical_io_reads, wm.avg_logical_io_reads,
2929-
wm.total_physical_io_reads, wm.avg_physical_io_reads, wm.total_logical_io_writes, wm.avg_logical_io_writes,
2941+
wm.total_physical_io_reads, wm.avg_physical_io_reads, wm.total_logical_io_writes, wm.avg_logical_io_writes, wm.total_rowcount, wm.avg_rowcount,
29302942
wm.total_query_max_used_memory, wm.avg_query_max_used_memory, wm.total_tempdb_space_used, wm.avg_tempdb_space_used,
29312943
wm.total_log_bytes_used, wm.avg_log_bytes_used, wm.total_num_physical_io_reads, wm.avg_num_physical_io_reads,
29322944
wm.first_execution_time, wm.last_execution_time, wpt.last_force_failure_reason_desc, wpt.context_settings, ROW_NUMBER() OVER (PARTITION BY wm.plan_id, wm.query_id, wm.last_execution_time ORDER BY wm.plan_id) AS rn
@@ -3907,6 +3919,10 @@ SELECT '#plan_cost' AS table_name, *
39073919
FROM #plan_cost AS pc
39083920
OPTION (RECOMPILE);
39093921

3922+
SELECT '#est_rows' AS table_name, *
3923+
FROM #est_rows AS er
3924+
OPTION (RECOMPILE);
3925+
39103926
END;
39113927

39123928
END TRY

0 commit comments

Comments
 (0)