Skip to content

Commit cd31cc8

Browse files
authored
Merge pull request #1375 from BrentOzarULTD/issue_1349
Closes #1349
2 parents e827768 + 15eae6e commit cd31cc8

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

sp_BlitzCache.sql

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,10 +2688,11 @@ FROM #relop r
26882688
CROSS APPLY r.relop.nodes('//p:Object') AS c(n)
26892689
)
26902690
UPDATE p
2691-
SET is_table_variable = CASE WHEN x.first_char = '@' THEN 1 END
2691+
SET is_table_variable = 1
26922692
FROM ##bou_BlitzCacheProcs AS p
26932693
JOIN x ON x.SqlHandle = p.SqlHandle
26942694
AND SPID = @@SPID
2695+
WHERE x.first_char = '@'
26952696
OPTION (RECOMPILE);
26962697

26972698
RAISERROR(N'Checking for functions', 0, 1) WITH NOWAIT;
@@ -2719,9 +2720,10 @@ SET key_lookup_cost = x.key_lookup_cost
27192720
FROM (
27202721
SELECT
27212722
qs.SqlHandle,
2722-
relop.value('sum(/p:RelOp/@EstimatedTotalSubtreeCost)', 'float') AS key_lookup_cost
2723+
MAX(relop.value('sum(/p:RelOp/@EstimatedTotalSubtreeCost)', 'float')) AS key_lookup_cost
27232724
FROM #relop qs
27242725
WHERE [relop].exist('/p:RelOp/p:IndexScan[(@Lookup[.="1"])]') = 1
2726+
GROUP BY qs.SqlHandle
27252727
) AS x
27262728
WHERE ##bou_BlitzCacheProcs.SqlHandle = x.SqlHandle
27272729
AND SPID = @@SPID
@@ -2735,9 +2737,10 @@ SET remote_query_cost = x.remote_query_cost
27352737
FROM (
27362738
SELECT
27372739
qs.SqlHandle,
2738-
relop.value('sum(/p:RelOp/@EstimatedTotalSubtreeCost)', 'float') AS remote_query_cost
2740+
MAX(relop.value('sum(/p:RelOp/@EstimatedTotalSubtreeCost)', 'float')) AS remote_query_cost
27392741
FROM #relop qs
27402742
WHERE [relop].exist('/p:RelOp[(@PhysicalOp[contains(., "Remote")])]') = 1
2743+
GROUP BY qs.SqlHandle
27412744
) AS x
27422745
WHERE ##bou_BlitzCacheProcs.SqlHandle = x.SqlHandle
27432746
AND SPID = @@SPID
@@ -2746,16 +2749,20 @@ OPTION (RECOMPILE) ;
27462749
RAISERROR(N'Checking for expensive sorts', 0, 1) WITH NOWAIT;
27472750
WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p)
27482751
UPDATE ##bou_BlitzCacheProcs
2749-
SET sort_cost = (x.sort_io + x.sort_cpu)
2752+
SET sort_cost = y.max_sort_cost
27502753
FROM (
2751-
SELECT
2752-
qs.SqlHandle,
2753-
relop.value('sum(/p:RelOp/@EstimateIO)', 'float') AS sort_io,
2754-
relop.value('sum(/p:RelOp/@EstimateCPU)', 'float') AS sort_cpu
2755-
FROM #relop qs
2756-
WHERE [relop].exist('/p:RelOp[(@PhysicalOp[.="Sort"])]') = 1
2757-
) AS x
2758-
WHERE ##bou_BlitzCacheProcs.SqlHandle = x.SqlHandle
2754+
SELECT x.SqlHandle, MAX((x.sort_io + x.sort_cpu)) AS max_sort_cost
2755+
FROM (
2756+
SELECT
2757+
qs.SqlHandle,
2758+
relop.value('sum(/p:RelOp/@EstimateIO)', 'float') AS sort_io,
2759+
relop.value('sum(/p:RelOp/@EstimateCPU)', 'float') AS sort_cpu
2760+
FROM #relop qs
2761+
WHERE [relop].exist('/p:RelOp[(@PhysicalOp[.="Sort"])]') = 1
2762+
) AS x
2763+
GROUP BY x.SqlHandle
2764+
) AS y
2765+
WHERE ##bou_BlitzCacheProcs.SqlHandle = y.SqlHandle
27592766
AND SPID = @@SPID
27602767
OPTION (RECOMPILE) ;
27612768

