SlideShare a Scribd company logo
Locking in SQL Server
Microsoft® SQL Server™ has allows different types of
resources to be locked by a transaction. To minimize the cost of
locking, SQL Server locks resources automatically at a level
appropriate to the task. Locking at a smaller granularity, such
as rows, increases concurrency, but has a higher overhead
because more locks must be held if many rows are locked.
Locking at a larger granularity, such as tables, are expensive in
terms of concurrency because locking an entire table restricts
access to any part of the table by other transactions, but has a
lower overhead because fewer locks are being maintained.
INTRODUCTION
Locking is a major part of every RDBMS and is important
to know about. It is a database functionality which without
a multi-user environment could not work. The main
problem of locking is that in an essence it's a logical and
not physical problem. This means that no amount of
hardware will help you in the end. Yes you might cut
execution times but this is only a virtual fix. In a heavy
multi-user environment any logical problems will appear
sooner or later.
SQL Server uses these resource lock modes.
SQL Server can lock these resources
(listed in order of increasing granularity).
Shared locks (S)
 Shared (S) locks allow concurrent
transactions to read (SELECT) a resource.
No other transactions can modify the data
while shared (S) locks exist on the
resource. Shared (S) locks on a resource
are released as soon as the data has been
read, unless the transaction isolation level
is set to repeatable read or higher, or a
locking hint is used to retain the shared (S)
locks for the duration of the transaction.
Example :
Update locks (U)
 Update locks are a mix of shared and
exclusive locks. When a DML statement is
executed SQL Server has to find the data it
wants to modify first, so to avoid lock
conversion deadlocks an update lock is used.
Only one update lock can be held on the data
at one time, similar to an exclusive lock. But
the difference here is that the update lock itself
can't modify the underlying data. It has to be
converted to an exclusive lock before the
modification takes place. You can also force
an update lock with the UPDLOCK hint.
Example:
Exclusive locks (X)
 Exclusive locks are used to lock data being
modified by one transaction thus
preventing modifications by other
concurrent transactions. You can read data
held by exclusive lock only by specifying a
NOLOCK hint or using a read uncommitted
isolation level. Because DML statements
first need to read the data they want to
modify you'll always find Exclusive locks
accompanied by shared locks on that same
data.
Example:
Intent locks (I)
 Intent locks are a means in which a transaction
notifies other transaction that it is intending to lock
the data. Thus the name. Their purpose is to assure
proper data modification by preventing other
transactions to acquire a lock on the object higher in
lock hierarchy. What this means is that before you
obtain a lock on the page or the row level an intent
lock is set on the table. This prevents other
transactions from putting exclusive locks on the table
that would try to cancel the row/page lock. In the
example we can see the intent exclusive locks being
placed on the page and the table where the key is to
protect the data from being locked by other
transactions.
Example:
Schema locks (Sch)
 There are two types of schema locks:
 Schema stability lock (Sch-S): Used
while generating execution plans. These
locks don't block access to the object
data.
 Schema modification lock (Sch-M):
Used while executing a DDL statement.
Blocks access to the object data since
its structure is being changed.
Example:
we can see the Sch-S and Sch-M locks being taken on the system tables and
the TestTable plus a lot of other locks on the system tables.
Bulk Update locks (BU)
 Bulk Update locks are used by bulk
operations when TABLOCK hint is used
by the import. This allows for multiple
fast concurrent inserts by disallowing
data reading to other transactions.
Conclusion
 Hopefully this PPT has shed some light on
how SQL Server operates with locks and
why is locking of such importance to proper
application and database design and
operation. Remember that locking
problems are of logical and not physical
nature so they have to be well thought out.
Locking goes hand in hand with transaction
isolation levels so be familiar with those
too. In the next article I'll show some ways
to resolve locking problems.
Created By
PRASHANT
THE END

More Related Content

What's hot

Svetlin Nakov - Database Transactions
Svetlin Nakov - Database TransactionsSvetlin Nakov - Database Transactions
Svetlin Nakov - Database TransactionsSvetlin Nakov
 
Transactions and Concurrency Control
Transactions and Concurrency ControlTransactions and Concurrency Control
Transactions and Concurrency Control
Dilum Bandara
 
Concurrency Conrol
Concurrency ConrolConcurrency Conrol
Concurrency Conrol
lubna19
 
Dbms
DbmsDbms
Transaction management and concurrency control
Transaction management and concurrency controlTransaction management and concurrency control
Transaction management and concurrency control
Dhani Ahmad
 
Transaction slide
Transaction slideTransaction slide
Transaction slide
shawon roy
 
Transaction
TransactionTransaction
Transaction
Amin Omi
 
