Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 43d772d

Browse files
Easy changes for release
Set nocount on Set read uncommitted Add recompile hints Add version Tidy up help Add documentation
1 parent 97be4b7 commit 43d772d

File tree

1 file changed

+96
-30
lines changed

1 file changed

+96
-30
lines changed

‎sp_BlitzLock.sql‎

Lines changed: 96 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,23 @@ GO
44

55
ALTER PROCEDURE dbo.sp_BlitzLock
66
(
7-
@Top INT = 2147483647, @StartDate DATETIME = '19000101', @EndDate DATETIME = '99991231', @Debug BIT = 0, @EventSessionPath NVARCHAR(256) = NULL, @Help BIT = 0
7+
@Top INT = 2147483647,
8+
@StartDate DATETIME = '19000101',
9+
@EndDate DATETIME = '99991231',
10+
@Debug BIT = 0,
11+
@EventSessionPath NVARCHAR(256) = NULL,
12+
@Help BIT = 0,
13+
@VersionDate DATETIME = NULL OUTPUT
814
)
915
AS
16+
SET NOCOUNT ON;
17+
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
18+
DECLARE @Version VARCHAR(30);
19+
SET @Version = '1.0';
20+
SET @VersionDate = '20171201';
21+
22+
23+
1024
BEGIN
1125

1226
IF @Help = 1 PRINT '
@@ -15,6 +29,31 @@ AS
1529
1630
This script checks for and analyzes deadlocks from the system health session or a custom extended event path
1731
32+
Variables you can use:
33+
@Top: Use if you want to limit the number of deadlocks to return.
34+
This is ordered by event date ascending
35+
36+
@DatabaseName: If you want to filter to a specific database
37+
38+
@StartDate: The date you want to start searching on.
39+
40+
@EndDate: The date you want to stop searching on.
41+
42+
@ObjectName: If you want to filter to a specific able.
43+
The object name has to be fully qualified ''Database.Schema.Table''
44+
45+
@StoredProcName: If you want to search for a single stored proc
46+
47+
@AppName: If you want to filter to a specific application
48+
49+
@HostName: If you want to filter to a specific host
50+
51+
@LoginName: If you want to filter to a specific login
52+
53+
@EventSessionPath: If you want to point this at an XE session rather than the system health session.
54+
55+
56+
1857
To learn more, visit http://FirstResponderKit.org where you can download new
1958
versions for free, watch training videos on how it works, get more info on
2059
the findings, contribute your own code, and more.
@@ -124,7 +163,9 @@ AS
124163
FROM xml
125164
WHERE xml.deadlock_xml.value('(/event/@name)[1]', 'VARCHAR(255)') = 'xml_deadlock_report'
126165
AND xml.deadlock_xml.value('(/event/@timestamp)[1]', 'datetime') >= @StartDate
127-
AND xml.deadlock_xml.value('(/event/@timestamp)[1]', 'datetime') < @EndDate;
166+
AND xml.deadlock_xml.value('(/event/@timestamp)[1]', 'datetime') < @EndDate
167+
ORDER BY xml.deadlock_xml.value('(/event/@timestamp)[1]', 'datetime')
168+
OPTION ( RECOMPILE );
128169

129170
IF @@ROWCOUNT < 1
130171
BEGIN
@@ -161,7 +202,8 @@ AS
161202
INTO #deadlock_process
162203
FROM #deadlock_data AS dd
163204
CROSS APPLY dd.deadlock_xml.nodes('//deadlock/process-list/process') AS ca(dp)
164-
CROSS APPLY dd.deadlock_xml.nodes('//deadlock/process-list/process/inputbuf') AS ca2(ib);
205+
CROSS APPLY dd.deadlock_xml.nodes('//deadlock/process-list/process/inputbuf') AS ca2(ib)
206+
OPTION ( RECOMPILE );
165207

166208

167209
/*Parse execution stack XML*/
@@ -170,14 +212,16 @@ AS
170212
ca.dp.value('@sqlhandle', 'NVARCHAR(128)') AS sql_handle
171213
INTO #deadlock_stack
172214
FROM #deadlock_process AS dp
173-
CROSS APPLY dp.process_xml.nodes('//executionStack/frame') AS ca(dp);
215+
CROSS APPLY dp.process_xml.nodes('//executionStack/frame') AS ca(dp)
216+
OPTION ( RECOMPILE );
174217

175218

176219
/*Grab the full resource list*/
177220
SELECT ca.dp.query('.') AS resource_xml
178221
INTO #deadlock_resource
179222
FROM #deadlock_data AS dd
180-
CROSS APPLY dd.deadlock_xml.nodes('//deadlock/resource-list') AS ca(dp);
223+
CROSS APPLY dd.deadlock_xml.nodes('//deadlock/resource-list') AS ca(dp)
224+
OPTION ( RECOMPILE );
181225

182226

183227
/*This parses object locks*/
@@ -192,7 +236,8 @@ AS
192236
FROM #deadlock_resource AS dr
193237
CROSS APPLY dr.resource_xml.nodes('//resource-list/objectlock') AS ca(dr)
194238
CROSS APPLY ca.dr.nodes('//waiter-list/waiter') AS w(l)
195-
CROSS APPLY ca.dr.nodes('//owner-list/owner') AS o(l);
239+
CROSS APPLY ca.dr.nodes('//owner-list/owner') AS o(l)
240+
OPTION ( RECOMPILE );
196241

197242

198243
/*This parses page locks*/
@@ -207,7 +252,8 @@ AS
207252
FROM #deadlock_resource AS dr
208253
CROSS APPLY dr.resource_xml.nodes('//resource-list/pagelock') AS ca(dr)
209254
CROSS APPLY ca.dr.nodes('//waiter-list/waiter') AS w(l)
210-
CROSS APPLY ca.dr.nodes('//owner-list/owner') AS o(l);
255+
CROSS APPLY ca.dr.nodes('//owner-list/owner') AS o(l)
256+
OPTION ( RECOMPILE );
211257

212258

213259
/*This parses key locks*/
@@ -222,7 +268,8 @@ AS
222268
FROM #deadlock_resource AS dr
223269
CROSS APPLY dr.resource_xml.nodes('//resource-list/keylock') AS ca(dr)
224270
CROSS APPLY ca.dr.nodes('//waiter-list/waiter') AS w(l)
225-
CROSS APPLY ca.dr.nodes('//owner-list/owner') AS o(l);
271+
CROSS APPLY ca.dr.nodes('//owner-list/owner') AS o(l)
272+
OPTION ( RECOMPILE );
226273

227274

228275
/*This parses rid locks*/
@@ -237,7 +284,8 @@ AS
237284
FROM #deadlock_resource AS dr
238285
CROSS APPLY dr.resource_xml.nodes('//resource-list/ridlock') AS ca(dr)
239286
CROSS APPLY ca.dr.nodes('//waiter-list/waiter') AS w(l)
240-
CROSS APPLY ca.dr.nodes('//owner-list/owner') AS o(l);
287+
CROSS APPLY ca.dr.nodes('//owner-list/owner') AS o(l)
288+
OPTION ( RECOMPILE );
241289

242290

243291
/*Begin checks based on parsed values*/
@@ -253,7 +301,8 @@ AS
253301
+ ' deadlocks.',
254302
NULL AS query_text
255303
FROM #deadlock_owner_waiter AS dow
256-
GROUP BY DB_NAME(dow.database_id);
304+
GROUP BY DB_NAME(dow.database_id)
305+
OPTION ( RECOMPILE );
257306

258307
/*Check 2 is deadlocks by object*/
259308

@@ -267,7 +316,8 @@ AS
267316
+ ' deadlocks.',
268317
NULL AS query_text
269318
FROM #deadlock_owner_waiter AS dow
270-
GROUP BY DB_NAME(dow.database_id), dow.object_name;
319+
GROUP BY DB_NAME(dow.database_id), dow.object_name
320+
OPTION ( RECOMPILE );
271321

272322

273323
/*Check 3 looks for Serializable locking*/
@@ -283,7 +333,9 @@ AS
283333
NULL AS query_text
284334
FROM #deadlock_process AS dp
285335
WHERE dp.isolation_level LIKE 'serializable%'
286-
GROUP BY DB_NAME(dp.database_id);
336+
GROUP BY DB_NAME(dp.database_id)
337+
OPTION ( RECOMPILE );
338+
287339

288340
/*Check 4 looks for Repeatable Read locking*/
289341
INSERT #deadlock_findings ( check_id, database_name, object_name, finding_group, finding, query_text )
@@ -298,7 +350,9 @@ AS
298350
NULL AS query_text
299351
FROM #deadlock_process AS dp
300352
WHERE dp.isolation_level LIKE 'repeatable read%'
301-
GROUP BY DB_NAME(dp.database_id);
353+
GROUP BY DB_NAME(dp.database_id)
354+
OPTION ( RECOMPILE );
355+
302356

