Skip to content

LCK_M_S Wait Type on SQL Server

In SQL Server, wait types are essential for diagnosing performance issues, as they indicate what a session is waiting on during query execution. One such wait type, LCK_M_S, is related to locking and can significantly impact database performance. This blog post explores the LCK_M_S wait type, its causes, effects, and strategies for mitigation.

What is the LCK_M_S Wait Type?

The LCK_M_S wait type indicates that a session is waiting to acquire a Shared (S) lock on a resource. Shared locks are typically used for read operations, allowing multiple sessions to read the same data concurrently without modifying it. When a session encounters an LCK_M_S wait, it is blocked because another session holds an incompatible lock, such as an Exclusive (X) or Update (U) lock, on the same resource.

Common Causes

LCK_M_S waits occur in scenarios involving lock contention, including:

  • Long-Running Transactions: A transaction holding an Exclusive lock (e.g., during a large update) blocks other sessions needing a Shared lock for reading.
  • High Concurrency: Multiple sessions attempting to read and modify the same data simultaneously, leading to lock conflicts.
  • Index Maintenance: Operations like index rebuilds or reorganizations that take Exclusive locks on tables or indexes.
  • Poorly Optimized Queries: Queries scanning large datasets or lacking proper indexes, holding locks longer than necessary.
  • Blocking Chains: A chain of sessions where one transaction’s lock blocks others, causing a cascade of LCK_M_S waits.

Impact on Performance

High LCK_M_S wait times can lead to:

  • Query Delays: Read operations waiting for Shared locks are delayed, increasing response times.
  • Reduced Throughput: In high-concurrency environments, lock contention can limit transaction processing rates.
  • User Experience Issues: Applications may experience timeouts or slow performance, affecting end users.

Diagnosing LCK_M_S Waits

To identify and resolve LCK_M_S waits, use the following tools:

1. sys.dm_os_wait_stats

Check cumulative Wait Statistics for LCK_M_S:

SELECT wait_type, wait_time_ms, waiting_tasks_countFROM sys.dm_os_wait_statsWHERE wait_type = 'LCK_M_S';    

wait_time_ms shows total wait time, and waiting_tasks_count indicates the number of blocked tasks.

2. sys.dm_tran_locks

Identify active locks and blocking sessions:

SELECT     resource_type,     resource_database_id,     resource_description,     request_mode,     request_status,    request_session_idFROM sys.dm_tran_locksWHERE request_status = 'WAIT';    

This query shows sessions waiting for locks, including the resource and lock type causing the LCK_M_S wait.

3. sys.dm_exec_requests

Find currently blocked sessions:

SELECT     session_id,     blocking_session_id,     wait_type,     wait_time,     sql_handleFROM sys.dm_exec_requestsWHERE wait_type = 'LCK_M_S';    

Use sql_handle with sys.dm_exec_sql_text to retrieve the blocking query text.

4. Extended Events

Capture lock-related events for detailed analysis:

CREATE EVENT SESSION [TrackLockWaits] ON SERVERADD EVENT sqlserver.lock_acquired,ADD EVENT sqlserver.lock_releasedADD TARGET package0.ring_buffer;ALTER EVENT SESSION [TrackLockWaits] ON SERVER STATE = START;    

Troubleshooting and Mitigation

To reduce LCK_M_S waits, consider these strategies:

  • Optimize Transactions: Keep transactions short and commit promptly to minimize lock duration.
  • Improve Query Performance: Add indexes to reduce table scans and optimize queries to access fewer rows.
  • Use Appropriate Isolation Levels: Consider Read Committed Snapshot Isolation (RCSI) or Snapshot Isolation to reduce Shared lock contention, though this increases tempdb usage.
  • Schedule Maintenance: Perform index rebuilds or large updates during low-traffic periods.
  • Monitor Blocking: Use SQL Server Agent alerts or monitoring tools to detect and resolve blocking chains quickly.

Conclusion

The LCK_M_S wait type indicates blocking due to Shared lock contention, often caused by long-running transactions or high concurrency. By monitoring with DMVs like sys.dm_os_wait_stats and sys.dm_tran_locks, and optimizing transactions and queries, you can mitigate these waits and improve SQL Server Performance. Share your experiences with LCK_M_S waits in the comments!

 

More from Stedman Solutions:

SteveStedman5
Steve and the team at Stedman Solutions are here for all your SQL Server needs.
Contact us today for your free 30 minute consultation..
We are ready to help!

Leave a Reply

Your email address will not be published. Required fields are marked *

two + two =