-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn2.sql
67 lines (67 loc) · 1.76 KB
/
n2.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
SELECT
DB_NAME([DES].[database_id]) AS [dbname],
[DES].[host_name],
[DER].[session_id],
'KILL ' + CAST([DER].[session_id] AS VARCHAR(500)),
[DES].[login_name],
[DER].[blocking_session_id],
(
SELECT TOP 1
[DEST].[text]
FROM
[sys].[dm_exec_connections] AS [DEC]
CROSS APPLY [sys].[dm_exec_sql_text]([DEC].[most_recent_sql_handle]) AS [DEST]
WHERE
[DEC].[session_id] = [DER].[blocking_session_id]
) AS [BlockingQuery],
(
SELECT TOP 1
[DES].[host_name]
FROM
[sys].[dm_exec_sessions] AS [DES]
WHERE
[DES].[session_id] = [DER].[blocking_session_id]
) AS [BlockingQueryHost],
(
SELECT TOP 1
[DES].[status]
FROM
[sys].[dm_exec_sessions] AS [DES]
WHERE
[DES].[session_id] = [DER].[blocking_session_id]
) AS [BlockingQueryState],
[DER].[total_elapsed_time],
[DER].[cpu_time],
[DER].[wait_type],
[DER].[wait_time],
[DER].[status],
[DER].[command],
[DES].[host_name],
[DER].[percent_complete],
[DER].[estimated_completion_time]/1000.0 AS [estimated_completion_time],
(
SELECT TOP 1
SUBSTRING
(
[DEST].[text] , [DER].[statement_start_offset] / 2 + 1,
(
(
CASE WHEN [DER].[statement_end_offset] = -1
THEN ( LEN(CONVERT(NVARCHAR(MAX), [DEST].[text])) * 2 )
ELSE [DER].[statement_end_offset]
END
) - [DER].[statement_start_offset]
) / 2 + 1
)
) AS CurrentSqlStatement ,
[DEST].[text],
qp.query_plan,
[DER].[sql_handle]
FROM
[sys].[dm_exec_requests] AS [DER]
JOIN [sys].[dm_exec_sessions] AS [DES]
ON [DES].[session_id] = [DER].[session_id]
CROSS APPLY [sys].[dm_exec_sql_text]([DER].[sql_handle]) AS [DEST]
CROSS APPLY sys.dm_exec_query_plan([DER].plan_handle) qp
--WHERE [DER].[command] = 'DbccFilesCompact'
ORDER BY [DER].[cpu_time] DESC