15. Transactions in DBMS
15. Transactions in DBMS15. Transactions in DBMS
15. Transactions in DBMSkoolkampus
 
Chapter23
Chapter23Chapter23
Chapter23
gourab87
 
Concurrency control
Concurrency  controlConcurrency  control
Concurrency control
Javed Khan
 
Database Transactions and SQL Server Concurrency
Database Transactions and SQL Server ConcurrencyDatabase Transactions and SQL Server Concurrency
Database Transactions and SQL Server Concurrency
Boris Hristov
 
Transaction management
Transaction managementTransaction management
Transaction managementrenuka_a
 
Deadlock in database
Deadlock in databaseDeadlock in database
Deadlock in database
Tayyab Hussain
 
16. Concurrency Control in DBMS
16. Concurrency Control in DBMS16. Concurrency Control in DBMS
16. Concurrency Control in DBMSkoolkampus
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
Soumyajit Dutta
 
Transactions in dbms
Transactions in dbmsTransactions in dbms
Transactions in dbms
Nancy Gulati
 
Distributed database management system
Distributed database management  systemDistributed database management  system
Distributed database management system
Pooja Dixit
 
protocols of concurrency control
protocols of concurrency controlprotocols of concurrency control
protocols of concurrency control
MOHIT DADU
 

What's hot (20)

Svetlin Nakov - Database Transactions
Svetlin Nakov - Database TransactionsSvetlin Nakov - Database Transactions
Svetlin Nakov - Database Transactions
 
Transactions and Concurrency Control
Transactions and Concurrency ControlTransactions and Concurrency Control
Transactions and Concurrency Control
 
Database System Architectures
Database System ArchitecturesDatabase System Architectures
Database System Architectures
 
Concurrency Conrol
Concurrency ConrolConcurrency Conrol
Concurrency Conrol
 
Dbms
DbmsDbms
Dbms
 
Transaction management and concurrency control
Transaction management and concurrency controlTransaction management and concurrency control
Transaction management and concurrency control
 
Transaction slide
Transaction slideTransaction slide
Transaction slide
 
Transaction
TransactionTransaction
Transaction
 
15. Transactions in DBMS
15. Transactions in DBMS15. Transactions in DBMS
15. Transactions in DBMS
 
Chapter23
Chapter23Chapter23
Chapter23
 
Concurrency control
Concurrency  controlConcurrency  control
Concurrency control
 
Database Transactions and SQL Server Concurrency
Database Transactions and SQL Server ConcurrencyDatabase Transactions and SQL Server Concurrency
Database Transactions and SQL Server Concurrency
 
Transaction management
Transaction managementTransaction management
Transaction management
 
Deadlock in database
Deadlock in databaseDeadlock in database
Deadlock in database
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
16. Concurrency Control in DBMS
16. Concurrency Control in DBMS16. Concurrency Control in DBMS
16. Concurrency Control in DBMS
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
 
Transactions in dbms
Transactions in dbmsTransactions in dbms
Transactions in dbms
 
Distributed database management system
Distributed database management  systemDistributed database management  system
Distributed database management system
 
protocols of concurrency control
protocols of concurrency controlprotocols of concurrency control
protocols of concurrency control
 

Viewers also liked

Notification Service 2005
Notification Service 2005Notification Service 2005
Notification Service 2005
shram2k
 
Uncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony DavisUncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony Davis
Red Gate Software
 
Sql server performance tuning and optimization
Sql server performance tuning and optimizationSql server performance tuning and optimization
Sql server performance tuning and optimization
Manish Rawat
 
SQL Server Profiler & Performance Monitor - SarabPreet Singh
SQL Server Profiler & Performance Monitor - SarabPreet SinghSQL Server Profiler & Performance Monitor - SarabPreet Singh
SQL Server Profiler & Performance Monitor - SarabPreet SinghRishu Mehra
 
Consultas en MS SQL Server 2012
Consultas en MS SQL Server 2012Consultas en MS SQL Server 2012
Consultas en MS SQL Server 2012
CarlosFloresRoman
 
MS SQL SERVER: Creating Views
MS SQL SERVER: Creating ViewsMS SQL SERVER: Creating Views
MS SQL SERVER: Creating Views
sqlserver content
 
Sql db optimization
Sql db optimizationSql db optimization
Sql db optimization
Nikhildas P C
 
Using grouping sets in sql server 2008 tech republic
Using grouping sets in sql server 2008   tech republicUsing grouping sets in sql server 2008   tech republic
Using grouping sets in sql server 2008 tech republicKaing Menglieng
 
