Skip to content

Commit 95a7d8c

Browse files
authored
Merge pull request #3365 from erikdarlingdata/issue_3329_3347
Update sp_BlitzLock.sql
2 parents 94bafa0 + 9bb31e0 commit 95a7d8c

File tree

1 file changed

+82
-44
lines changed

1 file changed

+82
-44
lines changed

sp_BlitzLock.sql

Lines changed: 82 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ BEGIN
314314
RETURN;
315315
END;
316316
END;
317-
317+
318318
IF @Azure = 1
319319
BEGIN
320320
IF NOT EXISTS
@@ -1844,7 +1844,7 @@ BEGIN
18441844
COUNT_BIG(DISTINCT dp.event_date)
18451845
) +
18461846
N' deadlocks.',
1847-
sort_order =
1847+
sort_order =
18481848
ROW_NUMBER()
18491849
OVER (ORDER BY COUNT_BIG(DISTINCT dp.event_date) DESC)
18501850
FROM #deadlock_process AS dp
@@ -1878,7 +1878,18 @@ BEGIN
18781878
check_id = 2,
18791879
dow.database_name,
18801880
object_name =
1881-
N'You Might Need RCSI',
1881+
CASE
1882+
WHEN EXISTS
1883+
(
1884+
SELECT
1885+
1/0
1886+
FROM sys.databases AS d
1887+
WHERE d.name = dow.database_name
1888+
AND d.is_read_committed_snapshot_on = 1
1889+
)
1890+
THEN N'You already enabled RCSI, but...'
1891+
ELSE N'You Might Need RCSI'
1892+
END,
18821893
finding_group = N'Total Deadlocks Involving Selects',
18831894
finding =
18841895
N'There have been ' +
@@ -1888,7 +1899,7 @@ BEGIN
18881899
COUNT_BIG(DISTINCT dow.event_date)
18891900
) +
18901901
N' deadlock(s) between read queries and modification queries.',
1891-
sort_order =
1902+
sort_order =
18921903
ROW_NUMBER()
18931904
OVER (ORDER BY COUNT_BIG(DISTINCT dow.event_date) DESC)
18941905
FROM #deadlock_owner_waiter AS dow
@@ -1950,7 +1961,7 @@ BEGIN
19501961
COUNT_BIG(DISTINCT dow.event_date)
19511962
) +
19521963
N' deadlock(s).',
1953-
sort_order =
1964+
sort_order =
19541965
ROW_NUMBER()
19551966
OVER (ORDER BY COUNT_BIG(DISTINCT dow.event_date) DESC)
19561967
FROM #deadlock_owner_waiter AS dow
@@ -1993,7 +2004,7 @@ BEGIN
19932004
COUNT_BIG(DISTINCT dow.event_date)
19942005
) +
19952006
N' deadlock(s).',
1996-
sort_order =
2007+
sort_order =
19972008
ROW_NUMBER()
19982009
OVER (ORDER BY COUNT_BIG(DISTINCT dow.event_date) DESC)
19992010
FROM #deadlock_owner_waiter AS dow
@@ -2042,7 +2053,7 @@ BEGIN
20422053
COUNT_BIG(DISTINCT dow.event_date)
20432054
) +
20442055
N' deadlock(s).',
2045-
sort_order =
2056+
sort_order =
20462057
ROW_NUMBER()
20472058
OVER (ORDER BY COUNT_BIG(DISTINCT dow.event_date) DESC)
20482059
FROM #deadlock_owner_waiter AS dow
@@ -2091,7 +2102,7 @@ BEGIN
20912102
COUNT_BIG(DISTINCT dp.event_date)
20922103
) +
20932104
N' instances of Serializable deadlocks.',
2094-
sort_order =
2105+
sort_order =
20952106
ROW_NUMBER()
20962107
OVER (ORDER BY COUNT_BIG(DISTINCT dp.event_date) DESC)
20972108
FROM #deadlock_process AS dp
@@ -2134,7 +2145,7 @@ BEGIN
21342145
COUNT_BIG(DISTINCT dp.event_date)
21352146
) +
21362147
N' instances of Repeatable Read deadlocks.',
2137-
sort_order =
2148+
sort_order =
21382149
ROW_NUMBER()
21392150
OVER (ORDER BY COUNT_BIG(DISTINCT dp.event_date) DESC)
21402151
FROM #deadlock_process AS dp
@@ -2196,7 +2207,7 @@ BEGIN
21962207
N'UNKNOWN'
21972208
) +
21982209
N'.',
2199-
sort_order =
2210+
sort_order =
22002211
ROW_NUMBER()
22012212
OVER (ORDER BY COUNT_BIG(DISTINCT dp.event_date) DESC)
22022213
FROM #deadlock_process AS dp
@@ -2308,7 +2319,7 @@ BEGIN
23082319
1,
23092320
N''
23102321
) + N' locks.',
2311-
sort_order =
2322+
sort_order =
23122323
ROW_NUMBER()
23132324
OVER (ORDER BY CONVERT(bigint, lt.lock_count) DESC)
23142325
FROM lock_types AS lt
@@ -2487,7 +2498,7 @@ BEGIN
24872498
COUNT_BIG(DISTINCT ds.id)
24882499
) +
24892500
N' deadlocks.',
2490-
sort_order =
2501+
sort_order =
24912502
ROW_NUMBER()
24922503
OVER (ORDER BY COUNT_BIG(DISTINCT ds.id) DESC)
24932504
FROM #deadlock_stack AS ds
@@ -2588,19 +2599,19 @@ BEGIN
25882599
)
25892600
),
25902601
wait_time_hms =
2591-
/*the more wait time you rack up the less accurate this gets,
2602+
/*the more wait time you rack up the less accurate this gets,
25922603
it's either that or erroring out*/
2593-
CASE
2594-
WHEN
2604+
CASE
2605+
WHEN
25952606
SUM
25962607
(
25972608
CONVERT
25982609
(
2599-
bigint,
2610+
bigint,
26002611
dp.wait_time
26012612
)
26022613
)/1000 > 2147483647
2603-
THEN
2614+
THEN
26042615
CONVERT
26052616
(
26062617
nvarchar(30),
@@ -2613,7 +2624,7 @@ BEGIN
26132624
(
26142625
CONVERT
26152626
(
2616-
bigint,
2627+
bigint,
26172628
dp.wait_time
26182629
)
26192630
)
@@ -2624,16 +2635,16 @@ BEGIN
26242635
),
26252636
14
26262637
)
2627-
WHEN
2638+
WHEN
26282639
SUM
26292640
(
26302641
CONVERT
26312642
(
2632-
bigint,
2643+
bigint,
26332644
dp.wait_time
26342645
)
26352646
) BETWEEN 2147483648 AND 2147483647000
2636-
THEN
2647+
THEN
26372648
CONVERT
26382649
(
26392650
nvarchar(30),
@@ -2646,7 +2657,7 @@ BEGIN
26462657
(
26472658
CONVERT
26482659
(
2649-
bigint,
2660+
bigint,
26502661
dp.wait_time
26512662
)
26522663
)
@@ -2728,7 +2739,7 @@ BEGIN
27282739
14
27292740
) +
27302741
N' [dd hh:mm:ss:ms] of deadlock wait time.',
2731-
sort_order =
2742+
sort_order =
27322743
ROW_NUMBER()
27332744
OVER (ORDER BY cs.total_waits DESC)
27342745
FROM chopsuey AS cs
@@ -2802,19 +2813,19 @@ BEGIN
28022813
)
28032814
) +
28042815
N' ' +
2805-
/*the more wait time you rack up the less accurate this gets,
2816+
/*the more wait time you rack up the less accurate this gets,
28062817
it's either that or erroring out*/
2807-
CASE
2808-
WHEN
2818+
CASE
2819+
WHEN
28092820
SUM
28102821
(
28112822
CONVERT
28122823
(
2813-
bigint,
2824+
bigint,
28142825
wt.total_wait_time_ms
28152826
)
28162827
)/1000 > 2147483647
2817-
THEN
2828+
THEN
28182829
CONVERT
28192830
(
28202831
nvarchar(30),
@@ -2827,7 +2838,7 @@ BEGIN
28272838
(
28282839
CONVERT
28292840
(
2830-
bigint,
2841+
bigint,
28312842
wt.total_wait_time_ms
28322843
)
28332844
)
@@ -2838,16 +2849,16 @@ BEGIN
28382849
),
28392850
14
28402851
)
2841-
WHEN
2852+
WHEN
28422853
SUM
28432854
(
28442855
CONVERT
28452856
(
2846-
bigint,
2857+
bigint,
28472858
wt.total_wait_time_ms
28482859
)
28492860
) BETWEEN 2147483648 AND 2147483647000
2850-
THEN
2861+
THEN
28512862
CONVERT
28522863
(
28532864
nvarchar(30),
@@ -2860,7 +2871,7 @@ BEGIN
28602871
(
28612872
CONVERT
28622873
(
2863-
bigint,
2874+
bigint,
28642875
wt.total_wait_time_ms
28652876
)
28662877
)
@@ -2893,7 +2904,7 @@ BEGIN
28932904
14
28942905
) END +
28952906
N' [dd hh:mm:ss:ms] of deadlock wait time.',
2896-
sort_order =
2907+
sort_order =
28972908
ROW_NUMBER()
28982909
OVER (ORDER BY SUM(CONVERT(bigint, wt.total_wait_time_ms)) DESC)
28992910
FROM wait_time AS wt
@@ -2931,7 +2942,7 @@ BEGIN
29312942
N'There have been ' +
29322943
RTRIM(COUNT_BIG(DISTINCT aj.event_date)) +
29332944
N' deadlocks from this Agent Job and Step.',
2934-
sort_order =
2945+
sort_order =
29352946
ROW_NUMBER()
29362947
OVER (ORDER BY COUNT_BIG(DISTINCT aj.event_date) DESC)
29372948
FROM #agent_job AS aj
@@ -2977,7 +2988,7 @@ BEGIN
29772988

29782989
/*Check 15 is total deadlocks involving sleeping sessions*/
29792990
SET @d = CONVERT(varchar(40), GETDATE(), 109);
2980-
RAISERROR('Check 15 sleeping deadlocks %s', 0, 1, @d) WITH NOWAIT;
2991+
RAISERROR('Check 15 sleeping and background deadlocks %s', 0, 1, @d) WITH NOWAIT;
29812992

29822993
INSERT
29832994
#deadlock_findings WITH(TABLOCKX)
@@ -3006,6 +3017,33 @@ BEGIN
30063017
HAVING COUNT_BIG(DISTINCT dp.event_date) > 0
30073018
OPTION(RECOMPILE);
30083019

3020+
INSERT
3021+
#deadlock_findings WITH(TABLOCKX)
3022+
(
3023+
check_id,
3024+
database_name,
3025+
object_name,
3026+
finding_group,
3027+
finding
3028+
)
3029+
SELECT
3030+
check_id = 15,
3031+
database_name = N'-',
3032+
object_name = N'-',
3033+
finding_group = N'Total deadlocks involving background processes',
3034+
finding =
3035+
N'There have been ' +
3036+
CONVERT
3037+
(
3038+
nvarchar(20),
3039+
COUNT_BIG(DISTINCT dp.event_date)
3040+
) +
3041+
N' deadlocks with background task.'
3042+
FROM #deadlock_process AS dp
3043+
WHERE dp.status = N'background'
3044+
HAVING COUNT_BIG(DISTINCT dp.event_date) > 0
3045+
OPTION(RECOMPILE);
3046+
30093047
RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT;
30103048

