[翻译]:SQL死锁-为什么会出现死锁
下面这篇对理解死锁非常重要,首先死锁是如何产生的我们要清楚。
We already know why blocking occurs in the system and howto detect and troubleshoot the blocking issues. Today I’d like us to focus on the deadlocks.First, what is the deadlock? Classic deadlock occurs when 2 processes compete for the resources and waiting on each other. Let’s take a look on that. Click on each picture below to open them in the different window. Let’s assume that Orders table has the clustered index on ID column.Let we have the session 1 that starts transaction and updates the row from the Order table. We already know that Session 1 acquires X lock on this row (ID=1) and hold it till end of transaction
我们已经知道为什么系统中会出现阻塞,也知道如何去探测这些引起阻塞的本质原因。今天我们将重点关注在死锁上。首先,什么是死锁呢?最经典的死锁出现在两个线程为了抢夺同一资源而相互竞争以至于出现你依赖我你依赖我的死循环情况。请看下图的图,我们假定在订单表Id列上有一个聚集索引,会话1开起一个事务去更新订单表的数据。此时会话1会在行数据(ID=1)上申请排它锁至于更新的事务结束。

Now let’s start session 2 and have it update another row in the Orders table. Same situation – session acquires and holds X lock on the row with (ID = 2).
现在,我们开启会话2也更新订单表,不同的是更新的数据行为Id=2,同样的在数据行Id=2上也会申请排它锁至到更新事务的结束。

Now if session 1 tries to acquire X lock on the row with ID = 2 , it cannot do that because Session 2 already held X lock there. So, Session 1 is going to be blocked till Session 2 rollback or commit the transaction
现在,如果会话1尝试在数据行Id=2上申请排它锁,将会受到阻塞,因为会话2已经申请了排它锁。只有当会话2回滚或者提交事务后才能够重新尝试获取排它锁。

It worth to mention that it would happen in any transaction isolation level except SNAPSHOT that handles such conditions differently. So read uncommitted does not help with the deadlocks at all.
这里值得提醒的时,上面的场景需要排除镜像这种特殊优化过的隔离模式。所以从上面的例子来看,read uncommitted隔离模式是不能帮忙我们解决死锁问题的。

Simple enough. Unfortunately it rarely happens in the real life. Let’s see it in the small example using the same dbo.Data table like before. Let’s start 2 sessions and update 2 separate rows in the table (acquire X locks).
足够简单,但上面的场景并不常出现。下面我们采用之前使用数据表,我们打开两个会话去更新不同的数据行。
Session 1:
会话1,更新ID=0的数据行
Session 2:
会话2,更新ID为40000的数据行

Now let’s run 2 selects.
现在我们再运行两个查询语句
Session 1:
会话1,查询Value=1的数据

Session 2:
会话2,查询Value=40001的数据

Well, as you can see, it introduces deadlock.
如下图所示,我们看到了死锁的相关提示

To understand why it happens, let’s take a look at the execution plan of the select statement:
为了理解到底发生了什么,我们看一相查询所产生的实际执行计划

