-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql-hanging-queries-inspect.sql
61 lines (56 loc) · 1.57 KB
/
sql-hanging-queries-inspect.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
DECLARE @ProcName VARCHAR(200) = 'ListGroupUsersTimesheetStatus'
DECLARE @Attributes TABLE (
AttributeID INT,
Name VARCHAR(200)
)
INSERT INTO @Attributes (AttributeID, Name)
VALUES
(1,'ANSI_PADDING'),
(2,'Parallel Plan'),
(4,'FORCEPLAN'),
(8,'CONCAT_NULL_YIELDS_NULL'),
(16,'ANSI_WARNINGS'),
(32,'ANSI_NULLS'),
(64,'QUOTED_IDENTIFIER'),
(128,'ANSI_NULL_DFLT_ON'),
(256,'ANSI_NULL_DFLT_OFF'),
(512,'NoBrowseTable'),
(1024,'TriggerOneRow'),
(2048,'ResyncQuery'),
(4096,'ARITH_ABORT'),
(8192,'NUMERIC_ROUNDABORT'),
(16384,'DATEFIRST'),
(32768,'DATEFORMAT'),
(65536,'LanguageID'),
(131072,'UPON'),
(262144,'ROWCOUNT')
select o.object_id, s.plan_handle, h.query_plan, att.Names
from sys.objects o
inner join sys.dm_exec_procedure_stats s on o.object_id = s.object_id
cross apply sys.dm_exec_query_plan(s.plan_handle) h
cross apply (
select * from sys.dm_exec_plan_attributes(s.plan_handle) a
where a.attribute = 'set_options'
) so
outer apply (
select STRING_AGG(Name, ', '+CHAR(13)+CHAR(10)) Names
from @Attributes
where AttributeID & CONVERT(INT, so.value) <> 0
) att
where o.object_id = object_id(@ProcName)
select s.plan_handle, h.query_plan, T.text, att.Names
from sys.dm_exec_query_stats s
cross apply sys.dm_exec_query_plan(s.plan_handle) h
cross apply (
select * from sys.dm_exec_plan_attributes(s.plan_handle) a
where a.attribute = 'set_options'
) so
outer apply (
select STRING_AGG(Name, ', '+CHAR(13)+CHAR(10)) Names
from @Attributes
where AttributeID & CONVERT(INT, so.value) <> 0
) att
outer apply (
select text from sys.dm_exec_sql_text(s.sql_handle)
) T
order by text desc