-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathWhatsUpLocks.sql
More file actions
181 lines (172 loc) · 5.18 KB
/
WhatsUpLocks.sql
File metadata and controls
181 lines (172 loc) · 5.18 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
SET ANSI_NULLS ON;
SET ANSI_PADDING ON;
SET ANSI_WARNINGS ON;
SET ARITHABORT ON;
SET CONCAT_NULL_YIELDS_NULL ON;
SET QUOTED_IDENTIFIER ON;
SET NUMERIC_ROUNDABORT OFF;
SET IMPLICIT_TRANSACTIONS OFF;
SET STATISTICS TIME, IO OFF;
GO
/*
This is a helper function I use in some of my presentations to look at locks taken.
It's definitely not a replacement for sp_WhoIsActive, it just gives me what I care about at the moment.
https://code.erikdarling.com
Copyright (c) 2025 Darling Data, LLC
https://www.erikdarling.com/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
IF OBJECT_ID(N'dbo.WhatsUpLocks', N'IF') IS NULL
BEGIN
DECLARE
@fsql nvarchar(MAX) = N'
CREATE FUNCTION
dbo.WhatsUpLocks()
RETURNS TABLE
AS
RETURN
SELECT
x = 138;';
PRINT @fsql;
EXECUTE (@fsql);
END;
GO
ALTER FUNCTION
dbo.WhatsUpLocks
(
@spid integer,
@outsider bit = 'false'
)
RETURNS
table
AS
RETURN
SELECT TOP (9223372036854775807)
dtl.request_session_id,
blocked_by =
ISNULL
(
(
SELECT
der.blocking_session_id
FROM sys.dm_exec_requests AS der
WITH(NOLOCK)
WHERE dtl.request_session_id = der.session_id
),
0
),
dtl.request_mode,
l.locked_object,
index_name = ISNULL(i.name, N'OBJECT'),
dtl.resource_type,
dtl.request_status,
dtl.request_owner_type,
hobt_lock_count =
SUM
(
CASE
WHEN p.hobt_id IS NOT NULL
THEN dtl.lock_count
ELSE 0
END
),
object_locks =
SUM
(
CASE
WHEN dtl.resource_type = N'OBJECT'
THEN dtl.lock_count
ELSE 0
END
),
page_locks =
SUM
(
CASE
WHEN dtl.resource_type = N'PAGE'
THEN dtl.lock_count
ELSE 0
END
),
row_locks =
SUM
(
CASE
WHEN dtl.resource_type IN (N'RID', N'KEY')
THEN dtl.lock_count
ELSE 0
END
),
total_locks = SUM(dtl.lock_count)
FROM
(
SELECT
dtl.request_session_id,
dtl.request_mode,
dtl.resource_type,
dtl.request_status,
dtl.request_owner_type,
dtl.resource_associated_entity_id,
lock_count = COUNT_BIG(*)
FROM sys.dm_tran_locks AS dtl
WITH(NOLOCK)
WHERE (dtl.request_session_id = @spid OR @spid IS NULL)
AND (dtl.request_request_id = CURRENT_REQUEST_ID() OR @spid IS NULL OR @outsider = 'true')
AND (dtl.request_owner_id = CURRENT_TRANSACTION_ID() OR @spid IS NULL OR @outsider = 'true')
AND dtl.resource_type <> N'DATABASE'
GROUP BY
dtl.request_session_id,
dtl.request_mode,
dtl.resource_type,
dtl.request_status,
dtl.request_owner_type,
dtl.resource_associated_entity_id
) AS dtl
LEFT JOIN sys.partitions AS p
WITH(NOLOCK)
ON dtl.resource_associated_entity_id = p.hobt_id
OUTER APPLY
(
SELECT TOP (1)
locked_object = ao.name
FROM sys.all_objects AS ao
WITH(NOLOCK)
WHERE dtl.resource_type = N'OBJECT'
AND dtl.resource_associated_entity_id = ao.object_id
UNION ALL
SELECT TOP (1)
locked_object = ao.name
FROM sys.all_objects AS ao
WITH(NOLOCK)
WHERE dtl.resource_type <> N'OBJECT'
AND p.object_id = ao.object_id
) AS l
OUTER APPLY
(
SELECT TOP (1)
i.name
FROM sys.indexes AS i
WITH(NOLOCK)
WHERE i.object_id = p.object_id
AND i.index_id = p.index_id
) AS i
WHERE l.locked_object <> N'WhatsUpLocks'
GROUP BY
dtl.request_session_id,
dtl.resource_type,
dtl.request_mode,
dtl.request_status,
dtl.request_owner_type,
l.locked_object,
i.name
ORDER BY
dtl.request_session_id,
l.locked_object,
index_name,
total_locks DESC;