3. Agenda
Lock Modes
Lock Granularity
Isolation Levels
Key-Range Locks
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
9. 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
Dirty read
Non repeatable read
Phantom
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
12. Read committed
Non-repeatable read
Phantom
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
13. Read committed
Non-repeatable read
Phantom
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
14. Repeatable read
Phantom
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
15. Repeatable read
Phantom
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
16. 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
…
17. 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
18. Key-Range Locking
SERIALIZABLE
Before key-range locking can occur, the following
conditions must be satisfied:
- The transaction isolation level must be set to
- The query must use an INDEX
19. Key-Range Locking
In equality operations (“=”, “IN”) – Key-
Range locks are not held on key values
that are found inside an Unique Index even
if the transaction is issued in Serializable
isolation level.
Example:
SELECT name FROM Users
WHERE id = 5
id name
1 Alexander
2 Barbara
3 Emily
4 Jennifer
5 Richard
6 Thomas
7 William
Id - Unique Index
20. 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
21. 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
22. Range S-S Locks
Mode – RangeS-S
Range – RangeS
Row – S
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'
24. Range S-U Locks
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'
Mode – RangeS-U
Range – RangeS
Row – U
26. Range X-X Locks
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'
Mode – RangeX-X
Range – RangeX
Row – X