@@ -3684,8 +3691,8 @@ SET frequent_execution = CASE WHEN ExecutionsPerMinute > @execution_threshold
36843691
long_running = CASE WHEN AverageDuration > @long_running_query_warning_seconds THEN 1
36853692
WHEN max_worker_time > @long_running_query_warning_seconds THEN 1
36863693
WHEN max_elapsed_time > @long_running_query_warning_seconds THEN 1 END,
3687-
is_key_lookup_expensive = CASE WHEN QueryPlanCost > (@ctp / 2) AND key_lookup_cost >= QueryPlanCost * .5 THEN 1 END,
3688-
is_sort_expensive = CASE WHEN QueryPlanCost > (@ctp / 2) AND sort_cost >= QueryPlanCost * .5 THEN 1 END,
3694+
is_key_lookup_expensive = CASE WHEN QueryPlanCost >= (@ctp / 2) AND key_lookup_cost >= QueryPlanCost * .5 THEN 1 END,
3695+
is_sort_expensive = CASE WHEN QueryPlanCost >= (@ctp / 2) AND sort_cost >= QueryPlanCost * .5 THEN 1 END,
36893696
is_remote_query_expensive = CASE WHEN remote_query_cost >= QueryPlanCost * .05 THEN 1 END,
36903697
is_forced_serial = CASE WHEN is_forced_serial = 1 THEN 1 END,
36913698
is_unused_grant = CASE WHEN PercentMemoryGrantUsed <= @memory_grant_warning_percent AND MinGrantKB > @MinMemoryPerQuery THEN 1 END,

sp_BlitzQueryStore.sql

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,13 +2543,14 @@ FROM #relop r
25432543
CROSS APPLY r.relop.nodes('//p:Object') AS c(n)
25442544
)
25452545
UPDATE b
2546-
SET b.is_table_variable = CASE WHEN x.first_char = '@' THEN 1 END
2546+
SET b.is_table_variable = 1
25472547
FROM #working_warnings b
25482548
JOIN x ON x.sql_handle = b.sql_handle
25492549
JOIN #working_metrics AS wm
25502550
ON b.plan_id = wm.plan_id
25512551
AND b.query_id = wm.query_id
25522552
AND wm.batch_sql_handle IS NOT NULL
2553+
WHERE x.first_char = '@'
25532554
OPTION (RECOMPILE);
25542555

25552556

@@ -2578,9 +2579,10 @@ FROM #working_warnings b
25782579
JOIN (
25792580
SELECT
25802581
r.sql_handle,
2581-
r.relop.value('sum(/p:RelOp/@EstimatedTotalSubtreeCost)', 'float') AS key_lookup_cost
2582+
MAX(r.relop.value('sum(/p:RelOp/@EstimatedTotalSubtreeCost)', 'float')) AS key_lookup_cost
25822583
FROM #relop r
25832584
WHERE r.relop.exist('/p:RelOp/p:IndexScan[(@Lookup[.="1"])]') = 1
2585+
GROUP BY r.sql_handle
25842586
) AS x ON x.sql_handle = b.sql_handle
25852587
OPTION (RECOMPILE);
25862588

@@ -2593,27 +2595,34 @@ FROM #working_warnings b
25932595
JOIN (
25942596
SELECT
25952597
r.sql_handle,
2596-
r.relop.value('sum(/p:RelOp/@EstimatedTotalSubtreeCost)', 'float') AS remote_query_cost
2598+
MAX(r.relop.value('sum(/p:RelOp/@EstimatedTotalSubtreeCost)', 'float')) AS remote_query_cost
25972599
FROM #relop r
25982600
WHERE r.relop.exist('/p:RelOp[(@PhysicalOp[contains(., "Remote")])]') = 1
2601+
GROUP BY r.sql_handle
25992602
) AS x ON x.sql_handle = b.sql_handle
26002603
OPTION (RECOMPILE);
26012604

26022605

26032606
RAISERROR(N'Checking for expensive sorts', 0, 1) WITH NOWAIT;
26042607
WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p)
26052608
UPDATE b
2606-
SET b.sort_cost = (x.sort_io + x.sort_cpu)
2609+
SET sort_cost = y.max_sort_cost
26072610
FROM #working_warnings b
26082611
JOIN (
2609-
SELECT
2610-
r.sql_handle,
2611-
r.relop.value('sum(/p:RelOp/@EstimateIO)', 'float') AS sort_io,
2612-
r.relop.value('sum(/p:RelOp/@EstimateCPU)', 'float') AS sort_cpu
2613-
FROM #relop r
2614-
WHERE r.relop.exist('/p:RelOp[(@PhysicalOp[.="Sort"])]') = 1
2615-
) AS x ON x.sql_handle = b.sql_handle
2616-
OPTION (RECOMPILE);
2612+
SELECT x.sql_handle, MAX((x.sort_io + x.sort_cpu)) AS max_sort_cost
2613+
FROM (
2614+
SELECT
2615+
qs.sql_handle,
2616+
relop.value('sum(/p:RelOp/@EstimateIO)', 'float') AS sort_io,
2617+
relop.value('sum(/p:RelOp/@EstimateCPU)', 'float') AS sort_cpu
2618+
FROM #relop qs
2619+
WHERE [relop].exist('/p:RelOp[(@PhysicalOp[.="Sort"])]') = 1
2620+
) AS x
2621+
GROUP BY x.sql_handle
2622+
) AS y
2623+
ON b.sql_handle = y.sql_handle
2624+
OPTION (RECOMPILE) ;
2625+
26172626

26182627

26192628
RAISERROR(N'Checking for icky cursors', 0, 1) WITH NOWAIT;
@@ -3374,8 +3383,8 @@ SET b.frequent_execution = CASE WHEN wm.xpm > @execution_threshold THEN 1 END
33743383
WHEN wm.max_duration > @long_running_query_warning_seconds THEN 1
33753384
WHEN wm.avg_cpu_time > @long_running_query_warning_seconds THEN 1
33763385
WHEN wm.max_cpu_time > @long_running_query_warning_seconds THEN 1 END,
3377-
b.is_key_lookup_expensive = CASE WHEN b.query_cost > (@ctp / 2) AND b.key_lookup_cost >= b.query_cost * .5 THEN 1 END,
3378-
b.is_sort_expensive = CASE WHEN b.query_cost > (@ctp / 2) AND b.sort_cost >= b.query_cost * .5 THEN 1 END,
3386+
b.is_key_lookup_expensive = CASE WHEN b.query_cost >= (@ctp / 2) AND b.key_lookup_cost >= b.query_cost * .5 THEN 1 END,
3387+
b.is_sort_expensive = CASE WHEN b.query_cost >= (@ctp / 2) AND b.sort_cost >= b.query_cost * .5 THEN 1 END,
33793388
b.is_remote_query_expensive = CASE WHEN b.remote_query_cost >= b.query_cost * .05 THEN 1 END,
33803389
b.is_unused_grant = CASE WHEN percent_memory_grant_used <= @memory_grant_warning_percent AND min_grant_kb > @min_memory_per_query THEN 1 END,
33813390
b.long_running_low_cpu = CASE WHEN wm.avg_duration > wm.avg_cpu_time * 4 THEN 1 END,

0 commit comments

Comments
 (0)