Lock escalation is when the system combines multiple locks into a higher level one. This is done to recover resources taken by the other finer granular locks. The system automatically does this. The threshold for this escalation is determined dynamically by the server.
To reduce system over head by recovering locks
Maximize the efficiency of queries
Helps to minimize the required memory to keep track of locks.
SQL Server has 3 main lock types:
Shared: Locks are compatible with other shared and update locks.
Update: Locks are compatible with shared locks.
Exclusive: Locks are not compatible with any other locks.
Apart from lock types, there are transaction isolation levels for managing security among transactions:
SQL Server has some locking optimizer hints along with lock types:
When a request for exclusive lock is denied again and again because a series of overlapping shared locks are interfering with each other and to adapt from each other they keep on changing the status, it is known as live lock.
When two computer programs sharing the same resource prevent each other from accessing the resource results in a deadlock. This deadlock results in finishing the program.
P1 requests R1 and receives it.
P2 requests R2 and receives it.
P1 requests resource R2 and is queued up, pending the release of R2.
P2 requests resource R1 and is queued up, pending the release of R1
Here, P and R is Program and Resource respectively.
Guidelines to minimize deadlocks:-
Avoid user interaction in the transactions. The transaction must not rely on any inputs from the user.
The concurrent transactions must access data in the same order. There should be consistency in which the operations occur
Transactions must be short and simple to avoid deadlocks. Long transactions may block other necessary activities
A lower isolation level like read committed must be used. Using lower isolation levels reduces the possibility of holding shared locks for a longer time
Bound connections should be used. Here, two or more connections opened by the same application can assist each other.
Different lock modes:
Shared (S): Mostly used for Read only operations like SELECT statements. It allows concurrent transactions to read data. No other transaction can modify the data until the lock is present. The lock is released as soon as the read is over.
Update locks (U): used to prevent dead locks. Used on resources that can be updated. Common forms of deadlocks occur when multiple sessions are reading, locking, and potentially updating resources later.
Exclusive (X): used for data modifications statements like INSERT, UPDATE or DELETE. This lock ensures multiple updates cant be made simultaneously.
Schema: Sch-M or Schema modification locks are used when an operation related to the table schema is being performed. Schema Stability or Sch-S locks are used when queries are being complied.
Bulk update locks: Used when bulk copy is being performed. BU allows processes to bulk copy data concurrently into the same table while preventing other processes that are not bulk copying data from accessing the table.
Lost updates: this occurs when two or more transactions are trying to update same row without being aware of each other. The last update overwrites updates made by the other transactions, which results in lost data.
Uncommitted dependency (Dirty read): This occurs when a second transaction selects a row that is being updated by another transaction. This second transaction is reading data that may not have been committed.
Inconsistent Analysis (Nonrepeatable Read): This occurs when a second transaction accesses the same row several times and reads different data each time. It is similar to dirty read. However, here it reads committed data, but different data each time.
Phantom Reads: This occurs when a range of rows which is being read by a transaction is deleted or updated. The transaction's first read of the range of rows shows a row that no longer exists in the second or succeeding read, as a result of a deletion by a different transaction.
Optimistic concurrency: - Assumes that a resource is likely to be available at all times. This means that resource locking is very unlikely. If a conflict occurs, the application must read the data and attempt the change again.
Pessimistic concurrency : - this locks the resources as and when required. A transaction can be assured to be completed unless a deadlock occurs
Types of concurrency problem:-
Lost or buried updates: - When the same row is selected for updates by two or more transactions and updates the row based on the value originally selected. Here, each transaction is unaware of the other transactions. The last update overwrites updates made by the other transactions, which results in lost data.
Uncommited dependency: - here, a transaction reads data of another transaction which has not been committed yet. The value may be changed by the other transaction.
Inconsistent analysis: - here, the transaction reads the data inconsistently. This means that every time the data is read; different values are read. This is because another transaction is continuously updating the data.
Phantom Read: - Here, an insert or update is done on a row that belongs to some other transaction. Hence the transaction may read a row that may be deleted by some other transaction.
Lock modes in SQL Server 2000:
Shared: Used for operations that read data. When shared lock is applied concurrent transactions can read data/resource but not modify it. This lock is released as soon as the transaction finishes.
Update: These locks are used when there is a need to update a row or page and later promote the update page lock to an exclusive lock before making the actual changes. Update locks are used to prevent deadlocks.
Exclusive: Used for data modification operations. No other transaction can read or modify data while Exclusive lock has been acquired.
Intent: These are used when SQL Server wants to acquire a shared or exclusive lock on some resource lower down in the hierarchy.
Schema: These are used when an operation dependent on the schema of a table is executing.
Bulk update: These are used during bulk copying of data into tables provided either TABLOCK hint is mentioned or table lock on bulk upload table option is set.
Key range: These are used by SQL Server to prevent phantom insertions/deletions into a set of records which are being accessed by a transaction.