forked from ktaranov/sqlserver-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueries_with_parallelism.sql
More file actions
41 lines (37 loc) · 1.67 KB
/
Copy pathQueries_with_parallelism.sql
File metadata and controls
41 lines (37 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Author: Eitan Blumin
Source link: http://www.madeiradata.com/cost-threshold-for-parallelism-and-how-to-increase-it-properly/
*/
DECLARE
@MinUseCount INT = 50 -- Set minimum usecount to ignore rarely-used plans
, @CurrentCostThreshold FLOAT = 5 -- Serves as minimum sub-tree cost
, @MaxSubTreeCost FLOAT = 30 -- Set the maximum sub-tree cost, plans with higher cost than this wouldn't normally interest us
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SELECT @CurrentCostThreshold = CONVERT(FLOAT, value_in_use)
FROM sys.configurations
WHERE [name] = 'cost threshold for parallelism';
WITH XMLNAMESPACES
(DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan')
SELECT *
FROM
(
SELECT
ecp.plan_handle,
CompleteQueryPlan = query_plan,
StatementText = n.value('(@StatementText)[1]', 'VARCHAR(4000)'),
StatementSubTreeCost = n.value('(@StatementSubTreeCost)[1]', 'VARCHAR(128)'),
ParallelSubTreeXML = n.query('.'),
ecp.usecounts,
ecp.size_in_bytes,
RankPerText = ROW_NUMBER() OVER (PARTITION BY n.value('(@StatementText)[1]', 'VARCHAR(4000)') ORDER BY ecp.usecounts DESC)
FROM sys.dm_exec_cached_plans AS ecp
CROSS APPLY sys.dm_exec_query_plan(plan_handle) AS eqp
CROSS APPLY query_plan.nodes('/ShowPlanXML/BatchSequence/Batch/Statements/StmtSimple') AS qn(n)
WHERE n.query('.').exist('//RelOp[@PhysicalOp="Parallelism"]') = 1
AND ecp.usecounts > @MinUseCount
AND n.value('(@StatementSubTreeCost)[1]', 'float') BETWEEN @CurrentCostThreshold AND @MaxSubTreeCost
) AS Q
WHERE
RankPerText = 1 -- This would filter out duplicate statements, returning only those with the highest usecount
ORDER BY
usecounts DESC;