SlideShare a Scribd company logo
1 of 30
Vitaliy Popovych
 Vitaliy Popovych
 Kiev, Ukraine
 SQL Developer at The Frayman Group
About me
Agenda
 Lock Modes
 Lock Granularity
 Isolation Levels
 Key-Range Locking
 Range S-S Locks
 Range S-U Locks
 Range X-X Locks
 Key Range Locking on READ COMMITTED
Lock Modes
 Shared (S) Used for read operations such as a SELECT
 Exclusive (X) Used for data-modification operations, such as INSERT,
UPDATE, or DELETE
 Update (U) Used on resources that can be updated.
 Intent (intent shared (IS), intent exclusive (IX)) Used to establish a lock
hierarchy
 Key-range (Range S-S, Range S-U, Range X-X, Range I-N ) Protects the
range of rows read by a query
 Schema (Sch-M) and (Sch-S), Bulk Update (BU)
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
-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
Heaps, clustered tables
941 Bob
124 Andrew
715 John
554 Mary
285 Paul
525 Victor
1 Eric
7 Linda
43 Peter
17 Andrew
99 Victor
657 Bob
2 Tom
30 Lisa
80 Paul
82 Eric
548 Brian
41 Mary
27 William
326 Michael
1 Eric
2 Tom
3 Michael
4 Aiden
5 Noah
6 Jayden
7 Linda
8 Olivia
40 Madison
41 Mary
42 Jacob
43 Peter
44 William
45 Michael
46 Emily
47 Olivia
457 Emily
458 William
459 Alexander
500 Sophia
HeapIndex
… …
…
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
 Read uncommitted
 Read committed
 Repeatable read
 Serializable
 Read committed snapshot
 Snapshot
Isolation Levels
Isolation level Dirty read Nonrepeatable
read Phantom
Read
uncommitted Yes Yes Yes
Read
committed No Yes Yes
Repeatable
read No No Yes
Serializable No No No
Snapshot No No No
 Dirty read
 Non repeatable read
 Phantom
Read uncommitted
Id Country
1 Ukraine
2 Italy
3 France
4 France
5 Poland
6 Germany
7 Germany
SELECT * FROM Users
WHERE Country = 'Poland'
BEGIN TRAN
UPDATE Users
SET Country = ‘Ukraine'
WHERE Country = ‘Poland'
ROLLBACK
0 Records
SELECT * FROM Users
WHERE Country = 'Poland'
SELECT * FROM Users
WHERE Country = 'Poland'
1 Record
1 Record
Id Country
1 Ukraine
2 Italy
3 France
4 France
5 Ukraine
6 Germany
7 Germany
X
Dirty read
 Non-repeatable read
 Phantom
Read committed
BEGIN TRAN
UPDATE Users
SET Country = 'Ukraine'
WHERE Country = 'Poland'
SELECT * FROM Users
WHERE Country = 'Poland'
SELECT * FROM Users
WHERE Country = 'Poland'
Id Country
1 Ukraine
2 Italy
3 France
4 France
5 Poland
6 Germany
7 Germany
1 Record
X
Id Country
1 Ukraine
2 Italy
3 France
4 France
5 Ukraine
6 Germany
7 Germany
Wait
No dirty read
 Non-repeatable read
 Phantom
Read committed
UPDATE Users
SET Country = ‘Ukraine'
WHERE id = 4
BEGIN TRAN
SELECT * FROM Users
WHERE Country = 'France'
SELECT * FROM Users
WHERE Country = ‘France'
Id Country
1 Ukraine
2 Italy
3 France
4 France
5 Poland
6 Germany
7 Germany
2 Records
X
Id Country
1 Ukraine
2 Italy
3 France
4 Ukraine
5 Poland
6 Germany
7 Germany
…
Non-repeatable read
1 Record
 Phantom
Repeatable read
BEGIN TRAN
UPDATE Users
SET Country = 'Ukraine'
WHERE id = 4
BEGIN TRAN
SELECT * FROM Users
WHERE Country = 'France'
SELECT * FROM Users
WHERE Country = 'France'
Id Country
1 Ukraine
2 Italy
3 France
4 France
5 Ukraine
6 Germany
7 Germany
No Non-repeatable read
2 Records
2 Records
…Wait X
 Phantom
Repeatable read
BEGIN TRAN
UPDATE Users
SET Country = 'France'
WHERE Country = 'Poland'
BEGIN TRAN
SELECT * FROM Users
WHERE Country = 'France'
SELECT * FROM Users
WHERE Country = 'France'
Id Country
1 Ukraine
2 Italy
3 France
4 France
5 Poland
6 Germany
7 Germany
Phantom
2 Records
3 Records
…
X
Id Country
1 Ukraine
2 Italy
3 France
4 France
5 France
6 Germany
7 Germany
COMMIT
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- The transaction isolation level must be set to
- The query must use an INDEX
Before key-range locking can occur, the following conditions must be satisfied:
Key-Range Locking
- In equality operations (“=”, “IN”) – Key-Range
locks are not held on key values that are found inside
an Unique Indexeven if the transaction is issued in
Serializable isolation level. id name
1 Alexander
2 Barbara
3 Emily
4 Jennifer
5 Richard
6 Thomas
7 William
Example:
SELECT name FROM Users WHERE id = 5
Id - Unique Index
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
Existing granted mode
Requested
mode S U X RangeS-U RangeI-N RangeX-X
RangeS-S Yes Yes No Yes No No
id name
1 Alexander
2 Barbara
3 Emily
4 Jennifer
5 Richard
6 Thomas
7 William
RangeS-S
RangeS-S
SELECT name
FROM [Users]
WHERE name = 'Jennifer'
1) Range – RangeS
2) Row – S
Mode – RangeS-S
DEMO
Range S-S Locks
Range S-U Locks
Existing granted mode
Requested
mode
S U X RangeS-S RangeI-N RangeX-X
RangeS-U Yes No No Yes No No
id name last_name
1 Alexander Wilson
2 Barbara Walker
3 Emily Robinson
4 Jennifer Clark
5 Richard Scott
6 Thomas Baker
7 William Morris
RangeS-U
RangeS-U
RangeS-U
UPDATE [Users]
SET last_name ='Smith'
WHERE name between 'Jennifer' and 'Richard'
1) Range – RangeS
2) Row – U
Mode – RangeS-U
DEMO
Range S-U Locks
Range X-X Locks
Existing granted mode
Requested
mode
S U X RangeS-U RangeI-N RangeS-S
RangeX-X No No No No No No
id name
1 Alexander
2 Barbara
3 Emily
4 Jennifer
5 Richard
6 Thomas
7 William
RangeS-U
RangeX-X
UPDATE [Users]
SET name ='Cooper'
WHERE name = 'Alexander'
1) Range – RangeX
2) Row – X
Mode – RangeX-X
DEMO
Range X-X Locks
DEMO
Key Range Lock on READCOMMITTED
Vitaliy Popovych
 Email: vitaliy.popovych@outlook.com
 Facebook: https://www.facebook.com/vitaliy.popovich.911
Thank you!

More Related Content

What's hot

Secure Socket Layer
Secure Socket LayerSecure Socket Layer
Secure Socket LayerPina Parmar
 
Virtual Nodes: Rethinking Topology in Cassandra
Virtual Nodes: Rethinking Topology in CassandraVirtual Nodes: Rethinking Topology in Cassandra
Virtual Nodes: Rethinking Topology in CassandraEric Evans
 
Introduction to Data Modeling with Apache Cassandra
Introduction to Data Modeling with Apache CassandraIntroduction to Data Modeling with Apache Cassandra
Introduction to Data Modeling with Apache CassandraDataStax Academy
 
Shell Scripts for Oracle Database and E-Business Suite.pdf
Shell Scripts for Oracle Database and E-Business Suite.pdfShell Scripts for Oracle Database and E-Business Suite.pdf
Shell Scripts for Oracle Database and E-Business Suite.pdfAkhashRamnath
 
SQL Joins With Examples | Edureka
SQL Joins With Examples | EdurekaSQL Joins With Examples | Edureka
SQL Joins With Examples | EdurekaEdureka!
 
MySQL Cluster - visão geral
MySQL Cluster - visão geralMySQL Cluster - visão geral
MySQL Cluster - visão geralMySQL Brasil
 
Oracle-DB: Performance Analysis with Panorama
Oracle-DB: Performance Analysis with PanoramaOracle-DB: Performance Analysis with Panorama
Oracle-DB: Performance Analysis with PanoramaPeter Ramm
 
