Technical Article

Lock Monitoring Script to summarize active locks

,

A simple script created to summarize locking by spid, by database, by type with more user friendly descriptions.  You can also run for a specific spid.  Hope it's useful. 

create procedure sp_Spec_CheckLocks
@spid smallint = NULL
as

/** Created by AugieBros to monitor active locking for 
a given instance.

This is a simple query designed to summarize locking
for a single spid, or for all spids.  Hope it's useful.  
**/
IF @spid IS NULL 
BEGIN
select req_spid,
convert(varchar(30),b.name) + ' (' + convert(char(2),rsc_dbid) + ') ' as DBName_ID,
convert(char(2), rsc_type) + 
CASE rsc_type
WHEN 1 THEN 'NULL Resource'
WHEN 2 THEN 'Database'
WHEN 3 THEN 'File'
WHEN 4 THEN 'Index'
WHEN 5 THEN 'Table'
WHEN 6 THEN 'Page'
WHEN 7 THEN 'Key'
WHEN 8 THEN 'Extent'
WHEN 9 THEN 'RID'
ELSE 'Unlisted'
END as LockType,
CASE
        WHEN req_status = 1 and req_mode = 1 THEN 'Sch-S (Schema stability) _ Granted'
        WHEN req_status = 1 and req_mode = 2 THEN 'Sch-M (Schema modification) _ Granted'
        WHEN req_status = 1 and req_mode = 3 THEN 'IS (Intent Shared) _ Granted'
        WHEN req_status = 1 and req_mode = 4 THEN 'SIU (Shared Intent Update) _ Granted'
        WHEN req_status = 1 and req_mode = 5 THEN 'IS-S (Intent Shared-Shared) _ Granted'
        WHEN req_status = 1 and req_mode = 6 THEN 'IX (Intent Exclusive) _ Granted'
        WHEN req_status = 1 and req_mode = 7 THEN 'SIX (Shared Intent Exclusive) _ Granted'
        WHEN req_status = 1 and req_mode = 8 THEN 'S (Shared) _ Granted'
        WHEN req_status = 1 and req_mode = 9 THEN 'U (Update) _ Granted'
        WHEN req_status = 1 and req_mode = 10 THEN 'IIn-Nul. (Intent Insert-NULL) _ Granted'
        WHEN req_status = 1 and req_mode = 11 THEN 'IS-X (Intent Shared-Exclusive) _ Granted'
        WHEN req_status = 1 and req_mode = 12 THEN 'IU (Intent Update) _ Granted'
        WHEN req_status = 1 and req_mode = 13 THEN 'IS-U (Intent Shared-Update) _ Granted'
        WHEN req_status = 1 and req_mode = 14 THEN 'X (Exclusive) _ Granted'
        WHEN req_status = 1 and req_mode = 15 THEN 'BU (bulk operations) _ Granted'
        WHEN req_status = 2 and req_mode = 1 THEN 'Sch-S (Schema stability) _ Converting'
        WHEN req_status = 2 and req_mode = 2 THEN 'Sch-M (Schema modification) _ Converting'
        WHEN req_status = 2 and req_mode = 3 THEN 'IS (Intent Shared) _ Converting'
        WHEN req_status = 2 and req_mode = 4 THEN 'SIU (Shared Intent Update) _ Converting'
        WHEN req_status = 2 and req_mode = 5 THEN 'IS-S (Intent Shared-Shared) _ Converting'
        WHEN req_status = 2 and req_mode = 6 THEN 'IX (Intent Exclusive) _ Converting'
        WHEN req_status = 2 and req_mode = 7 THEN 'SIX (Shared Intent Exclusive) _ Converting'
        WHEN req_status = 2 and req_mode = 8 THEN 'S (Shared) _ Converting'
        WHEN req_status = 2 and req_mode = 9 THEN 'U (Update) _ Converting'
        WHEN req_status = 2 and req_mode = 10 THEN 'IIn-Nul. (Intent Insert-NULL) _ Converting'
        WHEN req_status = 2 and req_mode = 11 THEN 'IS-X (Intent Shared-Exclusive) _ Converting'
        WHEN req_status = 2 and req_mode = 12 THEN 'IU (Intent Update) _ Converting'
        WHEN req_status = 2 and req_mode = 13 THEN 'IS-U (Intent Shared-Update) _ Converting'
        WHEN req_status = 2 and req_mode = 14 THEN 'X (Exclusive) _ Converting'
        WHEN req_status = 2 and req_mode = 15 THEN 'BU (bulk operations) _ Converting'
        WHEN req_status = 3 and req_mode = 1 THEN 'Sch-S (Schema stability) _ Waiting'
        WHEN req_status = 3 and req_mode = 2 THEN 'Sch-M (Schema modification) _ Waiting'
        WHEN req_status = 3 and req_mode = 3 THEN 'IS (Intent Shared) _ Waiting'
        WHEN req_status = 3 and req_mode = 4 THEN 'SIU (Shared Intent Update) _ Waiting'
        WHEN req_status = 3 and req_mode = 5 THEN 'IS-S (Intent Shared-Shared) _ Waiting'
        WHEN req_status = 3 and req_mode = 6 THEN 'IX (Intent Exclusive) _ Waiting'
        WHEN req_status = 3 and req_mode = 7 THEN 'SIX (Shared Intent Exclusive) _ Waiting'
        WHEN req_status = 3 and req_mode = 8 THEN 'S (Shared) _ Waiting'
        WHEN req_status = 3 and req_mode = 9 THEN 'U (Update) _ Waiting'
        WHEN req_status = 3 and req_mode = 10 THEN 'IIn-Nul. (Intent Insert-NULL) _ Waiting'
        WHEN req_status = 3 and req_mode = 11 THEN 'IS-X (Intent Shared-Exclusive) _ Waiting'
        WHEN req_status = 3 and req_mode = 12 THEN 'IU (Intent Update) _ Waiting'
        WHEN req_status = 3 and req_mode = 13 THEN 'IS-U (Intent Shared-Update) _ Waiting'
        WHEN req_status = 3 and req_mode = 14 THEN 'X (Exclusive) _ Waiting'
        WHEN req_status = 3 and req_mode = 15 THEN 'BU (bulk operations) _ Waiting'
        ELSE 'Unclassified'
END as LockMode,
count(*) as LockCount
from master..syslockinfo a LEFT JOIN master..sysdatabases b ON a.rsc_dbid = b.dbid
group by req_spid, 
rsc_dbid, 
b.name,
convert(char(2), rsc_type) + 
CASE rsc_type
WHEN 1 THEN 'NULL Resource'
WHEN 2 THEN 'Database'
WHEN 3 THEN 'File'
WHEN 4 THEN 'Index'
WHEN 5 THEN 'Table'
WHEN 6 THEN 'Page'
WHEN 7 THEN 'Key'
WHEN 8 THEN 'Extent'
WHEN 9 THEN 'RID'
ELSE 'Unlisted'
END,
CASE
        WHEN req_status = 1 and req_mode = 1 THEN 'Sch-S (Schema stability) _ Granted'
        WHEN req_status = 1 and req_mode = 2 THEN 'Sch-M (Schema modification) _ Granted'
        WHEN req_status = 1 and req_mode = 3 THEN 'IS (Intent Shared) _ Granted'
        WHEN req_status = 1 and req_mode = 4 THEN 'SIU (Shared Intent Update) _ Granted'
        WHEN req_status = 1 and req_mode = 5 THEN 'IS-S (Intent Shared-Shared) _ Granted'
        WHEN req_status = 1 and req_mode = 6 THEN 'IX (Intent Exclusive) _ Granted'
        WHEN req_status = 1 and req_mode = 7 THEN 'SIX (Shared Intent Exclusive) _ Granted'
        WHEN req_status = 1 and req_mode = 8 THEN 'S (Shared) _ Granted'
        WHEN req_status = 1 and req_mode = 9 THEN 'U (Update) _ Granted'
        WHEN req_status = 1 and req_mode = 10 THEN 'IIn-Nul. (Intent Insert-NULL) _ Granted'
        WHEN req_status = 1 and req_mode = 11 THEN 'IS-X (Intent Shared-Exclusive) _ Granted'
        WHEN req_status = 1 and req_mode = 12 THEN 'IU (Intent Update) _ Granted'
        WHEN req_status = 1 and req_mode = 13 THEN 'IS-U (Intent Shared-Update) _ Granted'
        WHEN req_status = 1 and req_mode = 14 THEN 'X (Exclusive) _ Granted'
        WHEN req_status = 1 and req_mode = 15 THEN 'BU (bulk operations) _ Granted'
        WHEN req_status = 2 and req_mode = 1 THEN 'Sch-S (Schema stability) _ Converting'
        WHEN req_status = 2 and req_mode = 2 THEN 'Sch-M (Schema modification) _ Converting'
        WHEN req_status = 2 and req_mode = 3 THEN 'IS (Intent Shared) _ Converting'
        WHEN req_status = 2 and req_mode = 4 THEN 'SIU (Shared Intent Update) _ Converting'
        WHEN req_status = 2 and req_mode = 5 THEN 'IS-S (Intent Shared-Shared) _ Converting'
        WHEN req_status = 2 and req_mode = 6 THEN 'IX (Intent Exclusive) _ Converting'
        WHEN req_status = 2 and req_mode = 7 THEN 'SIX (Shared Intent Exclusive) _ Converting'
        WHEN req_status = 2 and req_mode = 8 THEN 'S (Shared) _ Converting'
        WHEN req_status = 2 and req_mode = 9 THEN 'U (Update) _ Converting'
        WHEN req_status = 2 and req_mode = 10 THEN 'IIn-Nul. (Intent Insert-NULL) _ Converting'
        WHEN req_status = 2 and req_mode = 11 THEN 'IS-X (Intent Shared-Exclusive) _ Converting'
        WHEN req_status = 2 and req_mode = 12 THEN 'IU (Intent Update) _ Converting'
        WHEN req_status = 2 and req_mode = 13 THEN 'IS-U (Intent Shared-Update) _ Converting'
        WHEN req_status = 2 and req_mode = 14 THEN 'X (Exclusive) _ Converting'
        WHEN req_status = 2 and req_mode = 15 THEN 'BU (bulk operations) _ Converting'
        WHEN req_status = 3 and req_mode = 1 THEN 'Sch-S (Schema stability) _ Waiting'
        WHEN req_status = 3 and req_mode = 2 THEN 'Sch-M (Schema modification) _ Waiting'
        WHEN req_status = 3 and req_mode = 3 THEN 'IS (Intent Shared) _ Waiting'
        WHEN req_status = 3 and req_mode = 4 THEN 'SIU (Shared Intent Update) _ Waiting'
        WHEN req_status = 3 and req_mode = 5 THEN 'IS-S (Intent Shared-Shared) _ Waiting'
        WHEN req_status = 3 and req_mode = 6 THEN 'IX (Intent Exclusive) _ Waiting'
        WHEN req_status = 3 and req_mode = 7 THEN 'SIX (Shared Intent Exclusive) _ Waiting'
        WHEN req_status = 3 and req_mode = 8 THEN 'S (Shared) _ Waiting'
        WHEN req_status = 3 and req_mode = 9 THEN 'U (Update) _ Waiting'
        WHEN req_status = 3 and req_mode = 10 THEN 'IIn-Nul. (Intent Insert-NULL) _ Waiting'
        WHEN req_status = 3 and req_mode = 11 THEN 'IS-X (Intent Shared-Exclusive) _ Waiting'
        WHEN req_status = 3 and req_mode = 12 THEN 'IU (Intent Update) _ Waiting'
        WHEN req_status = 3 and req_mode = 13 THEN 'IS-U (Intent Shared-Update) _ Waiting'
        WHEN req_status = 3 and req_mode = 14 THEN 'X (Exclusive) _ Waiting'
        WHEN req_status = 3 and req_mode = 15 THEN 'BU (bulk operations) _ Waiting'
        ELSE 'Unclassified'
