SlideShare a Scribd company logo
1 of 29
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

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 

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 HubspotMarius 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 ChatGPTExpeed 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 EngineeringsPixeldarts
 
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 HealthThinkNow
 
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.pdfmarketingartwork
 
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 2024Neil 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 2024Albert 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 InsightsKurio // 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 2024Search 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 summarySpeakerHub
 
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 IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit 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 managementMindGenius
 
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 строку, про поиск не существующих значений. Это все может превести к взаимоблокеровке.