MySQL Cluster: El ‘qué’ y el ‘cómo’.
MySQL Cluster: El ‘qué’ y el ‘cómo’.MySQL Cluster: El ‘qué’ y el ‘cómo’.
MySQL Cluster: El ‘qué’ y el ‘cómo’.Keith Hollman
 
FILE STRUCTURE IN DBMS
FILE STRUCTURE IN DBMSFILE STRUCTURE IN DBMS
FILE STRUCTURE IN DBMSAbhishek Dutta
 
Paper Presentation of SQL and PL/SQL
Paper Presentation of SQL and PL/SQL Paper Presentation of SQL and PL/SQL
Paper Presentation of SQL and PL/SQL PraveenRaj280263
 
Database Security And Authentication
Database Security And AuthenticationDatabase Security And Authentication
Database Security And AuthenticationSudeb Das
 
Cassandra Basics, Counters and Time Series Modeling
Cassandra Basics, Counters and Time Series ModelingCassandra Basics, Counters and Time Series Modeling
Cassandra Basics, Counters and Time Series ModelingVassilis Bekiaris
 
11. transaction sql
11. transaction sql11. transaction sql
11. transaction sqlUmang Gupta
 
Introduction to Clustered Indexes and Heaps
Introduction to Clustered Indexes and HeapsIntroduction to Clustered Indexes and Heaps
Introduction to Clustered Indexes and HeapsJason Strate
 

What's hot (20)

Secure Socket Layer
Secure Socket LayerSecure Socket Layer
Secure Socket Layer
 
Virtual Nodes: Rethinking Topology in Cassandra
Virtual Nodes: Rethinking Topology in CassandraVirtual Nodes: Rethinking Topology in Cassandra
Virtual Nodes: Rethinking Topology in Cassandra
 
Introduction to Data Modeling with Apache Cassandra
Introduction to Data Modeling with Apache CassandraIntroduction to Data Modeling with Apache Cassandra
Introduction to Data Modeling with Apache Cassandra
 
Array
ArrayArray
Array
 
Shell Scripts for Oracle Database and E-Business Suite.pdf
Shell Scripts for Oracle Database and E-Business Suite.pdfShell Scripts for Oracle Database and E-Business Suite.pdf
Shell Scripts for Oracle Database and E-Business Suite.pdf
 
PostgreSQL: Advanced indexing
PostgreSQL: Advanced indexingPostgreSQL: Advanced indexing
PostgreSQL: Advanced indexing
 
SQL Joins With Examples | Edureka
SQL Joins With Examples | EdurekaSQL Joins With Examples | Edureka
SQL Joins With Examples | Edureka
 
MySQL Cluster - visão geral
MySQL Cluster - visão geralMySQL Cluster - visão geral
MySQL Cluster - visão geral
 
Oracle-DB: Performance Analysis with Panorama
Oracle-DB: Performance Analysis with PanoramaOracle-DB: Performance Analysis with Panorama
Oracle-DB: Performance Analysis with Panorama
 
MySQL Cluster: El ‘qué’ y el ‘cómo’.
MySQL Cluster: El ‘qué’ y el ‘cómo’.MySQL Cluster: El ‘qué’ y el ‘cómo’.
MySQL Cluster: El ‘qué’ y el ‘cómo’.
 
Oracle Database Trigger
Oracle Database TriggerOracle Database Trigger
Oracle Database Trigger
 
FILE STRUCTURE IN DBMS
FILE STRUCTURE IN DBMSFILE STRUCTURE IN DBMS
FILE STRUCTURE IN DBMS
 
Paper Presentation of SQL and PL/SQL
Paper Presentation of SQL and PL/SQL Paper Presentation of SQL and PL/SQL
Paper Presentation of SQL and PL/SQL
 
Database Security And Authentication
Database Security And AuthenticationDatabase Security And Authentication
Database Security And Authentication
 
Cassandra Basics, Counters and Time Series Modeling
Cassandra Basics, Counters and Time Series ModelingCassandra Basics, Counters and Time Series Modeling
Cassandra Basics, Counters and Time Series Modeling
 
11. transaction sql
11. transaction sql11. transaction sql
11. transaction sql
 
Mysql
MysqlMysql
Mysql
 
