写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. 实验吧之【拐弯抹角】(url伪静态)

    题目地址:http://ctf5.shiyanbar.com/indirection/ 打开后给了源码 <?php // code by SEC@USTC echo '<html>& ...

  2. 冰释前嫌——转入Android Studio与连接手机无法识别问题

    前言:曾有段时间被AS+gradle虽紧密结合却依然搞不定联网依赖的模样弄的头疼,尝试了各种改代理.改配置均无果,于是坚守Eclipse进行开发学习,结果一方面受制于gradle Android项目的 ...

  3. 2.Linux Bash认识

    虚拟机快照操作 1.什么是Bash shell? 它就是命令解释器,将用户输入的指令翻译给内核程序,内核处理完成之后将结果返回给Bash 2.Bash shell的用途? 几乎能完成所有的操作: 文件 ...

  4. 百万年薪python之路 -- 基础数据类型的补充练习

    1.看代码写结果 v1 = [1,2,3,4,5] v2 = [v1,v1,v1] v1.append(6) print(v1) print(v2) [1,2,3,4,5,6] [[1,2,3,4,5 ...

  5. 两行代码玩转SUMO!

    两行代码玩转SUMO! 这篇博客很简单,但是内容很丰富 如何生成如下所示的研究型路网结构? 只需要打开ubuntu终端输入如下代码即可,grid.number代表路口数量,grid.length代表路 ...

  6. (二)Kinect关节识别

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

  7. HTML5+CSS:02用户注册表单

            新的学期已开始接近两个月了,还记得是在国庆节那几天申请的博客账号,可过了一个月都还没开始写博客,(>_<)有点小偷懒了,不过,学习还是不能落下的,今写一个有点实践运用的关于 ...

  8. 前端技术之:JSON.stringfy详细说明

    JSON.stringify() 语法JSON.stringify(value[, replacer[, space]]) value 被序列化为字符串的对象 replacer 根据类型不同,其行为也 ...

  9. DOM增删改替换

    一.在创建元素的时候为什么要把创建元素到也页面写到后面?   要求:创建一个div,在div中创建10个span. var div = document.createElement("div ...

  10. 关于js中函数的一点总结

    1函数中this作用域 this根据当前环境来决定作用域,可以使用call和apply的方法来改变当前的this指向 <script> var name = "global&qu ...