写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

T-SQL Part V: Locks的更多相关文章

  1. Oracle 11g实时SQL监控 v$sql_monitor

    Oracle 11g实时SQL监控: 前面提到,在Oracle Database 11g中,v$session视图增加了一些新的字段,这其中包括SQL_EXEC_START和SQL_EXEC_ID, ...

  2. 转:oracle常见重要视图-v$sql,v$sql_plan,v$sqltext,v$sqlarea,v$sql_plan_statistcs

    v$sql V$SQL中存储具体的SQL语句. 一条语句可以映射多个cursor,因为对象所指的cursor可以有不同用户(如例1).如果有多个cursor(子游标)存在,在V$SQLAREA为所有c ...

  3. oracle 入门笔记--v$sql和v$sqlarea视图(转载)

    转载于作者:dbtan 原文链接:http://www.dbtan.com/2009/12/vsql-and-vsqlarea-view.html v$sql和v$sqlarea视图: 上文提到,v$ ...

  4. v$sql、v$sqlarea、v$sqltext、v$sql_plan

    转自:http://gldbhome.blog.51cto.com/1552935/886316 视图v$sqltext中没有SQL语句的相关统计信息,但是v$sqltext用多行来保存sql语句,而 ...

  5. 转:V$SQL,V$SQLAREA,V$SQLTEXT

    V$SQL*表用于查看Shared SQL Area中SQL情况 V$SQLTEXT V$SQLTEXT用途很简单,就是用来查看完整的SQL语句,V$SQL和V$SQLAREA只能显示1000 byt ...

  6. oracle之 v$sql_monitor 监视正在运行的SQL语句的统计信息

    11g中引入了新的动态性能视图V$SQL_MONITOR,该视图用以显示Oracle监视的SQL语句信息.SQL监视会对那些并行执行或者消耗5秒以上cpu时间或I/O时间的SQL语句自动启动,同时在V ...

  7. 学习动态性能表(3)--v$sql&v$sql_plan

    学习动态性能表 第三篇-(1)-v$sq 2007.5.25 V$SQL中存储具体的SQL语句. 一条语句可以映射多个cursor,因为对象所指的cursor可以有不同用户(如例1).如果有多个cur ...

  8. v$sqlarea,v$sql,v$sqltext这三个视图提供的sql语句有什么区别?

    v$sqltext存储的是完整的SQL,SQL被分割 SQL> desc v$sqltextName                                      Null?    ...

  9. oracle中视图v$sql的用途

    1.获取正在执行的sql语句.sql语句的执行时间.sql语句的等待事件: select a.sql_text,b.status,b.last_call_et,b.machine,b.event,b. ...

随机推荐

  1. 初学 Spring MVC(基于 Spring in Action)

    Spring MVC(Model-View-Controller) 当你看到本博文时,我猜你可能正面临着我已探索过的问题. 同其他博主一样,我先按照书上详细的介绍一下 Spring MVC,也是为了自 ...

  2. XSS扫盲到漏洞挖掘上手

    复习xss ,也总结一下XSS基础的点到进阶的知识  目录 0x01 XSS扫盲入门 0x02 XSS payload构造 0x03 XSS payload变形进阶   0x01 XSS扫盲入门 XS ...

  3. 一文读懂Spring MVC执行流程

    说到Spring MVC执行流程,网上有很多这方面的文章介绍,但是都不太详细,作为一个初学者去读会有许多不理解的地方,今天这篇文章记录一下我学习Spring MVC的心得体会 话不多说,先上图:   ...

  4. Java NIO之Java中的IO分类

    前言 前面两篇文章(Java NIO之理解I/O模型(一).Java NIO之理解I/O模型(二))介绍了,IO的机制,以及几种IO模型的内容,还有涉及到的设计模式.这次要写一些更贴近实际一些的内容了 ...

  5. win7更新,360手机安装谷歌框架

    这两天把11平台被卸载了,不能打竞技场了,很伤心. 成年男子,总要找点有趣的事情去做.我准备洗心革面,好好学习.(巴拉巴拉巴拉一万字.) 首先第一件事情就是重装系统,(由于买了个假显卡,win10以上 ...

  6. 身为一个 CS专业的留学生,你还在为堆积如山的编程assignment而发愁吗?

    每个人都渴望圆梦,当我们看见梦想在别人身上实现时,总在抱怨幸运之神为何不眷顾自己:其实更多的时候,梦想就在你身边的不远处,只要你迈出正确的步伐. 记得刚入大学时,意气风发.我以全班前几名的优异成绩考入 ...

  7. 通过FeignClient接收shaded的javabean的JSON序列化

    问题说明 最近做了关于flink的需求. 现在需要通过HTTP访问FLINK的 RESTAPI, rest 接口的JSON 非常庞大而复杂. 那么怎么去完整的接收数据呢? 方法一就是手写部分需要的Ja ...

  8. 数据结构(四十一)多路查找树(B树)

    一.多路查找树的背景 前面所讨论的查找算法都是在内存中进行的,它们适用于较小的文件,而对于较大的.存放在外存储器上的文件就不合适了,对于此类大规模的文件,即使是采用了平衡二叉树,在查找效率上仍然较低. ...

  9. (二)Kinect关节识别

    基础:添加KinectManager 组件 1)局部关节获取(参考插件场景KinectOverlayDemo1) 要获取局部某一关节及其位置,添加脚本JointOverlayer即可,通过Tracke ...

  10. vuex状态管理安装方法

    1.可以启动vue ui 手动添加vuex. 或使用 cnpm install vuex 2.使用,import vuex from “vuex” vue.use(vuex) 3.安装插件, 首先键入 ...