As you can see – there is the table scan. So let’s think what happened
如上图所示,这是一个表扫描,我们来想一下发生了什么:
- First of all, we have 2 X locks on the different rows acquired by both sessions.
首先,我们有两个排它锁在不同的数据行上,每个会话占用一个。
- When session 1 ran select, it introduced table scan. In read committed, repeatable read or serializable isolation levels, readers issue shared S locks, so when session 1 tried to read the row with ID = 40000 (X lock held by session 2) – it was blocked.
当会话1执行查询时,它会引发表扫描,在三种悲观事务隔离级别事务中(除read uncommitted),读会产生共享锁,所有当会话1尝试去读取Id=40000的数据行时就会出现阻塞,因为会话2更新数据行的事务并未结束。
- Same thing happens with session 2 – it’s blocked on the row with ID = 1 (X lock held by session 1).
同样的情况出现在会话2中,当它尝试去读取ID=1的数据行时,此时由于会话1更新的事务并未结束,所以此次查询也会被阻塞。
So this is the classic deadlock even if there are no data updates involved. It worth to mention that read uncommitted transaction isolation level would not introduce deadlock – readers in this isolation level do not acquire S locks. Although you’ll have deadlock in the case if you change select to update even in read uncommitted level. Otimistic isolation levels also behave differently and we will talk about it later.So as you can see, in the real life as other blocking issues deadlocks happen due non-optimized queries.
这是经典的死锁场景即使此时没有任何相关的数据被更新。这里值得再次提醒的是,在read uncommitted事务隔离级别下是不能减少死锁的,尽管在此模式时并不需要申请共享锁。
注:本篇文章中一直来讲read uncommitted模式不能解决死锁,在本篇没有举例子,但之前的系列文章中已经有了,可以详细参考。
Next week we will talk how to detect and deal with deadlocks.
下一次我将讲如何探测死锁以及如何处理死锁问题。
[翻译]:SQL死锁-为什么会出现死锁的更多相关文章
- [翻译]:SQL死锁-死锁排除
As we already saw, the reasons why we have blocking issues and deadlocks in the system are pretty mu ...
- [翻译]:SQL死锁-锁的类型
很久没有写博客了,这里面的原因有很多.最近的一个项目由于客户明确提出要做下性能压力测试,使用的工具就是VS自带的压力测试工具.以前其它项目做压力测试后反馈的其中一个重要问题就是数据库的死锁.没想到我们 ...
- [翻译]:SQL死锁-阻塞探测
到了这篇,才是真正动手解决问题的时候,有了死锁之后就要分析死锁的原因,具体就是需要定位到具体的SQL语句上.那么如何发现产生死锁的问题本质呢?下面这篇讲的非常细了,还提到了不少实用的SQL,但对我个人 ...
- 30分钟全面解析-SQL事务+隔离级别+阻塞+死锁
以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化. 本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...
- SQL Server-聚焦深入理解死锁以及避免死锁建议(三十三)
前言 终于进入死锁系列,前面也提到过我一直对隔离级别和死锁以及如何避免死锁等问题模棱两可,所以才鼓起了重新学习SQL Server系列的勇气,本节我们来讲讲SQL Server中的死锁,看到许多文章都 ...
- SQL Server-聚焦深入理解死锁以及避免死锁建议(转载)
前言 终于进入死锁系列,前面也提到过我一直对隔离级别和死锁以及如何避免死锁等问题模棱两可,所以才鼓起了重新学习SQL Server系列的勇气,本节我们来讲讲SQL Server中的死锁,看到许多文章都 ...
- (4.12)全面解析-SQL事务+隔离级别+阻塞+死锁
30分钟全面解析-SQL事务+隔离级别+阻塞+死锁 转自:https://blog.csdn.net/slowlifes/article/details/52752735 2016年10月07日 23 ...
- (4.7)怎么捕获和记录SQL Server中发生的死锁?
转自:https://blog.csdn.net/c_enhui/article/details/19498327 怎么捕获和记录SQL Server中发生的死锁? 关键词:死锁记录,死锁捕获 sql ...
- (分享别人的一篇好文章,来自jackson0714)30分钟全面解析-SQL事务+隔离级别+阻塞+死锁()
30分钟全面解析-SQL事务+隔离级别+阻塞+死锁 阅读目录 概述: 一.事务 二.锁 三.阻塞 四.隔离级别 五.死锁 以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQ ...
随机推荐
- WPF之DataGrid
1.WPF 4 DataGrid 控件(基本功能篇) 基本使用,绑定数据展示 2.WPF 4 DataGrid 控件(自定义样式篇) 定义行,列,头,单元格等样式 3.WPF 4 DataGrid 控 ...
- Second Day learning English
Today I have set my Microsoft word program, use it send documents to the blog site.
- Windows Server 2008 Workstation Converter优化设置
http://www.win2008workstation.com/windows-server-2008-workstation-converter/ If you don’t want to co ...
- 购物车增加、减少商品时动画效果:jQuery.Fly.js插件使用方法
某些电商网站加入购物车和减少购物车商品数量时,有个小动画,以抛物线形式增减,如图: 这里用到了第三方jQuery.Fly.js插件(底层依赖Jquery库,地址:https://github ...
- android 隐藏标题栏的方法
1:单个activity里 onCreate() { super.onCreate(); requestWindowFeature(Window.FEATURE_NO_TITLE); setConte ...
- 移动审批App开发总结
公司新需求要在手机上进行审批. 现在开发完成了. 总结:1.初步把公司的工作流模块做成RPC服务,公共服务可以进行调用. 2.服务分层,每个App的页面对应一个服务端的接口,作为前端控制器,用来从更低 ...
- Unity 中的协同程序
今天咱就说说,协同程序coroutine.(这文章是在网吧敲的,没有unity,但是所有结论都被跑过,不管你信得过我还是信不过我,都要自己跑一下看看,同时欢迎纠错)先说说啥是协程:协同程序是一个非常让 ...
- php -- 获取当月天数及当月第一天及最后一天、上月第一天及最后一天(备忘)
Learn From :http://www.jxbh.cn/newshow.asp?id=1635&tag=2 //1.获取上个月第一天及最后一天. date('Y-m-01', strto ...
- Sass学习之路:Sass、Compass安装与命令行
导言 CSS不是一门真正意义上的编程语言,很多编程语言理所当然的特性(比如变量),都不被支持.同时再开发模块化的web项目的时候,也要避免相互干扰.为了弥补CSS的这些不足,就产生了CSS预处理器,S ...
- 使用aspose.cell动态导出多表头 EXCEL
效果图: 前台调用: using System; using System.Collections.Generic; using System.Linq; using System.Web; usin ...