lock compatibility / Lock Granularity and Hierarchies

--------------------------------------------------------------------------------------------------------------------

Lock Compatibility (Database Engine)

Lock compatibility controls whether multiple transactions can acquire locks on the same resource at the same time. If a resource is already locked by another transaction, a new lock request can be granted only if the mode of the requested lock is compatible with the mode of the existing lock. If the mode of the requested lock is not compatible with the existing lock, the transaction requesting the new lock waits for the existing lock to be released or for the lock timeout interval to expire. For example, no lock modes are compatible with exclusive locks. While an exclusive (X) lock is held, no other transaction can acquire a lock of any kind (shared, update, or exclusive) on that resource until the exclusive (X) lock is released. Alternatively, if a shared (S) lock has been applied to a resource, other transactions can also acquire a shared lock or an update (U) lock on that item even if the first transaction has not completed. However, other transactions cannot acquire an exclusive lock until the shared lock has been released.

Applies to: SQL Server 2008 R2 and higher versions.

The following table shows the compatibility of the most commonly encountered lock modes.

 

Existing granted mode

 

 

 

 

 

Requested mode

IS

S

U

IX

SIX

X

Intent shared (IS)

Yes

Yes

Yes

Yes

Yes

No

Shared (S)

Yes

Yes

Yes

No

No

No

Update (U)

Yes

Yes

No

No

No

No

Intent exclusive (IX)

Yes

No

No

Yes

No

No

Shared with intent exclusive (SIX)

Yes

No

No

No

No

No

Exclusive (X)

No

No

No

No

No

No

NoteNote

An intent exclusive (IX) lock is compatible with an IX lock mode because IX means the intention is to update only some of the rows rather than all of them. Other transactions that attempt to read or update some of the rows are also permitted as long as they are not the same rows being updated by other transactions. Further, if two transactions attempt to update the same row, both transactions will be granted an IX lock at table and page level. However, one transaction will be granted an X lock at row level. The other transaction must wait until the row-level lock is removed.

Complete Lock Compatibility Matrix

Use the following table to determine the compatibility of all the lock modes available in Microsoft SQL Server.

Diagram showing lock compatibility matrix
-----------------------------------------------------------------------------------------------------------------------------

Lock Granularity and Hierarchies

The Microsoft SQL Server Database Engine has multigranular locking that allows different types of resources to be locked by a transaction. To minimize the cost of locking, the Database Engine 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. However, it has a lower overhead because fewer locks are being maintained.

Applies to: SQL Server 2008 R2 and higher versions.

The Database Engine often has to acquire locks at multiple levels of granularity to fully protect a resource. This group of locks at multiple levels of granularity is called a lock hierarchy. For example, to fully protect a read of an index, an instance of the Database Engine may have to acquire share locks on rows and intent share locks on the pages and table.

The following table shows the resources that the Database Engine can lock.

Resource

Description

RID

A row identifier used to lock a single row within a heap.

KEY

A row lock within an index used to protect key ranges in serializable transactions.

PAGE

An 8-kilobyte (KB) page in a database, such as data or index pages.

EXTENT

A contiguous group of eight pages, such as data or index pages.

HoBT

A heap or B-tree. A lock protecting a B-tree (index) or the heap data pages in a table that does not have a clustered index.

TABLE

The entire table, including all data and indexes.

FILE

A database file.

APPLICATION

An application-specified resource.

METADATA

Metadata locks.

ALLOCATION_UNIT

An allocation unit.

DATABASE

The entire database.

NoteNote

HoBT and TABLE locks can be affected by the LOCK_ESCALATION option of ALTER TABLE.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值