T-SQL Part V: Locks
写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的更多相关文章
- Oracle 11g实时SQL监控 v$sql_monitor
Oracle 11g实时SQL监控: 前面提到,在Oracle Database 11g中,v$session视图增加了一些新的字段,这其中包括SQL_EXEC_START和SQL_EXEC_ID, ...
- 转: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 ...
- oracle 入门笔记--v$sql和v$sqlarea视图(转载)
转载于作者:dbtan 原文链接:http://www.dbtan.com/2009/12/vsql-and-vsqlarea-view.html v$sql和v$sqlarea视图: 上文提到,v$ ...
- v$sql、v$sqlarea、v$sqltext、v$sql_plan
转自:http://gldbhome.blog.51cto.com/1552935/886316 视图v$sqltext中没有SQL语句的相关统计信息,但是v$sqltext用多行来保存sql语句,而 ...
- 转:V$SQL,V$SQLAREA,V$SQLTEXT
V$SQL*表用于查看Shared SQL Area中SQL情况 V$SQLTEXT V$SQLTEXT用途很简单,就是用来查看完整的SQL语句,V$SQL和V$SQLAREA只能显示1000 byt ...
- oracle之 v$sql_monitor 监视正在运行的SQL语句的统计信息
11g中引入了新的动态性能视图V$SQL_MONITOR,该视图用以显示Oracle监视的SQL语句信息.SQL监视会对那些并行执行或者消耗5秒以上cpu时间或I/O时间的SQL语句自动启动,同时在V ...
- 学习动态性能表(3)--v$sql&v$sql_plan
学习动态性能表 第三篇-(1)-v$sq 2007.5.25 V$SQL中存储具体的SQL语句. 一条语句可以映射多个cursor,因为对象所指的cursor可以有不同用户(如例1).如果有多个cur ...
- v$sqlarea,v$sql,v$sqltext这三个视图提供的sql语句有什么区别?
v$sqltext存储的是完整的SQL,SQL被分割 SQL> desc v$sqltextName Null? ...
- oracle中视图v$sql的用途
1.获取正在执行的sql语句.sql语句的执行时间.sql语句的等待事件: select a.sql_text,b.status,b.last_call_et,b.machine,b.event,b. ...
随机推荐
- hydra暴力破解
hydra,是一个非常好用的暴力破解工具,而且名字也很cool. 下面是官网上的介绍: AFP, Cisco AAA, Cisco auth, Cisco enable, CVS, Firebird, ...
- php函数fsockopen的使用
函数说明:fsockopen — 打开一个网络连接或者一个Unix套接字连接 语法: resource fsockopen ( string $hostname [, int $port = -1 [ ...
- 【RabbitMQ 实战指南】一 RabbitMQ 开发
1.RabbitMQ 安装 RabbitMQ 的安装可以参考官方文档:https://www.rabbitmq.com/download.html 2.管理页面 rabbitmq-management ...
- 面试必问的Synchronized知道这些就可以了
Synchronized关键字算是Java的元老级锁了,一开始它撑起了Java的同步任务,其用法简单粗暴容易上手.但是有些与它相关的知识点还是需要我们开发者去深入掌握的.比如,我们都知道通过Synch ...
- 三维动画形变算法(Mixed Finite Elements)
混合有限元方法通入引入辅助变量后可以将高阶微分问题变成一系列低阶微分问题的组合.在三维网格形变问题中,我们考虑如下泛函极值问题: 其中u: Ω0 → R3是变形体的空间坐标,上述泛函极值问题对应的欧拉 ...
- ESP32 开发之旅① 走进ESP32的世界 安装开发环境
1.前言 欢迎大家来到ESP32的世界,从现在开始,笔者将带领大家慢慢揭开ESP32神秘的面纱. 在学习ESP32之前,博主希望读者能有ESP8266的学习基础(ESP32 Wifi模 ...
- python-Debug、函数装饰器
Debug操作: 程序出问题的时候可以用debug来看一下代码运行轨迹,然后找找问题在哪里 1.先给即将debug的代码打上断点: 2.打完断点之后右键点击debug: 3.然后依次点击开始按钮让 ...
- 记录一次诡异的Maven Profile不生效的问题
记录一次诡异的Maven Profile不生效的问题 现象 maven 打包之后,复制的 profile对应的resource文件总是不正确的. 即便是加了 mvn clean package -P ...
- 优化 .net core 应用的 dockerfile
优化 .net core 应用的 dockerfile Intro 在给 .net core 应用的写 dockerfile 的时候一直有个苦恼,就是如果有很多个项目,在 dockerfile 里写起 ...
- MyBatis(3)-- Mapper映射器
一.select元素 1.select元素的应用 id为Mapper的全限定名,联合称为一个唯一的标识 paremeterType标识这条SQL接收的参数类型 resultType标识这条SQL返回的 ...