END
order by req_spid, DBName_ID, LockType desc
END

ELSE

BEGIN
select convert(varchar(30),b.name) + ' (' + convert(char(2),rsc_dbid) + ') ' as DBName_ID,
convert(char(2), rsc_type) + 
CASE rsc_type
WHEN 1 THEN 'NULL Resource'
WHEN 2 THEN 'Database'
WHEN 3 THEN 'File'
WHEN 4 THEN 'Index'
WHEN 5 THEN 'Table'
WHEN 6 THEN 'Page'
WHEN 7 THEN 'Key'
WHEN 8 THEN 'Extent'
WHEN 9 THEN 'RID'
ELSE 'Unlisted'
END as LockType,
CASE
        WHEN req_status = 1 and req_mode = 1 THEN 'Sch-S (Schema stability) _ Granted'
        WHEN req_status = 1 and req_mode = 2 THEN 'Sch-M (Schema modification) _ Granted'
        WHEN req_status = 1 and req_mode = 3 THEN 'IS (Intent Shared) _ Granted'
        WHEN req_status = 1 and req_mode = 4 THEN 'SIU (Shared Intent Update) _ Granted'
        WHEN req_status = 1 and req_mode = 5 THEN 'IS-S (Intent Shared-Shared) _ Granted'
        WHEN req_status = 1 and req_mode = 6 THEN 'IX (Intent Exclusive) _ Granted'
        WHEN req_status = 1 and req_mode = 7 THEN 'SIX (Shared Intent Exclusive) _ Granted'
        WHEN req_status = 1 and req_mode = 8 THEN 'S (Shared) _ Granted'
        WHEN req_status = 1 and req_mode = 9 THEN 'U (Update) _ Granted'
        WHEN req_status = 1 and req_mode = 10 THEN 'IIn-Nul. (Intent Insert-NULL) _ Granted'
        WHEN req_status = 1 and req_mode = 11 THEN 'IS-X (Intent Shared-Exclusive) _ Granted'
        WHEN req_status = 1 and req_mode = 12 THEN 'IU (Intent Update) _ Granted'
        WHEN req_status = 1 and req_mode = 13 THEN 'IS-U (Intent Shared-Update) _ Granted'
        WHEN req_status = 1 and req_mode = 14 THEN 'X (Exclusive) _ Granted'
        WHEN req_status = 1 and req_mode = 15 THEN 'BU (bulk operations) _ Granted'
        WHEN req_status = 2 and req_mode = 1 THEN 'Sch-S (Schema stability) _ Converting'
        WHEN req_status = 2 and req_mode = 2 THEN 'Sch-M (Schema modification) _ Converting'
        WHEN req_status = 2 and req_mode = 3 THEN 'IS (Intent Shared) _ Converting'
        WHEN req_status = 2 and req_mode = 4 THEN 'SIU (Shared Intent Update) _ Converting'
        WHEN req_status = 2 and req_mode = 5 THEN 'IS-S (Intent Shared-Shared) _ Converting'
        WHEN req_status = 2 and req_mode = 6 THEN 'IX (Intent Exclusive) _ Converting'
        WHEN req_status = 2 and req_mode = 7 THEN 'SIX (Shared Intent Exclusive) _ Converting'
        WHEN req_status = 2 and req_mode = 8 THEN 'S (Shared) _ Converting'
        WHEN req_status = 2 and req_mode = 9 THEN 'U (Update) _ Converting'
        WHEN req_status = 2 and req_mode = 10 THEN 'IIn-Nul. (Intent Insert-NULL) _ Converting'
        WHEN req_status = 2 and req_mode = 11 THEN 'IS-X (Intent Shared-Exclusive) _ Converting'
        WHEN req_status = 2 and req_mode = 12 THEN 'IU (Intent Update) _ Converting'
        WHEN req_status = 2 and req_mode = 13 THEN 'IS-U (Intent Shared-Update) _ Converting'
        WHEN req_status = 2 and req_mode = 14 THEN 'X (Exclusive) _ Converting'
        WHEN req_status = 2 and req_mode = 15 THEN 'BU (bulk operations) _ Converting'
        WHEN req_status = 3 and req_mode = 1 THEN 'Sch-S (Schema stability) _ Waiting'
        WHEN req_status = 3 and req_mode = 2 THEN 'Sch-M (Schema modification) _ Waiting'
        WHEN req_status = 3 and req_mode = 3 THEN 'IS (Intent Shared) _ Waiting'
        WHEN req_status = 3 and req_mode = 4 THEN 'SIU (Shared Intent Update) _ Waiting'
        WHEN req_status = 3 and req_mode = 5 THEN 'IS-S (Intent Shared-Shared) _ Waiting'
        WHEN req_status = 3 and req_mode = 6 THEN 'IX (Intent Exclusive) _ Waiting'
        WHEN req_status = 3 and req_mode = 7 THEN 'SIX (Shared Intent Exclusive) _ Waiting'
        WHEN req_status = 3 and req_mode = 8 THEN 'S (Shared) _ Waiting'
        WHEN req_status = 3 and req_mode = 9 THEN 'U (Update) _ Waiting'
        WHEN req_status = 3 and req_mode = 10 THEN 'IIn-Nul. (Intent Insert-NULL) _ Waiting'
        WHEN req_status = 3 and req_mode = 11 THEN 'IS-X (Intent Shared-Exclusive) _ Waiting'
        WHEN req_status = 3 and req_mode = 12 THEN 'IU (Intent Update) _ Waiting'
        WHEN req_status = 3 and req_mode = 13 THEN 'IS-U (Intent Shared-Update) _ Waiting'
        WHEN req_status = 3 and req_mode = 14 THEN 'X (Exclusive) _ Waiting'
        WHEN req_status = 3 and req_mode = 15 THEN 'BU (bulk operations) _ Waiting'
        ELSE 'Unclassified'
END as LockMode,
count(*) as LockCount
from master..syslockinfo a LEFT JOIN master..sysdatabases b ON a.rsc_dbid = b.dbid
where req_spid = @spid
group by rsc_dbid, 
b.name,
convert(char(2), rsc_type) + 
CASE rsc_type
WHEN 1 THEN 'NULL Resource'
WHEN 2 THEN 'Database'
WHEN 3 THEN 'File'
WHEN 4 THEN 'Index'
WHEN 5 THEN 'Table'
WHEN 6 THEN 'Page'
WHEN 7 THEN 'Key'
WHEN 8 THEN 'Extent'
WHEN 9 THEN 'RID'
ELSE 'Unlisted'
END,
CASE
        WHEN req_status = 1 and req_mode = 1 THEN 'Sch-S (Schema stability) _ Granted'
        WHEN req_status = 1 and req_mode = 2 THEN 'Sch-M (Schema modification) _ Granted'
        WHEN req_status = 1 and req_mode = 3 THEN 'IS (Intent Shared) _ Granted'
        WHEN req_status = 1 and req_mode = 4 THEN 'SIU (Shared Intent Update) _ Granted'
        WHEN req_status = 1 and req_mode = 5 THEN 'IS-S (Intent Shared-Shared) _ Granted'
        WHEN req_status = 1 and req_mode = 6 THEN 'IX (Intent Exclusive) _ Granted'
        WHEN req_status = 1 and req_mode = 7 THEN 'SIX (Shared Intent Exclusive) _ Granted'
        WHEN req_status = 1 and req_mode = 8 THEN 'S (Shared) _ Granted'
        WHEN req_status = 1 and req_mode = 9 THEN 'U (Update) _ Granted'
        WHEN req_status = 1 and req_mode = 10 THEN 'IIn-Nul. (Intent Insert-NULL) _ Granted'
        WHEN req_status = 1 and req_mode = 11 THEN 'IS-X (Intent Shared-Exclusive) _ Granted'
        WHEN req_status = 1 and req_mode = 12 THEN 'IU (Intent Update) _ Granted'
        WHEN req_status = 1 and req_mode = 13 THEN 'IS-U (Intent Shared-Update) _ Granted'
        WHEN req_status = 1 and req_mode = 14 THEN 'X (Exclusive) _ Granted'
        WHEN req_status = 1 and req_mode = 15 THEN 'BU (bulk operations) _ Granted'
        WHEN req_status = 2 and req_mode = 1 THEN 'Sch-S (Schema stability) _ Converting'
        WHEN req_status = 2 and req_mode = 2 THEN 'Sch-M (Schema modification) _ Converting'
        WHEN req_status = 2 and req_mode = 3 THEN 'IS (Intent Shared) _ Converting'
        WHEN req_status = 2 and req_mode = 4 THEN 'SIU (Shared Intent Update) _ Converting'
        WHEN req_status = 2 and req_mode = 5 THEN 'IS-S (Intent Shared-Shared) _ Converting'
        WHEN req_status = 2 and req_mode = 6 THEN 'IX (Intent Exclusive) _ Converting'
        WHEN req_status = 2 and req_mode = 7 THEN 'SIX (Shared Intent Exclusive) _ Converting'
        WHEN req_status = 2 and req_mode = 8 THEN 'S (Shared) _ Converting'
        WHEN req_status = 2 and req_mode = 9 THEN 'U (Update) _ Converting'
        WHEN req_status = 2 and req_mode = 10 THEN 'IIn-Nul. (Intent Insert-NULL) _ Converting'
        WHEN req_status = 2 and req_mode = 11 THEN 'IS-X (Intent Shared-Exclusive) _ Converting'
        WHEN req_status = 2 and req_mode = 12 THEN 'IU (Intent Update) _ Converting'
        WHEN req_status = 2 and req_mode = 13 THEN 'IS-U (Intent Shared-Update) _ Converting'
        WHEN req_status = 2 and req_mode = 14 THEN 'X (Exclusive) _ Converting'
        WHEN req_status = 2 and req_mode = 15 THEN 'BU (bulk operations) _ Converting'
        WHEN req_status = 3 and req_mode = 1 THEN 'Sch-S (Schema stability) _ Waiting'
        WHEN req_status = 3 and req_mode = 2 THEN 'Sch-M (Schema modification) _ Waiting'
        WHEN req_status = 3 and req_mode = 3 THEN 'IS (Intent Shared) _ Waiting'
        WHEN req_status = 3 and req_mode = 4 THEN 'SIU (Shared Intent Update) _ Waiting'
        WHEN req_status = 3 and req_mode = 5 THEN 'IS-S (Intent Shared-Shared) _ Waiting'
        WHEN req_status = 3 and req_mode = 6 THEN 'IX (Intent Exclusive) _ Waiting'
        WHEN req_status = 3 and req_mode = 7 THEN 'SIX (Shared Intent Exclusive) _ Waiting'
        WHEN req_status = 3 and req_mode = 8 THEN 'S (Shared) _ Waiting'
        WHEN req_status = 3 and req_mode = 9 THEN 'U (Update) _ Waiting'
        WHEN req_status = 3 and req_mode = 10 THEN 'IIn-Nul. (Intent Insert-NULL) _ Waiting'
        WHEN req_status = 3 and req_mode = 11 THEN 'IS-X (Intent Shared-Exclusive) _ Waiting'
        WHEN req_status = 3 and req_mode = 12 THEN 'IU (Intent Update) _ Waiting'
        WHEN req_status = 3 and req_mode = 13 THEN 'IS-U (Intent Shared-Update) _ Waiting'
        WHEN req_status = 3 and req_mode = 14 THEN 'X (Exclusive) _ Waiting'
        WHEN req_status = 3 and req_mode = 15 THEN 'BU (bulk operations) _ Waiting'
        ELSE 'Unclassified'
END
order by DBName_ID, LockType desc
END
GO

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating