This document discusses different types of SQL locks including shared, update, exclusive, intent, schema, and bulk update locks. It explains the locking hierarchy in SQL Server from database level locks down to row level locks. The goal of locks is to maintain data integrity and consistency while allowing concurrent access. Locks prevent issues like deadlocks by controlling how transactions access and modify database objects.
2. OVERVIEW
UNDERSTANDING LOCKS
DIFFERENT MODELS/TYPES OF SQL SERVER LOCKS
i. SHARED LOCKS(S)
ii. UPDATE LOCKS(U)
iii. EXCLUSIVE LOCKS(X)
iv. INTENT LOCKS(I)
INTENT EXCLUSIVE(IX)
INTENT SHARED(IS)
INTENT UPDATE(IU)
v. SCHEMA LOCKS
SCHEMA MODIFICATION LOCKS
SCHEMA STABILITY LOCKS
vi. BULK UPDATE(BU)
LOCKING HIERARCHY/WHERE LOCKS ARE PUT IN DATABASE
3. UNDERSTANDING LOCKS
Lock is a mechanism to ensure data integrity , consistency while allowing concurrent access to
data by multiple users or to prevent concurrent use by different transactions.
Locks are held on SQL Server Resources (database, table, page, rows).
Locks are used to protect resources and locks are acquired based on given statement or query.
SQL Server locks objects when the transaction starts.
SQL Server releases the locked object when transaction is completed.
SQL Server uses Lock Escalation to manage the locking Granularity.
4. DIFFERENT MODELS/TYPES OF SQL SERVER LOCKS
1. Shared Locks(S):
SQL Server allow concurrent transactions to read a resource, but no other transaction can modify (INSERT,DELETE..) the resource while the lock is held. For
example SELECT.
Shared Locks are compatible with other Shared Locks as well as with Update Locks.
The Lock is released as soon as the data reading is finished.
2. Update Locks(U):
Update Locks are used to lock data that a user process wants to modify.
Update lock is compatible with shared lock(multiple) , in that both can be acquired on same resource simultaneously but only one Update Lock can be
acquired at a time on any resource.
Update locks are used to prevent DEADLOCK problem.
Once transaction holds Update Lock is ready to change the data , the Update lock will be transformed to an Exclusive Lock.
DEDALOCK: A SQL Server deadlock occurs when multiple Transactions are trying to modify data on the same resource at the same time, but those
transactions(processes) cannot continue to completion , which means neither process can proceed. The only way out of a DEADLOCK is for one of the
processes(transaction) to be terminated.
5. CONT
3. Exclusive Lock(X):
An Exclusive Lock is granted to a transaction when it is ready to perform data modification (making it ready in Update Lock).
When this lock is set , no other Transaction can interfere with that data as Exclusive Locks are incompatible with any other lock
type.
(Here, transaction means to modify page or row by DML commands like INSERT,UPDATE,DELETE).
4. Intent Locks(I):
Intent Locks are used for establishing a Lock Hierarchy(Database-Table-Page-Row).
Intent Locks improve locking performance by allowing SQL Server to inspect locks at the Table Level to determine the types of
locks held on Table , rather than searching multiple locks at the page or row level within that table.
i. Intent Shared(IS):Transaction has the intention to read some lower Hierarchy Resources.
ii. Intent Update Lock(IU):to update lower hierarchy Resources by transforming it to Shared Exclusive ,first read by Intent Shared
lock.
iii. Intent Exclusive(IE):to modify some of lower Hierarchy Resources.
6. CON
T
5. Schema Locks:
SQL Server uses schema Locks to maintain Structural Integrity(completeness , accuracy)
of SQL Server tables within a transaction.
i. Schema Modification Locks: Used when a table DDL operations is being
performed(such as Adding a column by ALTER , or deleting(dropping) a table , or
creating a table etc).
ii. Schema Stability Locks: Used when compiling queries , this lock do not Block any
Transactional locks, including Exclusive Locks. However , DDL operations can’t be
performed on the table.
6. Bulk Update(BU):
BU locks are used when bulk copying data into a table when issued with a TABLOCK
hint/argument or when a user wants to insert huge data in database.
BU lock allows processes to bulk copy data concurrently into the same table.
7. LOCKING HIERARCHY/WHERE LOCKS ARE PUT IN DATABASE
Locking Hierarchy is applied when reading or changing of data is performed.
Lock Hierarchy starts with the Database at the highest hierarchy level and down via Table
and Page to the Row at the Lowest Level.
Essentially, there is always a Shared Lock on the Database Level to prevent
dropping(deletion) of the database.
LOCK HIERARCHY OF SQL SERVER