写SQL最常见的问题就是Dead Lock了。本篇简单介绍入门级别的Lock使用和排查。

首先来看MSDN上的官方文档(https://technet.microsoft.com/en-us/library/jj856598(v=sql.110).aspx)。

摘要一下,SQL Server可以进行Lock的Resource:

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.

Lock的类型:

Lock mode Description
Shared (S) Used for read operations that do not change or update data, such as a SELECT statement.
Update (U) Used on resources that can be updated. Prevents a common form of deadlock that occurs when multiple sessions are reading, locking, and potentially updating resources later.
Exclusive (X) Used for data-modification operations, such as INSERT, UPDATE, or DELETE. Ensures that multiple updates cannot be made to the same resource at the same time.
Intent Used to establish a lock hierarchy. The types of intent locks are: intent shared (IS), intent exclusive (IX), and shared with intent exclusive (SIX).
Schema Used when an operation dependent on the schema of a table is executing. The types of schema locks are: schema modification (Sch-M) and schema stability (Sch-S).
Bulk Update (BU) Used when bulk copying data into a table and the TABLOCK hint is specified.
Key-range Protects the range of rows read by a query when using the serializable transaction isolation level. Ensures that other transactions cannot insert rows that would qualify for the queries of the serializable transaction if the queries were run again.

T-SQL中,使用Lock最简单的方法当然是SELECT ... FOR UPDATE,选中对应的ROW进行Lock以便Update。

进行Lock排查,可以通过以下方式进行查看当前Lock的状态:

  • exec sp_lock; 这是最原始的方式,dump所有Lock相关的信息。
  • select cmd,* from sys.sysprocesseswhere blocked > 0通过查看当前sysprocesses的方式来抉择那些process被blocked。配合上exec sp_who2和kill,分别用来查看process的信息和终止指定的process。
  • select * from sys.dm_tran_locks; Dynamic View dm_trans_locks返回当前系统中的locks。Dynamic Views and Functions请参阅:https://msdn.microsoft.com/en-us/library/ms188754.aspx

是为之记。
Alva Chien
2016.5.30

最新文章

  1. sublime text快捷键
  2. loj 1002(spfa变形)
  3. 读书笔记——数据库的ADO开发总结
  4. Java基础(58):Eclipse中的快捷键大全(转)
  5. java.lang.IllegalArgumentException: addChild: Child name '/SSHE' is not unique
  6. zoj 3822 Domination (概率dp 天数期望)
  7. [转] 接触C# 反射 2
  8. oracle数据操纵语言(DML)data manipulation language
  9. Gulp 自动化的项目构建工具
  10. 纯CSS3实现宽屏二级下拉菜单
  11. 黑马程序员_Java面向对象3_多态
  12. 不要62 hdu2089
  13. JavaScript之图片懒加载的实现
  14. linux系统,关于Python多版本共存
  15. org.springframework.dao.InvalidDataAccessResourceUsageException: Unexpected cursor position change. Spring Batch 错误
  16. 使用VSFTPD传输文件
  17. 启动oracle command
  18. FTP文件传输
  19. Git中清除远程仓库HTTPS认证信息的方法
  20. mysql字符编码相关

热门文章

  1. [USACO17JAN]Building a Tall Barn建谷仓
  2. 关于StreamReader的知识分享
  3. idea迁移到其他电脑,省去重新安装破解及配置
  4. windows 抓hash获取管理员密码
  5. JAVA动态代理 你真的完全了解Java动态代理吗?
  6. Spring Boot构建的Web项目如何在服务端校验表单输入
  7. 百万年薪python之路 -- 内置函数
  8. 2019.10.29 CSP%您赛第四场t2
  9. 总结Java equals()和hashCode()的联系
  10. Veins(车载通信仿真框架)入门教程