Find Locked objects with high waitstats

  • Hi,

    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 !

    Sanz
  • 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

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Hi,

    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:

    SELECT

    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 'DATABASE' THEN 'DATABASE'

    ELSE CASE

    WHEN L1.resource_database_id = DB_ID() THEN

    (SELECT

    OBJECT_NAME(object_id, L1.resource_database_id)

    FROM sys.partitions

    WHERE hobt_id = L1.resource_associated_entity_id

    )

    ELSE NULL

    END

    END AS ObjectName ,

    L1.resource_description ,

    L1.request_session_id ,

    L1.request_mode ,

    L1.request_status

    FROM

    sys.dm_tran_locks AS L1

    JOIN sys.dm_tran_locks AS L2 ON L1.resource_associated_entity_id = L2.resource_associated_entity_id

    WHERE

    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)

    )

    ORDER BY

    L1.resource_database_id ,

    L1.resource_associated_entity_id ,

    L1.request_status ASC;

    Regards,

    IgorMi

    Igor Micev,My blog: www.igormicev.com

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply