Professional Documents
Culture Documents
SQL Locks
SQL Locks
SQL Server
Locking and Concurrency
[email protected]
www.sqlserverutilities.com
blogs.microsoft.co.il/blogs/yaniv_etrogi
1
Concurrency Models
Pessimistic Concurrency
Optimistic Concurrency
2
Pessimistic Concurrency
Locks are acquired on data being read
Preventing other processes from modifying
that data
Other processes can still read the data
3
Optimistic Concurrency
Readers see the state of the data before the
modification occurred using Row versioning
5
Isolation Levels 1
Read Uncommitted
8
Isolation Levels 4
Serializable
9
Isolation Levels 5
Snapshot
10
Lock Modes
http://msdn.microsoft.com/en-us/library/ms175519.aspx
11
Shared Locks
Acquired when data is read
12
Exclusive Locks
Acquired on data when the data is modified by
any DML statement (INSERT, UPDATE,
DELETE, SELECT INTO, MERGE)
14
Intent Locks
15
Schema Locks
schema stability
schema modification
bulk update
16
Conversion Locks
Conversion locks are the result of a
conversion from one lock mode to
another
SIX
SIU
UIX
See example
17
Key Locks
In the Read Committed, Repeatable
Read and Snapshot isolation levels Key
Locks are used to lock the index keys
When a clustered index exists the data rows are the leaf
level of the index and key locks are acquired
When the table is a heap key locks can be acquired on
the non-clustered indexes and row locks (RID) for the
actual data
See example 18
Key Locks Types
There are nine types of
key-range locks and each has
a two-part name
The first part indicates the type of lock on
the range of data between index keys
The second part indicates the type of lock
on the key itself
19
Key Locks Details
RangeS-S
Shared lock on the range between keys; shared lock on the key at
the end of the range
RangeS-U
Shared lock on the range between keys; update lock on the key at
the end of the range
RangeIn-Null
Exclusive lock to prevent inserts on the range between keys; no
lock on the keys themselves
RangeX-X
Exclusive lock on the range between keys; exclusive lock on the
key at the end of the range
20
Key Locks Details 2
RangeIn-S
Conversion lock created by S and RangeIn_Null lock
RangeIn-U
Conversion lock created by U and RangeIn_Null lock
RangeIn-X
Conversion of X and RangeIn_Null lock
RangeX-S
Conversion of RangeIn_Null and RangeS_S lock
RangeX-U
Conversion of RangeIn_Null and RangeS_U lock
21
Lock Duration
The duration a lock is held depends on the mode of the
lock and the transaction isolation level in effect
22
Lock Compatibility
Two locks are compatible if one lock can
be granted while another lock on the
same resource is already held by a
different process
Two connections can hold a shared lock on the same
row or page
24
Lock Starvation
By examining the compatibility of locks not only with
processes granted but also processes waiting SQL
Server prevents lock starvation which can result when
requests for shared locks keep overlapping so that the
request for the exclusive lock can never be granted
25
Lock Escalation
26
Lock Escalation 2
The number of locks held by a single statement on one
object (index or heap) exceeds a threshold (~5000)
27
Lock Escalation 3
The CREATE/ ALTER TABLE command can
also have an impact on Locks and Lock
escalation
ALLOW_ROW_LOCKS = { ON | OFF }
Row locks are allowed when accessing the index
ALLOW_PAGE_LOCKS = { ON | OFF }
Page locks are allowed when accessing the index
28
Lock Escalation 4
The CREATE/ ALTER INDEX command can also
impact the way data is accessed and locked
31
Lock Hints
HOLDLOCK – serializes access same as
SERIALIZABLE
32
Lock Hints 2
TABLOCKX - exclusively locks the table
Equal to (TABLOCK , XLOCK )
33
Lock Hints 3
XLOCK - Acquires an exclusive lock on all data processed by the
statement
If combined with ROWLOCK, PAGLOCK, or TABLOCK, the exclusive locks apply to the
appropriate level of granularity
Watch example
35
Resolving Blocking
Writer/Writer
sys.dm_os_wait_stats
http://msdn.microsoft.com/en-us/library/ms179984.aspx
sys.dm_tran_locks
http://msdn.microsoft.com/en-us/library/ms190345.aspx
39