Skill 2.2.4
Identify and resolve query performance issues, including blocking and deadlocks
Key Concepts
Blocking occurs when a session waits for a lock held by another; long chains form when the head blocker runs a long transaction, waits for the application or is idle with an open transaction. sys.dm_exec_requests.blocking_session_id, sys.dm_os_waiting_tasks and sys.dm_tran_locks identify the chain, and LCK_M_* waits confirm it. Fixes include committing promptly, indexing so statements touch fewer rows, RCSI, smaller batches and, as a last resort, ending the head session.
A deadlock is a cycle of sessions each waiting for the other; the lock monitor detects it and rolls back the cheapest victim with error 1205. The deadlock graph, captured by the xml_deadlock_report extended event (or the Deadlocks diagnostic log in Azure SQL Database), shows the resources and statements. Fixes are accessing objects in the same order, keeping transactions short, adding covering indexes to avoid lookups, using row versioning and retrying the victim's transaction. SET DEADLOCK_PRIORITY chooses which session should lose.