303357
/*Check 5 breaks down app, host, and login information*/
304358
INSERT #deadlock_findings ( check_id, database_name, object_name, finding_group, finding, query_text )
@@ -317,7 +371,9 @@ AS
317371
AS finding,
318372
NULL AS query_text
319373
FROM #deadlock_process AS dp
320-
GROUP BY DB_NAME(dp.database_id), dp.login_name, dp.client_app, dp.host_name;
374+
GROUP BY DB_NAME(dp.database_id), dp.login_name, dp.client_app, dp.host_name
375+
OPTION ( RECOMPILE );
376+
321377

322378
/*Check 6 breaks down the types of locks (object, page, key, etc.)*/
323379
WITH lock_types AS (
@@ -343,7 +399,9 @@ AS
343399
FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 1, N'')
344400
+ ' locks',
345401
NULL AS query_text
346-
FROM lock_types AS lt;
402+
FROM lock_types AS lt
403+
OPTION ( RECOMPILE );
404+
347405

348406
/*Check 7 gives you more info queries for sp_BlitzCache */
349407
INSERT #deadlock_findings ( check_id, database_name, object_name, finding_group, finding, query_text )
@@ -362,7 +420,9 @@ AS
362420
NULL AS query_text
363421
FROM #deadlock_stack AS ds
364422
JOIN #deadlock_owner_waiter AS dow
365-
ON dow.owner_id = ds.id;
423+
ON dow.owner_id = ds.id
424+
OPTION ( RECOMPILE );
425+
366426

367427
/*Check 8 gives you more info queries for sp_BlitzCache */
368428
WITH bi AS (
@@ -384,7 +444,9 @@ AS
384444
', @TableName = ' + QUOTENAME(bi.table_name, '''') +
385445
';' AS finding,
386446
NULL AS query_text
387-
FROM bi;
447+
FROM bi
448+
OPTION ( RECOMPILE );
449+
388450

389451
/*Check 9 gets total deadlock wait time per database*/
390452
WITH wait_time AS (
@@ -404,7 +466,8 @@ AS
404466
+ ' [d/h/m/s] of deadlock wait time.',
405467
NULL AS query_text
406468
FROM wait_time AS wt
407-
GROUP BY wt.database_name;
469+
GROUP BY wt.database_name
470+
OPTION ( RECOMPILE );
408471

409472

410473
/*Thank you goodnight*/
@@ -499,40 +562,43 @@ AS
499562
JOIN (SELECT TOP 1 * FROM #deadlock_process AS dp) AS owner
500563
ON owner.id = dow.owner_id
501564
JOIN (SELECT TOP 1 * FROM #deadlock_process AS dp) AS waiter
502-
ON waiter.id = dow.owner_id;
565+
ON waiter.id = dow.owner_id
566+
OPTION ( RECOMPILE );
503567

504568

505569

506570
SELECT df.check_id, df.database_name, df.object_name, df.finding_group, df.finding, df.query_text
507571
FROM #deadlock_findings AS df
508-
ORDER BY df.check_id;
572+
ORDER BY df.check_id
573+
OPTION ( RECOMPILE );
509574

510575

511576
IF @Debug = 1
512577
BEGIN
513578

514579
SELECT '#deadlock_data' AS table_name, *
515-
FROM #deadlock_data AS dd;
580+
FROM #deadlock_data AS dd
581+
OPTION ( RECOMPILE );
516582

517583
SELECT '#deadlock_resource' AS table_name, *
518-
FROM #deadlock_resource AS dr;
584+
FROM #deadlock_resource AS dr
585+
OPTION ( RECOMPILE );
519586

520587
SELECT '#deadlock_owner_waiter' AS table_name, *
521-
FROM #deadlock_owner_waiter AS dow;
588+
FROM #deadlock_owner_waiter AS dow
589+
OPTION ( RECOMPILE );
522590

523591
SELECT '#deadlock_process' AS table_name, *
524-
FROM #deadlock_process AS dp;
592+
FROM #deadlock_process AS dp
593+
OPTION ( RECOMPILE );
525594

526595
SELECT '#deadlock_stack' AS table_name, *
527-
FROM #deadlock_stack AS ds;
596+
FROM #deadlock_stack AS ds
597+
OPTION ( RECOMPILE );
528598

529599
END; -- End debug
530600

531601
END; --Final End
532-
GO
533-
534-
535-
536-
537602

603+
GO
538604

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /