Does SQL Server's serializable isolation level lock entire tableWhat is lock escalation?How do I implement insert-if-not-found for transactions at serializable isolation level?MySQL InnoDB locks primary key on delete even in READ COMMITTEDDoes SQL Server place shared locks on scanned records when using REPEATABLE READWhy are U locks required when using Read Committed Snapshot IsolationSELECT INTO OUTFILE vs INSERT INTO … SELECTElevate transaction isolation in Maria DBUnderstanding repeatable read isolation levelWhy does this RX-X lock not appear in Extended Events?Key-range RangeI-N lock compatibility in SQL Server
Using Fermat's Little Theorem to Show Divisibility
Is Irish buttermilk different
What's the difference between jpg and JPG on mac?
Why is dwarfism considered an advantage for jobs in space?
Arcane Adept: is this proposed Warlock feat balanced as compared to PHB feats?
What does exclamation mark mean before invoking a method in C# 8.0?
Do creatures with Defender have summoning sickness?
Is gaining 1 AC in exchange for disadvantage on Perception checks a balanced trade?
How much tech advancement could be made out of modern processor appearing in 1980s?
Codewars Solution - Functions acting on each other nested
Turing award papers
Is there a material or method to allow "swimmable" coins?
Does a patron have to know their warlock?
Feeling of forcing oneself to do something
Integrate over a region
Build a matrix from the coordinates of its elements and complete it with zeros
What's that in front of the overhead panel?
Intersection of sorted lists
Why do Russian names transliterated into English have unpronounceable 'k's before 'h's (e.g. 'Mikhail' instead of just 'Mihail')?
I wasted the 6 years of my life and I don't know what should I do and how will I survive?
Is American Express widely accepted in Hong Kong?
Can any number of squares be a square?
Did "2001: A Space Odyssey" make any reference to the names of companies, or show any evidence of the existence of advertisements?
What is the word for 'regarding the structure of a theory'?
Does SQL Server's serializable isolation level lock entire table
What is lock escalation?How do I implement insert-if-not-found for transactions at serializable isolation level?MySQL InnoDB locks primary key on delete even in READ COMMITTEDDoes SQL Server place shared locks on scanned records when using REPEATABLE READWhy are U locks required when using Read Committed Snapshot IsolationSELECT INTO OUTFILE vs INSERT INTO … SELECTElevate transaction isolation in Maria DBUnderstanding repeatable read isolation levelWhy does this RX-X lock not appear in Extended Events?Key-range RangeI-N lock compatibility in SQL Server
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
Me and a colleague of mine discussed the implications of use of the serializable isolation level. He said it locked the entire table, but I disagreed to that telling him it potentially could but it tries to apply range locks and it doesn't apply true serialization as explained here: The Serializable Isolation Level.
I can't find anything in the docs either for the "locks entire table": SET TRANSACTION ISOLATION LEVEL.
The doc states a bunch of things regarding range locks, so in theory you could lock the entire table by simply having a range lock that locks the entire range of possible values in the table, but it doesn't lock the table.
Am I completely wrong here? Does it in fact lock the entire table (or tables)?
sql-server isolation-level serialization
add a comment
|
Me and a colleague of mine discussed the implications of use of the serializable isolation level. He said it locked the entire table, but I disagreed to that telling him it potentially could but it tries to apply range locks and it doesn't apply true serialization as explained here: The Serializable Isolation Level.
I can't find anything in the docs either for the "locks entire table": SET TRANSACTION ISOLATION LEVEL.
The doc states a bunch of things regarding range locks, so in theory you could lock the entire table by simply having a range lock that locks the entire range of possible values in the table, but it doesn't lock the table.
Am I completely wrong here? Does it in fact lock the entire table (or tables)?
sql-server isolation-level serialization
add a comment
|
Me and a colleague of mine discussed the implications of use of the serializable isolation level. He said it locked the entire table, but I disagreed to that telling him it potentially could but it tries to apply range locks and it doesn't apply true serialization as explained here: The Serializable Isolation Level.
I can't find anything in the docs either for the "locks entire table": SET TRANSACTION ISOLATION LEVEL.
The doc states a bunch of things regarding range locks, so in theory you could lock the entire table by simply having a range lock that locks the entire range of possible values in the table, but it doesn't lock the table.
Am I completely wrong here? Does it in fact lock the entire table (or tables)?
sql-server isolation-level serialization
Me and a colleague of mine discussed the implications of use of the serializable isolation level. He said it locked the entire table, but I disagreed to that telling him it potentially could but it tries to apply range locks and it doesn't apply true serialization as explained here: The Serializable Isolation Level.
I can't find anything in the docs either for the "locks entire table": SET TRANSACTION ISOLATION LEVEL.
The doc states a bunch of things regarding range locks, so in theory you could lock the entire table by simply having a range lock that locks the entire range of possible values in the table, but it doesn't lock the table.
Am I completely wrong here? Does it in fact lock the entire table (or tables)?
sql-server isolation-level serialization
sql-server isolation-level serialization
edited Sep 28 at 14:30
Erik Darling
29.6k13 gold badges89 silver badges146 bronze badges
29.6k13 gold badges89 silver badges146 bronze badges
asked Sep 28 at 6:57
mslotmslot
3055 bronze badges
3055 bronze badges
add a comment
|
add a comment
|
2 Answers
2
active
oldest
votes
Escalation, though
Lock escalation under serializable isolation level may occur the same as it does with other isolation levels.
- Correct indexes can help to avoid lock escalation up to a point
- Locking many indexes will increase the likelihood of lock escalation; the count is cumulative across objects for a single statement
Some quick examples using a single table with a single index. Id is the primary key and clustered index on the table.
One Row
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
UPDATE c
SET c.Score = 2147483647
FROM dbo.Comments AS c
WHERE c.Id = 138; --One value
ROLLBACK
For a single Id value, locking is minimal.
+--------------+---------------+---------------+-------------+
| request_mode | locked_object | resource_type | total_locks |
+--------------+---------------+---------------+-------------+
| RangeX-X | Comments | KEY | 1 |
| IX | Comments | OBJECT | 1 |
| IX | Comments | PAGE | 1 |
+--------------+---------------+---------------+-------------+
Multiple Rows
But locks will go up if we start working in ranges:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
UPDATE c
SET c.Score = 2147483647
FROM dbo.Comments AS c
WHERE c.Id BETWEEN 1 AND 5000; -- Small range
ROLLBACK
Now we have more exclusive locks on more keys:
+--------------+---------------+---------------+-------------+
| request_mode | locked_object | resource_type | total_locks |
+--------------+---------------+---------------+-------------+
| RangeX-X | Comments | KEY | 2429 |
| IX | Comments | OBJECT | 1 |
| IX | Comments | PAGE | 97 |
+--------------+---------------+---------------+-------------+
Way More Rows
This will carry on until we hit a tipping point:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
UPDATE c
SET c.Score = 2147483647
FROM dbo.Comments AS c
WHERE c.Id BETWEEN 1 AND 11655; --Larger range
ROLLBACK
Lock escalation is attempted and is successful:
+--------------+---------------+---------------+-------------+
| request_mode | locked_object | resource_type | total_locks |
+--------------+---------------+---------------+-------------+
| X | Comments | OBJECT | 1 |
+--------------+---------------+---------------+-------------+
Pay Attention
It's important to separate two concepts here: the isolation level will be serializable no matter what kind of locks are taken. The query chooses the isolation level, and the storage engine chooses the locks. Serializable won't always result in range locks -- the storage engine can pick whichever kind of locks still honor the isolation level.
add a comment
|
If there is an index on a search predicate, it might be used for range locks.
I.e., lock from first row to next in range. And from that next to third row, etc. Up to the last row in the range. So essentially a number of row locks, but it blocks the range from inserts also for "in-between" values (locking the range).
For this to happen you (SQL Server) need to have an index to work with. Without indexes to do the locking in (indexes on predicates), you'll (from what I know) will get table locks.
add a comment
|
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "182"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f249838%2fdoes-sql-servers-serializable-isolation-level-lock-entire-table%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Escalation, though
Lock escalation under serializable isolation level may occur the same as it does with other isolation levels.
- Correct indexes can help to avoid lock escalation up to a point
- Locking many indexes will increase the likelihood of lock escalation; the count is cumulative across objects for a single statement
Some quick examples using a single table with a single index. Id is the primary key and clustered index on the table.
One Row
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
UPDATE c
SET c.Score = 2147483647
FROM dbo.Comments AS c
WHERE c.Id = 138; --One value
ROLLBACK
For a single Id value, locking is minimal.
+--------------+---------------+---------------+-------------+
| request_mode | locked_object | resource_type | total_locks |
+--------------+---------------+---------------+-------------+
| RangeX-X | Comments | KEY | 1 |
| IX | Comments | OBJECT | 1 |
| IX | Comments | PAGE | 1 |
+--------------+---------------+---------------+-------------+
Multiple Rows
But locks will go up if we start working in ranges:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
UPDATE c
SET c.Score = 2147483647
FROM dbo.Comments AS c
WHERE c.Id BETWEEN 1 AND 5000; -- Small range
ROLLBACK
Now we have more exclusive locks on more keys:
+--------------+---------------+---------------+-------------+
| request_mode | locked_object | resource_type | total_locks |
+--------------+---------------+---------------+-------------+
| RangeX-X | Comments | KEY | 2429 |
| IX | Comments | OBJECT | 1 |
| IX | Comments | PAGE | 97 |
+--------------+---------------+---------------+-------------+
Way More Rows
This will carry on until we hit a tipping point:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
UPDATE c
SET c.Score = 2147483647
FROM dbo.Comments AS c
WHERE c.Id BETWEEN 1 AND 11655; --Larger range
ROLLBACK
Lock escalation is attempted and is successful:
+--------------+---------------+---------------+-------------+
| request_mode | locked_object | resource_type | total_locks |
+--------------+---------------+---------------+-------------+
| X | Comments | OBJECT | 1 |
+--------------+---------------+---------------+-------------+
Pay Attention
It's important to separate two concepts here: the isolation level will be serializable no matter what kind of locks are taken. The query chooses the isolation level, and the storage engine chooses the locks. Serializable won't always result in range locks -- the storage engine can pick whichever kind of locks still honor the isolation level.
add a comment
|
Escalation, though
Lock escalation under serializable isolation level may occur the same as it does with other isolation levels.
- Correct indexes can help to avoid lock escalation up to a point
- Locking many indexes will increase the likelihood of lock escalation; the count is cumulative across objects for a single statement
Some quick examples using a single table with a single index. Id is the primary key and clustered index on the table.
One Row
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
UPDATE c
SET c.Score = 2147483647
FROM dbo.Comments AS c
WHERE c.Id = 138; --One value
ROLLBACK
For a single Id value, locking is minimal.
+--------------+---------------+---------------+-------------+
| request_mode | locked_object | resource_type | total_locks |
+--------------+---------------+---------------+-------------+
| RangeX-X | Comments | KEY | 1 |
| IX | Comments | OBJECT | 1 |
| IX | Comments | PAGE | 1 |
+--------------+---------------+---------------+-------------+
Multiple Rows
But locks will go up if we start working in ranges:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
UPDATE c
SET c.Score = 2147483647
FROM dbo.Comments AS c
WHERE c.Id BETWEEN 1 AND 5000; -- Small range
ROLLBACK
Now we have more exclusive locks on more keys:
+--------------+---------------+---------------+-------------+
| request_mode | locked_object | resource_type | total_locks |
+--------------+---------------+---------------+-------------+
| RangeX-X | Comments | KEY | 2429 |
| IX | Comments | OBJECT | 1 |
| IX | Comments | PAGE | 97 |
+--------------+---------------+---------------+-------------+
Way More Rows
This will carry on until we hit a tipping point:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
UPDATE c
SET c.Score = 2147483647
FROM dbo.Comments AS c
WHERE c.Id BETWEEN 1 AND 11655; --Larger range
ROLLBACK
Lock escalation is attempted and is successful:
+--------------+---------------+---------------+-------------+
| request_mode | locked_object | resource_type | total_locks |
+--------------+---------------+---------------+-------------+
| X | Comments | OBJECT | 1 |
+--------------+---------------+---------------+-------------+
Pay Attention
It's important to separate two concepts here: the isolation level will be serializable no matter what kind of locks are taken. The query chooses the isolation level, and the storage engine chooses the locks. Serializable won't always result in range locks -- the storage engine can pick whichever kind of locks still honor the isolation level.
add a comment
|
Escalation, though
Lock escalation under serializable isolation level may occur the same as it does with other isolation levels.
- Correct indexes can help to avoid lock escalation up to a point
- Locking many indexes will increase the likelihood of lock escalation; the count is cumulative across objects for a single statement
Some quick examples using a single table with a single index. Id is the primary key and clustered index on the table.
One Row
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
UPDATE c
SET c.Score = 2147483647
FROM dbo.Comments AS c
WHERE c.Id = 138; --One value
ROLLBACK
For a single Id value, locking is minimal.
+--------------+---------------+---------------+-------------+
| request_mode | locked_object | resource_type | total_locks |
+--------------+---------------+---------------+-------------+
| RangeX-X | Comments | KEY | 1 |
| IX | Comments | OBJECT | 1 |
| IX | Comments | PAGE | 1 |
+--------------+---------------+---------------+-------------+
Multiple Rows
But locks will go up if we start working in ranges:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
UPDATE c
SET c.Score = 2147483647
FROM dbo.Comments AS c
WHERE c.Id BETWEEN 1 AND 5000; -- Small range
ROLLBACK
Now we have more exclusive locks on more keys:
+--------------+---------------+---------------+-------------+
| request_mode | locked_object | resource_type | total_locks |
+--------------+---------------+---------------+-------------+
| RangeX-X | Comments | KEY | 2429 |
| IX | Comments | OBJECT | 1 |
| IX | Comments | PAGE | 97 |
+--------------+---------------+---------------+-------------+
Way More Rows
This will carry on until we hit a tipping point:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
UPDATE c
SET c.Score = 2147483647
FROM dbo.Comments AS c
WHERE c.Id BETWEEN 1 AND 11655; --Larger range
ROLLBACK
Lock escalation is attempted and is successful:
+--------------+---------------+---------------+-------------+
| request_mode | locked_object | resource_type | total_locks |
+--------------+---------------+---------------+-------------+
| X | Comments | OBJECT | 1 |
+--------------+---------------+---------------+-------------+
Pay Attention
It's important to separate two concepts here: the isolation level will be serializable no matter what kind of locks are taken. The query chooses the isolation level, and the storage engine chooses the locks. Serializable won't always result in range locks -- the storage engine can pick whichever kind of locks still honor the isolation level.
Escalation, though
Lock escalation under serializable isolation level may occur the same as it does with other isolation levels.
- Correct indexes can help to avoid lock escalation up to a point
- Locking many indexes will increase the likelihood of lock escalation; the count is cumulative across objects for a single statement
Some quick examples using a single table with a single index. Id is the primary key and clustered index on the table.
One Row
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
UPDATE c
SET c.Score = 2147483647
FROM dbo.Comments AS c
WHERE c.Id = 138; --One value
ROLLBACK
For a single Id value, locking is minimal.
+--------------+---------------+---------------+-------------+
| request_mode | locked_object | resource_type | total_locks |
+--------------+---------------+---------------+-------------+
| RangeX-X | Comments | KEY | 1 |
| IX | Comments | OBJECT | 1 |
| IX | Comments | PAGE | 1 |
+--------------+---------------+---------------+-------------+
Multiple Rows
But locks will go up if we start working in ranges:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
UPDATE c
SET c.Score = 2147483647
FROM dbo.Comments AS c
WHERE c.Id BETWEEN 1 AND 5000; -- Small range
ROLLBACK
Now we have more exclusive locks on more keys:
+--------------+---------------+---------------+-------------+
| request_mode | locked_object | resource_type | total_locks |
+--------------+---------------+---------------+-------------+
| RangeX-X | Comments | KEY | 2429 |
| IX | Comments | OBJECT | 1 |
| IX | Comments | PAGE | 97 |
+--------------+---------------+---------------+-------------+
Way More Rows
This will carry on until we hit a tipping point:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRAN
UPDATE c
SET c.Score = 2147483647
FROM dbo.Comments AS c
WHERE c.Id BETWEEN 1 AND 11655; --Larger range
ROLLBACK
Lock escalation is attempted and is successful:
+--------------+---------------+---------------+-------------+
| request_mode | locked_object | resource_type | total_locks |
+--------------+---------------+---------------+-------------+
| X | Comments | OBJECT | 1 |
+--------------+---------------+---------------+-------------+
Pay Attention
It's important to separate two concepts here: the isolation level will be serializable no matter what kind of locks are taken. The query chooses the isolation level, and the storage engine chooses the locks. Serializable won't always result in range locks -- the storage engine can pick whichever kind of locks still honor the isolation level.
answered Sep 28 at 13:01
Erik DarlingErik Darling
29.6k13 gold badges89 silver badges146 bronze badges
29.6k13 gold badges89 silver badges146 bronze badges
add a comment
|
add a comment
|
If there is an index on a search predicate, it might be used for range locks.
I.e., lock from first row to next in range. And from that next to third row, etc. Up to the last row in the range. So essentially a number of row locks, but it blocks the range from inserts also for "in-between" values (locking the range).
For this to happen you (SQL Server) need to have an index to work with. Without indexes to do the locking in (indexes on predicates), you'll (from what I know) will get table locks.
add a comment
|
If there is an index on a search predicate, it might be used for range locks.
I.e., lock from first row to next in range. And from that next to third row, etc. Up to the last row in the range. So essentially a number of row locks, but it blocks the range from inserts also for "in-between" values (locking the range).
For this to happen you (SQL Server) need to have an index to work with. Without indexes to do the locking in (indexes on predicates), you'll (from what I know) will get table locks.
add a comment
|
If there is an index on a search predicate, it might be used for range locks.
I.e., lock from first row to next in range. And from that next to third row, etc. Up to the last row in the range. So essentially a number of row locks, but it blocks the range from inserts also for "in-between" values (locking the range).
For this to happen you (SQL Server) need to have an index to work with. Without indexes to do the locking in (indexes on predicates), you'll (from what I know) will get table locks.
If there is an index on a search predicate, it might be used for range locks.
I.e., lock from first row to next in range. And from that next to third row, etc. Up to the last row in the range. So essentially a number of row locks, but it blocks the range from inserts also for "in-between" values (locking the range).
For this to happen you (SQL Server) need to have an index to work with. Without indexes to do the locking in (indexes on predicates), you'll (from what I know) will get table locks.
edited Sep 29 at 18:28
Razvan Socol
1927 bronze badges
1927 bronze badges
answered Sep 28 at 11:08
Tibor KarasziTibor Karaszi
2,9201 silver badge9 bronze badges
2,9201 silver badge9 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f249838%2fdoes-sql-servers-serializable-isolation-level-lock-entire-table%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown