SlideShare a Scribd company logo
Key-Range Locks
Все тайное становится явным
Vitaliy Popovych
Kiev, Ukraine
Database Developer at Intapp
Sponsors
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
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
Lock Modes
Shared (S) Used for read operations such as a
SELECT
X
-Locked -Wait
Lock Modes
Exclusive (X) Used for data-modification operations,
such as INSERT, UPDATE, or DELETE
X
-Locked -Wait
Lock Modes
Update (U) Used on resources that can be updated
-Locked -Wait
Lock Modes
Intent (intent shared (IS), intent exclusive (IX)) Used
to establish a lock hierarchy
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
Isolation Levels
Pessimistic
 Read uncommitted
 Read committed
 Repeatable read
 Serializable
Optimistic
 Read committed
snapshot
 Snapshot
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
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
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
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
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
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
…
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
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
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
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
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
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'
Range S-S Locks
DEMO
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
Range S-U Locks
DEMO
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
Range X-X Locks
DEMO
Key Range Lock on READ COMMITTED
DEMO
Questions?
Vitaliy Popovych
 Email: vitaliy.popovych@outlook.com
 Facebook: www.facebook.com/vitaliy.popovich.911
 Twitter: @PopovychVitaliy
Thanks!!!

More Related Content

Recently uploaded

Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 

Recently uploaded (20)

Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

SQL Saturday 406 - Key Range Locking

  • 1. Key-Range Locks Все тайное становится явным Vitaliy Popovych Kiev, Ukraine Database Developer at Intapp
  • 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
  • 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 X -Locked -Wait
  • 7. Lock Modes Update (U) Used on resources that can be updated -Locked -Wait
  • 8. Lock Modes Intent (intent shared (IS), intent exclusive (IX)) Used to establish a lock hierarchy
  • 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
  • 10. Isolation Levels Pessimistic  Read uncommitted  Read committed  Repeatable read  Serializable Optimistic  Read committed snapshot  Snapshot
  • 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
  • 28. Key Range Lock on READ COMMITTED DEMO
  • 29. Questions? Vitaliy Popovych  Email: vitaliy.popovych@outlook.com  Facebook: www.facebook.com/vitaliy.popovich.911  Twitter: @PopovychVitaliy Thanks!!!

Editor's Notes

  1. Обеспечивают корректную работу нескольких процессов(пользователей) с одним и тем же источником данных
  2. Странички по 8Кб, загрузаем с диска в пямять и работаем
  3. Блокировка с намерением
  4. Пессимистическая/Оптимистическая модель обработки конкурентных запросов
  5. Блокировки на чтение не устанавливаются и мы можем читать данные незавершенных транзакций
  6. Блокировки на чтение устанавливаются, но только до завершения инструкции чтения
  7. Блокировки на чтение устанавливаются до конца транзакции
  8. Блокировки на чтение устанавливаются на диапазон ключа, (то что мы ищем)
  9. Блокировки диапазона ключа используються в индексированных представлениях, ssis пакетах (настройка по умолчанию)
  10. Итог: не забываем про + 1 строку, про поиск не существующих значений. Это все может превести к взаимоблокеровке.