Sql tuning guideline
Sql tuning guidelineSql tuning guideline
Sql tuning guidelineSidney Chen
 
e computer notes - Subqueries
e computer notes - Subqueriese computer notes - Subqueries
e computer notes - Subqueriesecomputernotes
 
Sub-queries,Groupby and having in SQL
Sub-queries,Groupby and having in SQLSub-queries,Groupby and having in SQL
Sub-queries,Groupby and having in SQL
Vikash Sharma
 
Triggers-Sequences-SQL
Triggers-Sequences-SQLTriggers-Sequences-SQL
Triggers-Sequences-SQLPatrick Seery
 
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1 "Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
Andriy Krayniy
 
Lecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML TriggersLecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML Triggers
Alexey Furmanov
 
Sql query analyzer & maintenance
Sql query analyzer & maintenanceSql query analyzer & maintenance
Sql query analyzer & maintenance
nspyrenet
 
SQL Server - Using Tools to Analyze Query Performance
SQL Server - Using Tools to Analyze Query PerformanceSQL Server - Using Tools to Analyze Query Performance
SQL Server - Using Tools to Analyze Query Performance
Marek Maśko
 

Viewers also liked (20)

Notification Service 2005
Notification Service 2005Notification Service 2005
Notification Service 2005
 
Uncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony DavisUncovering SQL Server query problems with execution plans - Tony Davis
Uncovering SQL Server query problems with execution plans - Tony Davis
 
Sql server performance tuning and optimization
Sql server performance tuning and optimizationSql server performance tuning and optimization
Sql server performance tuning and optimization
 
SQL Server Profiler & Performance Monitor - SarabPreet Singh
SQL Server Profiler & Performance Monitor - SarabPreet SinghSQL Server Profiler & Performance Monitor - SarabPreet Singh
SQL Server Profiler & Performance Monitor - SarabPreet Singh
 
Consultas en MS SQL Server 2012
Consultas en MS SQL Server 2012Consultas en MS SQL Server 2012
Consultas en MS SQL Server 2012
 
MS SQL SERVER: Creating Views
MS SQL SERVER: Creating ViewsMS SQL SERVER: Creating Views
MS SQL SERVER: Creating Views
 
Joins SQL Server
Joins SQL ServerJoins SQL Server
Joins SQL Server
 
Sql db optimization
Sql db optimizationSql db optimization
Sql db optimization
 
Using grouping sets in sql server 2008 tech republic
Using grouping sets in sql server 2008   tech republicUsing grouping sets in sql server 2008   tech republic
Using grouping sets in sql server 2008 tech republic
 
Sql tuning guideline
Sql tuning guidelineSql tuning guideline
Sql tuning guideline
 
Sql xp 04
Sql xp 04Sql xp 04
Sql xp 04
 
Statistics
StatisticsStatistics
Statistics
 
e computer notes - Subqueries
e computer notes - Subqueriese computer notes - Subqueries
e computer notes - Subqueries
 
Sub-queries,Groupby and having in SQL
Sub-queries,Groupby and having in SQLSub-queries,Groupby and having in SQL
Sub-queries,Groupby and having in SQL
 
Review of SQL
Review of SQLReview of SQL
Review of SQL
 
Triggers-Sequences-SQL
Triggers-Sequences-SQLTriggers-Sequences-SQL
Triggers-Sequences-SQL
 
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1 "Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
 
Lecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML TriggersLecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML Triggers
 
Sql query analyzer & maintenance
Sql query analyzer & maintenanceSql query analyzer & maintenance
Sql query analyzer & maintenance
 
SQL Server - Using Tools to Analyze Query Performance
SQL Server - Using Tools to Analyze Query PerformanceSQL Server - Using Tools to Analyze Query Performance
SQL Server - Using Tools to Analyze Query Performance
 

Similar to Locking in SQL Server

SQL locks-presentation
SQL locks-presentationSQL locks-presentation
SQL locks-presentation
Nuzhat Bhat
 
Locking and concurrency
Locking and concurrencyLocking and concurrency
Locking and concurrency
RumeysaDinsoy
 
Intro to tsql unit 12
Intro to tsql   unit 12Intro to tsql   unit 12
Intro to tsql unit 12Syed Asrarali
 
Locking unit 1 topic 3
Locking unit 1 topic 3Locking unit 1 topic 3
Locking unit 1 topic 3avniS
 
SAP ABAP Lock concept and enqueue
SAP ABAP Lock concept and enqueueSAP ABAP Lock concept and enqueue
SAP ABAP Lock concept and enqueue
Milind Patil
 
Database consistency in NonStop SQL/MX
Database consistency in NonStop SQL/MXDatabase consistency in NonStop SQL/MX
Database consistency in NonStop SQL/MX
Frans Jongma
 
Understanding Locking Mechanisms in Database Systems
Understanding Locking Mechanisms in Database SystemsUnderstanding Locking Mechanisms in Database Systems
Understanding Locking Mechanisms in Database Systems
kiansahafi
 
Locks In Disributed Systems
Locks In Disributed SystemsLocks In Disributed Systems
Locks In Disributed Systems
mridul mishra
 
Sql server-dba
Sql server-dbaSql server-dba
Sql server-dba
NaviSoft
 
Managing Memory & Locks - Series 2 Transactions & Lock management
Managing  Memory & Locks - Series 2 Transactions & Lock managementManaging  Memory & Locks - Series 2 Transactions & Lock management
Managing Memory & Locks - Series 2 Transactions & Lock management
DAGEOP LTD
 
Oracle table lock modes
Oracle table lock modesOracle table lock modes
Oracle table lock modes
Franck Pachot
 
Locks with updt nowait
Locks with updt nowaitLocks with updt nowait
Locks with updt nowaitavniS
 
Look inside the locking mechanism
Look inside the locking mechanismLook inside the locking mechanism
Look inside the locking mechanism
Liron Amitzi
 
Abhishek Kumar - CloudStack Locking Service
Abhishek Kumar - CloudStack Locking ServiceAbhishek Kumar - CloudStack Locking Service
Abhishek Kumar - CloudStack Locking Service
ShapeBlue
 
Chap 07 Locking.pptx
Chap 07  Locking.pptxChap 07  Locking.pptx
Chap 07 Locking.pptx
SunenaGhulamani
 
Data warehouse 2.0 and sql server architecture and vision
Data warehouse 2.0 and sql server architecture and visionData warehouse 2.0 and sql server architecture and vision
Data warehouse 2.0 and sql server architecture and visionKlaudiia Jacome
 
Sql server concurrency
Sql server concurrencySql server concurrency
Sql server concurrency
Mahabubur Rahaman
 
Sql interview question part 5
Sql interview question part 5Sql interview question part 5
Sql interview question part 5kaashiv1
 
Compare the capabilities of the Microsoft Access, Microsoft SQL Serv.pdf
Compare the capabilities of the Microsoft Access, Microsoft SQL Serv.pdfCompare the capabilities of the Microsoft Access, Microsoft SQL Serv.pdf
Compare the capabilities of the Microsoft Access, Microsoft SQL Serv.pdf
arihantplastictanksh
 

Similar to Locking in SQL Server (20)

SQL locks-presentation
SQL locks-presentationSQL locks-presentation
SQL locks-presentation
 
Locking and concurrency
Locking and concurrencyLocking and concurrency
Locking and concurrency
 
Intro to tsql unit 12
Intro to tsql   unit 12Intro to tsql   unit 12
Intro to tsql unit 12
 
Locking unit 1 topic 3
Locking unit 1 topic 3Locking unit 1 topic 3
Locking unit 1 topic 3
 
SAP ABAP Lock concept and enqueue
SAP ABAP Lock concept and enqueueSAP ABAP Lock concept and enqueue
SAP ABAP Lock concept and enqueue
 
Database consistency in NonStop SQL/MX
Database consistency in NonStop SQL/MXDatabase consistency in NonStop SQL/MX
Database consistency in NonStop SQL/MX
 
Understanding Locking Mechanisms in Database Systems
Understanding Locking Mechanisms in Database SystemsUnderstanding Locking Mechanisms in Database Systems
Understanding Locking Mechanisms in Database Systems
 
Locks In Disributed Systems
Locks In Disributed SystemsLocks In Disributed Systems
Locks In Disributed Systems
 
Sql server-dba
Sql server-dbaSql server-dba
Sql server-dba
 
Managing Memory & Locks - Series 2 Transactions & Lock management
Managing  Memory & Locks - Series 2 Transactions & Lock managementManaging  Memory & Locks - Series 2 Transactions & Lock management
Managing Memory & Locks - Series 2 Transactions & Lock management
 
Oracle table lock modes
Oracle table lock modesOracle table lock modes
Oracle table lock modes
 
Locks with updt nowait
Locks with updt nowaitLocks with updt nowait
Locks with updt nowait
 
Look inside the locking mechanism
Look inside the locking mechanismLook inside the locking mechanism
Look inside the locking mechanism
 
Abhishek Kumar - CloudStack Locking Service
Abhishek Kumar - CloudStack Locking ServiceAbhishek Kumar - CloudStack Locking Service
Abhishek Kumar - CloudStack Locking Service
 
