This document discusses database locking concepts including lock modes, lock granularity, isolation levels, and key-range locking. It defines different lock modes like shared, exclusive, update, and intent locks. It describes lock granularity levels like row, page, table. It explains isolation levels and their treatment of phenomena like dirty reads, non-repeatable reads and phantoms. Finally, it details the four types of key-range locks used in SQL Server and demonstrates examples of each.
3. Agenda
Lock Modes
Lock Granularity
Isolation Levels
Key-Range Locking
Range S-S Locks
Range S-U Locks
Range X-X Locks
Key Range Locking on READ COMMITTED
4. Lock Modes
Shared (S) Used for read operations such as a SELECT
Exclusive (X) Used for data-modification operations, such as INSERT,
UPDATE, or DELETE
Update (U) Used on resources that can be updated.
Intent (intent shared (IS), intent exclusive (IX)) Used to establish a lock
hierarchy
Key-range (Range S-S, Range S-U, Range X-X, Range I-N ) Protects the
range of rows read by a query
Schema (Sch-M) and (Sch-S), Bulk Update (BU)
5. Lock Modes
Shared (S) Used for read operations such as a SELECT
X
-Locked
-Wait
6. Lock Modes
Exclusive (X) Used for data-modification operations, such as INSERT,
UPDATE, or DELETE
-Locked
-Wait
8. Lock Modes
Intent (intent shared (IS), intent exclusive (IX)) Used to establish a lock
hierarchy
9. Heaps, clustered tables
941 Bob
124 Andrew
715 John
554 Mary
285 Paul
525 Victor
1 Eric
7 Linda
43 Peter
17 Andrew
99 Victor
657 Bob
2 Tom
30 Lisa
80 Paul
82 Eric
548 Brian
41 Mary
27 William
326 Michael
1 Eric
2 Tom
3 Michael
4 Aiden
5 Noah
6 Jayden
7 Linda
8 Olivia
40 Madison
41 Mary
42 Jacob
43 Peter
44 William
45 Michael
46 Emily
47 Olivia
457 Emily
458 William
459 Alexander
500 Sophia
HeapIndex
… …
…
10. Lock Granularity
RID A row identifier used to lock a single row within a heap.
KEY A row lock within an index used to protect key.
PAGE An 8-kilobyte (KB) page in a database.
TABLE The entire table.
EXTENT, HoBT, FILE, APPLICATION, METADATA, ALLOCATION_UNIT,
DATABASE
11. Read uncommitted
Read committed
Repeatable read
Serializable
Read committed snapshot
Snapshot
Isolation Levels
Isolation level Dirty read Nonrepeatable
read Phantom
Read
uncommitted Yes Yes Yes
Read
committed No Yes Yes
Repeatable
read No No Yes
Serializable No No No
Snapshot No No No
12. Dirty read
Non repeatable read
Phantom
Read uncommitted
Id Country
1 Ukraine
2 Italy
3 France
4 France
5 Poland
6 Germany
7 Germany
SELECT * FROM Users
WHERE Country = 'Poland'
BEGIN TRAN
UPDATE Users
SET Country = ‘Ukraine'
WHERE Country = ‘Poland'
ROLLBACK
0 Records
SELECT * FROM Users
WHERE Country = 'Poland'
SELECT * FROM Users
WHERE Country = 'Poland'
1 Record
1 Record
Id Country
1 Ukraine
2 Italy
3 France
4 France
5 Ukraine
6 Germany
7 Germany
X
Dirty read
13. Non-repeatable read
Phantom
Read committed
BEGIN TRAN
UPDATE Users
SET Country = 'Ukraine'
WHERE Country = 'Poland'
SELECT * FROM Users
WHERE Country = 'Poland'
SELECT * FROM Users
WHERE Country = 'Poland'
Id Country
1 Ukraine
2 Italy
3 France
4 France
5 Poland
6 Germany
7 Germany
1 Record
X
Id Country
1 Ukraine
2 Italy
3 France
4 France
5 Ukraine
6 Germany
7 Germany
Wait
No dirty read
14. Non-repeatable read
Phantom
Read committed
UPDATE Users
SET Country = ‘Ukraine'
WHERE id = 4
BEGIN TRAN
SELECT * FROM Users
WHERE Country = 'France'
SELECT * FROM Users
WHERE Country = ‘France'
Id Country
1 Ukraine
2 Italy
3 France
4 France
5 Poland
6 Germany
7 Germany
2 Records
X
Id Country
1 Ukraine
2 Italy
3 France
4 Ukraine
5 Poland
6 Germany
7 Germany
…
Non-repeatable read
1 Record
15. Phantom
Repeatable read
BEGIN TRAN
UPDATE Users
SET Country = 'Ukraine'
WHERE id = 4
BEGIN TRAN
SELECT * FROM Users
WHERE Country = 'France'
SELECT * FROM Users
WHERE Country = 'France'
Id Country
1 Ukraine
2 Italy
3 France
4 France
5 Ukraine
6 Germany
7 Germany
No Non-repeatable read
2 Records
2 Records
…Wait X
16. Phantom
Repeatable read
BEGIN TRAN
UPDATE Users
SET Country = 'France'
WHERE Country = 'Poland'
BEGIN TRAN
SELECT * FROM Users
WHERE Country = 'France'
SELECT * FROM Users
WHERE Country = 'France'
Id Country
1 Ukraine
2 Italy
3 France
4 France
5 Poland
6 Germany
7 Germany
Phantom
2 Records
3 Records
…
X
Id Country
1 Ukraine
2 Italy
3 France
4 France
5 France
6 Germany
7 Germany
COMMIT
17. Serializable
INSERT INTO Users
VALUE (8,'France')
Id Country
1 Ukraine
2 Italy
3 France
4 France
5 France
6 Poland
7 Germany
BEGIN TRAN
SELECT * FROM Users
WHERE Country = 'France'
RANGES-S
No Phantom
3 Records
SELECT * FROM Users
WHERE Country = 'France'
3 Records
…
18. Key-Range Locking
There are 4 types of locks:
RangeS-S – Serializable range scan.
RangeS-U – Serializable update scan.
RangeX-X – Used when updating a key in a range.
RangeI-N – Used to test ranges before inserting a new key
into an index
19. Key-Range Locking
SERIALIZABLE- The transaction isolation level must be set to
- The query must use an INDEX
Before key-range locking can occur, the following conditions must be satisfied:
20. Key-Range Locking
- In equality operations (“=”, “IN”) – Key-Range
locks are not held on key values that are found inside
an Unique Indexeven if the transaction is issued in
Serializable isolation level. id name
1 Alexander
2 Barbara
3 Emily
4 Jennifer
5 Richard
6 Thomas
7 William
Example:
SELECT name FROM Users WHERE id = 5
Id - Unique Index
21. Key-Range Locking
- In equality operations (“=”, “IN”) – Key-Range locks are
held on “next” key values that are not found inside an
Unique or Non-Unique Index.
id name
1 Alexander
2 Barbara
3 Emily
4 Jennifer
5 Richard
6 Thomas
7 William
Example:
SELECT name FROM Users WHERE name = 'Olivia'
RangeS-S
id Name
1 Alexander
2 Barbara
3 Emily
4 Jennifer
… …
… Olivia
… …
5 Richard
6 Thomas
7 William
22. Key-Range Locking
- In in-equality operations (“>”, “<", BETWEEN, LIKE,
"<>“) – Key-Range locks are held on all key values
(found) from the range specified and the “next” value.
This is true for both Unique or Non-Unique Indexes.
Example:
SELECT name FROM Users
WHERE name BETWEEN 'Jennifer' AND 'Thomas'
id name
1 Alexander
2 Barbara
3 Emily
4 Jennifer
5 Richard
6 Thomas
7 William
RangeS-S
23. Range S-S Locks
Existing granted mode
Requested
mode S U X RangeS-U RangeI-N RangeX-X
RangeS-S Yes Yes No Yes No No
id name
1 Alexander
2 Barbara
3 Emily
4 Jennifer
5 Richard
6 Thomas
7 William
RangeS-S
RangeS-S
SELECT name
FROM [Users]
WHERE name = 'Jennifer'
1) Range – RangeS
2) Row – S
Mode – RangeS-S
25. Range S-U Locks
Existing granted mode
Requested
mode
S U X RangeS-S RangeI-N RangeX-X
RangeS-U Yes No No Yes No No
id name last_name
1 Alexander Wilson
2 Barbara Walker
3 Emily Robinson
4 Jennifer Clark
5 Richard Scott
6 Thomas Baker
7 William Morris
RangeS-U
RangeS-U
RangeS-U
UPDATE [Users]
SET last_name ='Smith'
WHERE name between 'Jennifer' and 'Richard'
1) Range – RangeS
2) Row – U
Mode – RangeS-U
27. Range X-X Locks
Existing granted mode
Requested
mode
S U X RangeS-U RangeI-N RangeS-S
RangeX-X No No No No No No
id name
1 Alexander
2 Barbara
3 Emily
4 Jennifer
5 Richard
6 Thomas
7 William
RangeS-U
RangeX-X
UPDATE [Users]
SET name ='Cooper'
WHERE name = 'Alexander'
1) Range – RangeX
2) Row – X
Mode – RangeX-X