-
Notifications
You must be signed in to change notification settings - Fork 51
/
Plan Cache queries - warnings.sql
103 lines (98 loc) · 3.14 KB
/
Plan Cache queries - warnings.sql
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
-- Plan Cache queries - warnings
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
-- This script lists all queries in the Plan Cache that contain a warning.
-- From https://www.simple-talk.com/sql/t-sql-programming/checking-the-plan-cache-warnings-for-a-sql-server-database/
-- =============================================
-- Author: Dennes Torres
-- Create date: 01/23/2015
-- Description: Return the query plans in cache for a specific database
-- =============================================
CREATE OR ALTER FUNCTION [dbo].[planCachefromDatabase]
(
-- Add the parameters for the function here
@DatabaseName VARCHAR(50)
)
RETURNS TABLE
AS
RETURN
(
WITH XMLNAMESPACES
(
DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan'
)
SELECT qp.query_plan,
qt.text,
qs.statement_start_offset,
qs.statement_end_offset,
qs.creation_time,
qs.last_execution_time,
qs.execution_count,
qs.total_worker_time,
qs.last_worker_time,
qs.min_worker_time,
qs.max_worker_time,
qs.total_physical_reads,
qs.last_physical_reads,
qs.min_physical_reads,
qs.max_physical_reads,
qs.total_logical_writes,
qs.last_logical_writes,
qs.min_logical_writes,
qs.max_logical_writes,
qs.total_logical_reads,
qs.last_logical_reads,
qs.min_logical_reads,
qs.max_logical_reads,
qs.total_elapsed_time,
qs.last_elapsed_time,
qs.min_elapsed_time,
qs.max_elapsed_time,
qs.total_rows,
qs.last_rows,
qs.min_rows,
qs.max_rows
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) AS qp
WHERE qp.query_plan.exist('//ColumnReference[fn:lower-case(@Database)=fn:lower-case(sql:variable("@DatabaseName"))]') = 1
);
GO
-- =============================================
-- Author: Dennes Torres
-- Create date: 01/24/2015
-- Description: Return the warnings in the query plans in cache
-- =============================================
CREATE OR ALTER FUNCTION [dbo].[FindWarnings]
(
-- Add the parameters for the function here
@DatabaseName VARCHAR(50)
)
RETURNS TABLE
AS
RETURN
(
WITH XMLNAMESPACES
(
DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan'
)
, qry
AS (SELECT PlanCache.[text],
CAST(nos.query('local-name(.)') AS VARCHAR) AS warning,
PlanCache.total_worker_time
FROM dbo.planCachefromDatabase(@DatabaseName) AS PlanCache
CROSS APPLY query_plan.nodes('//Warnings/*')(nos) )
SELECT [text],
warning,
COUNT(*) qtd,
MAX(total_worker_time) total_worker_time
FROM qry
GROUP BY [text],
warning
);
GO
SELECT [text],
warning,
qtd,
total_worker_time
FROM dbo.FindWarnings('[' + DB_NAME() + ']')
ORDER BY total_worker_time DESC;