Chap 07 Locking.pptx
Chap 07  Locking.pptxChap 07  Locking.pptx
Chap 07 Locking.pptx
 
Data warehouse 2.0 and sql server architecture and vision
Data warehouse 2.0 and sql server architecture and visionData warehouse 2.0 and sql server architecture and vision
Data warehouse 2.0 and sql server architecture and vision
 
Sql server concurrency
Sql server concurrencySql server concurrency
Sql server concurrency
 
Ebook5
Ebook5Ebook5
Ebook5
 
Sql interview question part 5
Sql interview question part 5Sql interview question part 5
Sql interview question part 5
 
Compare the capabilities of the Microsoft Access, Microsoft SQL Serv.pdf
Compare the capabilities of the Microsoft Access, Microsoft SQL Serv.pdfCompare the capabilities of the Microsoft Access, Microsoft SQL Serv.pdf
Compare the capabilities of the Microsoft Access, Microsoft SQL Serv.pdf
 

Locking in SQL Server

  • 1. Locking in SQL Server
  • 2. Microsoft® SQL Server™ has allows different types of resources to be locked by a transaction. To minimize the cost of locking, SQL Server locks resources automatically at a level appropriate to the task. Locking at a smaller granularity, such as rows, increases concurrency, but has a higher overhead because more locks must be held if many rows are locked. Locking at a larger granularity, such as tables, are expensive in terms of concurrency because locking an entire table restricts access to any part of the table by other transactions, but has a lower overhead because fewer locks are being maintained. INTRODUCTION
  • 3. Locking is a major part of every RDBMS and is important to know about. It is a database functionality which without a multi-user environment could not work. The main problem of locking is that in an essence it's a logical and not physical problem. This means that no amount of hardware will help you in the end. Yes you might cut execution times but this is only a virtual fix. In a heavy multi-user environment any logical problems will appear sooner or later.
  • 4. SQL Server uses these resource lock modes.
  • 5. SQL Server can lock these resources (listed in order of increasing granularity).
  • 6. Shared locks (S)  Shared (S) locks allow concurrent transactions to read (SELECT) a resource. No other transactions can modify the data while shared (S) locks exist on the resource. Shared (S) locks on a resource are released as soon as the data has been read, unless the transaction isolation level is set to repeatable read or higher, or a locking hint is used to retain the shared (S) locks for the duration of the transaction.
  • 8. Update locks (U)  Update locks are a mix of shared and exclusive locks. When a DML statement is executed SQL Server has to find the data it wants to modify first, so to avoid lock conversion deadlocks an update lock is used. Only one update lock can be held on the data at one time, similar to an exclusive lock. But the difference here is that the update lock itself can't modify the underlying data. It has to be converted to an exclusive lock before the modification takes place. You can also force an update lock with the UPDLOCK hint.
  • 10. Exclusive locks (X)  Exclusive locks are used to lock data being modified by one transaction thus preventing modifications by other concurrent transactions. You can read data held by exclusive lock only by specifying a NOLOCK hint or using a read uncommitted isolation level. Because DML statements first need to read the data they want to modify you'll always find Exclusive locks accompanied by shared locks on that same data.
  • 12. Intent locks (I)  Intent locks are a means in which a transaction notifies other transaction that it is intending to lock the data. Thus the name. Their purpose is to assure proper data modification by preventing other transactions to acquire a lock on the object higher in lock hierarchy. What this means is that before you obtain a lock on the page or the row level an intent lock is set on the table. This prevents other transactions from putting exclusive locks on the table that would try to cancel the row/page lock. In the example we can see the intent exclusive locks being placed on the page and the table where the key is to protect the data from being locked by other transactions.
  • 14. Schema locks (Sch)  There are two types of schema locks:  Schema stability lock (Sch-S): Used while generating execution plans. These locks don't block access to the object data.  Schema modification lock (Sch-M): Used while executing a DDL statement. Blocks access to the object data since its structure is being changed.
  • 15. Example: we can see the Sch-S and Sch-M locks being taken on the system tables and the TestTable plus a lot of other locks on the system tables.
  • 16. Bulk Update locks (BU)  Bulk Update locks are used by bulk operations when TABLOCK hint is used by the import. This allows for multiple fast concurrent inserts by disallowing data reading to other transactions.
  • 17. Conclusion  Hopefully this PPT has shed some light on how SQL Server operates with locks and why is locking of such importance to proper application and database design and operation. Remember that locking problems are of logical and not physical nature so they have to be well thought out. Locking goes hand in hand with transaction isolation levels so be familiar with those too. In the next article I'll show some ways to resolve locking problems.