-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathplan-cache-analysis.sql
More file actions
59 lines (56 loc) · 1.96 KB
/
plan-cache-analysis.sql
File metadata and controls
59 lines (56 loc) · 1.96 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
-----------------------------------------------------------------
-- Analyze the SQL Server plan cache
-- rudi@babaluga.com, go ahead license
-----------------------------------------------------------------
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
;WITH attr AS (
SELECT plan_handle, pvt.set_options, pvt.sql_handle, pvt.date_format, pvt.date_first
FROM (
SELECT plan_handle, epa.attribute, epa.value
FROM sys.dm_exec_cached_plans
OUTER APPLY sys.dm_exec_plan_attributes(plan_handle) AS epa
WHERE cacheobjtype = 'Compiled Plan'
) AS ecpa
PIVOT (MAX(ecpa.value) FOR ecpa.attribute IN ("set_options", "sql_handle", "date_format", "date_first")) AS pvt
),
qs AS (
SELECT
plan_handle,
MIN(creation_time) AS creation_time,
MAX(last_execution_time) AS last_execution_time,
MAX(last_worker_time) AS last_worker_time,
MAX(last_logical_reads) AS last_logical_reads
FROM sys.dm_exec_query_stats
GROUP BY plan_handle
)
SELECT
ISNULL(DB_NAME(st.dbid),'ResourceDB') AS db,
st.text,
cp.refcounts,
cp.usecounts,
cp.cacheobjtype,
cp.objtype,
cp.pool_id,
cp.size_in_bytes,
qs.creation_time,
qs.last_execution_time,
qs.last_worker_time,
qs.last_logical_reads,
CASE WHEN EXISTS (SELECT * FROM sys.dm_exec_cached_plan_dependent_objects(cp.plan_handle) do WHERE do.cacheobjtype = 'Cursor')
THEN 1
ELSE 0
END as HasCursor,
attr.set_options,
attr.date_format,
attr.date_first,
ce.disk_ios_count, ce.context_switches_count,
qp.query_plan
--ce.original_cost, ce.current_cost,
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st
OUTER APPLY sys.dm_exec_query_plan(cp.plan_handle) qp
LEFT JOIN attr ON cp.plan_handle = attr.plan_handle
LEFT JOIN qs ON cp.plan_handle = qs.plan_handle
LEFT JOIN sys.dm_os_memory_cache_entries ce ON cp.memory_object_address = ce.memory_object_address
OPTION (RECOMPILE, MAXDOP 1);