30113049
/*Check 16 is total deadlocks involving implicit transactions*/
@@ -3684,18 +3722,18 @@ BEGIN
36843722
BEGIN
36853723
SET @d = CONVERT(varchar(40), GETDATE(), 109);
36863724
RAISERROR('Results to client %s', 0, 1, @d) WITH NOWAIT;
3687-
3725+
36883726
IF @Debug = 1 BEGIN SET STATISTICS XML ON; END;
3689-
3727+
36903728
EXEC sys.sp_executesql
36913729
@deadlock_result;
3692-
3730+
36933731
IF @Debug = 1
36943732
BEGIN
36953733
SET STATISTICS XML OFF;
36963734
PRINT @deadlock_result;
36973735
END;
3698-
3736+
36993737
RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT;
37003738

37013739
SET @d = CONVERT(varchar(40), GETDATE(), 109);
@@ -3873,10 +3911,10 @@ BEGIN
38733911
OPTION(RECOMPILE, LOOP JOIN, HASH JOIN);
38743912

38753913
RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT;
3876-
3914+
38773915
SET @d = CONVERT(varchar(40), GETDATE(), 109);
38783916
RAISERROR('Returning findings %s', 0, 1, @d) WITH NOWAIT;
3879-
3917+
38803918
SELECT
38813919
df.check_id,
38823920
df.database_name,
@@ -3888,7 +3926,7 @@ BEGIN
38883926
df.check_id,
38893927
df.sort_order
38903928
OPTION(RECOMPILE);
3891-
3929+
38923930
SET @d = CONVERT(varchar(40), GETDATE(), 109);
38933931
RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT;
38943932
END; /*done with output to client app.*/
@@ -3972,7 +4010,7 @@ BEGIN
39724010
table_name = N'#dm_exec_query_stats',
39734011
*
39744012
FROM #dm_exec_query_stats
3975-
OPTION(RECOMPILE);
4013+
OPTION(RECOMPILE);
39764014

39774015
SELECT
39784016
procedure_parameters =

0 commit comments

Comments
 (0)