Trigger
TriggerTrigger
Trigger
 
Red black tree
Red black treeRed black tree
Red black tree
 
Introduction to Clustered Indexes and Heaps
Introduction to Clustered Indexes and HeapsIntroduction to Clustered Indexes and Heaps
Introduction to Clustered Indexes and Heaps
 

Similar to SQL Locking Modes, Granularity, Isolation Levels and Key Range Locking

SQL Saturday 406 - Key Range Locking
SQL Saturday 406 - Key Range LockingSQL Saturday 406 - Key Range Locking
SQL Saturday 406 - Key Range LockingVitaliy Popovych
 
Present Paper: Protecting Free Expression Online on Freenet
Present Paper: Protecting Free Expression Online on FreenetPresent Paper: Protecting Free Expression Online on Freenet
Present Paper: Protecting Free Expression Online on FreenetSivadon Chaisiri
 
Splunk Enterprise for Information Security (Hands-On)
Splunk Enterprise for Information Security (Hands-On)                           Splunk Enterprise for Information Security (Hands-On)
Splunk Enterprise for Information Security (Hands-On) Splunk
 
Leverage the Network to Detect and Manage Threats
Leverage the Network to Detect and Manage ThreatsLeverage the Network to Detect and Manage Threats
Leverage the Network to Detect and Manage ThreatsCisco Canada
 
"Query Execution: Expectation - Reality (Level 300)" Денис Резник
"Query Execution: Expectation - Reality (Level 300)" Денис Резник"Query Execution: Expectation - Reality (Level 300)" Денис Резник
"Query Execution: Expectation - Reality (Level 300)" Денис РезникFwdays
 
File handling 2016
File handling 2016File handling 2016
File handling 2016TONY THOMAS
 
Next-generation sequencing data format and visualization with ngs.plot 2015
Next-generation sequencing data format and visualization with ngs.plot 2015Next-generation sequencing data format and visualization with ngs.plot 2015
Next-generation sequencing data format and visualization with ngs.plot 2015Li Shen
 
Network Securities.pptx
Network Securities.pptxNetwork Securities.pptx
Network Securities.pptxatharkaleem2
 

Similar to SQL Locking Modes, Granularity, Isolation Levels and Key Range Locking (10)

SQL Saturday 406 - Key Range Locking
SQL Saturday 406 - Key Range LockingSQL Saturday 406 - Key Range Locking
SQL Saturday 406 - Key Range Locking
 
Present Paper: Protecting Free Expression Online on Freenet
Present Paper: Protecting Free Expression Online on FreenetPresent Paper: Protecting Free Expression Online on Freenet
Present Paper: Protecting Free Expression Online on Freenet
 
SqlSaturday199 - Deadlocks
SqlSaturday199 - DeadlocksSqlSaturday199 - Deadlocks
SqlSaturday199 - Deadlocks
 
Splunk Enterprise for Information Security (Hands-On)
Splunk Enterprise for Information Security (Hands-On)                           Splunk Enterprise for Information Security (Hands-On)
Splunk Enterprise for Information Security (Hands-On)
 
Leverage the Network to Detect and Manage Threats
Leverage the Network to Detect and Manage ThreatsLeverage the Network to Detect and Manage Threats
Leverage the Network to Detect and Manage Threats
 
"Query Execution: Expectation - Reality (Level 300)" Денис Резник
"Query Execution: Expectation - Reality (Level 300)" Денис Резник"Query Execution: Expectation - Reality (Level 300)" Денис Резник
"Query Execution: Expectation - Reality (Level 300)" Денис Резник
 
File handling 2016
File handling 2016File handling 2016
File handling 2016
 
Next-generation sequencing data format and visualization with ngs.plot 2015
Next-generation sequencing data format and visualization with ngs.plot 2015Next-generation sequencing data format and visualization with ngs.plot 2015
Next-generation sequencing data format and visualization with ngs.plot 2015
 
Variables and Data Types
Variables and Data TypesVariables and Data Types
Variables and Data Types
 
Network Securities.pptx
Network Securities.pptxNetwork Securities.pptx
Network Securities.pptx
 

Recently uploaded

SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
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
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
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
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 

Recently uploaded (20)

SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
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
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
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
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 

SQL Locking Modes, Granularity, Isolation Levels and Key Range Locking

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