Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Find Locked objects with high waitstats Expand / Collapse
Posted Friday, September 27, 2013 1:40 AM


Group: General Forum Members
Last Login: Yesterday @ 3:14 AM
Points: 189, Visits: 950

I have a SQL JOB that takes almost 8 hours to complete. I could find that when this runs there are a lot of waits. Screenshot is attached.
I would like to know on which objects these locks and latches are occurring and if possible the SQL statement involved.
These locks and latches are not being kept for a long duration, so it is hard to track from sysprocesses.

Thanks in Advance !


  Post Attachments 
Capture.JPG (4 views, 27.01 KB)
Post #1499232
Posted Friday, September 27, 2013 3:36 AM



Group: General Forum Members
Last Login: Yesterday @ 3:43 PM
Points: 17,173, Visits: 32,137
The options are, query the dynamic management objects (DMO) such as sys.dm_exec_requests while the query is running in order to see which locks are taking a long time during execution. You can combine that DMO with others to see the query, the locked objects and the execution plan. The other option would be to set up extended events to capture information. That one is going to be much tougher. What you would need to do is start with the lock_acquired event. You can combine that with lock_released and look for pairings that exceed a certain value. It's going to be a bit of work to set up, but you can narrow right down to specific events that way. You should be able to combine that data with rpc_completed and sql_batch_completed to also capture the queries involved.

"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of:
SQL Server Query Performance Tuning
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1499274
Posted Friday, September 27, 2013 5:07 AM

SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 8:17 AM
Points: 4,004, Visits: 4,720

It's easy with extended events (XE) in sql server 2012 by help of its GUI. It should be possible with XE 2008 R2, but you'll have to write some code (easier if you can find a gui for sql 2008 r2).
Another option is to use some code in a scheduled job and collect data for a certain period.

I use the following code to catch locks:

L1.resource_type ,
DB_NAME(L1.resource_database_id) AS DatabaseName ,
CASE L1.resource_type
WHEN 'OBJECT' THEN OBJECT_NAME(L1.resource_associated_entity_id,L1.resource_database_id)
WHEN L1.resource_database_id = DB_ID() THEN
OBJECT_NAME(object_id, L1.resource_database_id)
FROM sys.partitions
WHERE hobt_id = L1.resource_associated_entity_id
END AS ObjectName ,
L1.resource_description ,
L1.request_session_id ,
L1.request_mode ,
sys.dm_tran_locks AS L1
JOIN sys.dm_tran_locks AS L2 ON L1.resource_associated_entity_id = L2.resource_associated_entity_id
L1.request_status <> L2.request_status
AND ( L1.resource_description = L2.resource_description
OR ( L1.resource_description IS NULL AND L2.resource_description IS NULL)
L1.resource_database_id ,
L1.resource_associated_entity_id ,
L1.request_status ASC;


Igor Micev,
SQL Server developer at Seavus
Post #1499311
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse