AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 220615
Accepted
jesijesi
jesijesi
Asked: 2018-10-20 13:40:27 +0800 CST2018-10-20 13:40:27 +0800 CST 2018-10-20 13:40:27 +0800 CST

死锁 - 由于更新导致索引内的索引页面移动

  • 772

我仍在对这个问题中提到的死锁进行故障排除。在这方面,我得到了帮助,理解了为什么 select 会持有共享锁,并且我也能够测试并确认这一点。

现在我正在查看陷入僵局的第二个 spid。该会话使用主键进行更新(WHERE BatchId = @BatchI,BatchId 是 PK)并且它需要在两个页面上使用 IX。最初我不明白为什么它只更新一行时需要锁定非聚集索引的两页。然后我意识到对该行的更新将更改该行在非聚集索引中的位置,这可能是它需要在两个页面上锁定的原因。

那是对的吗?

其次,请帮助确认我对这个僵局的理解是正确的。为简单起见,我们假设该非聚集索引中只有两个页面。第 1 页和第 2 页。这就是正在发生的事情吗 在此处输入图像描述

sql-server performance
  • 1 1 个回答
  • 487 Views

1 个回答

  • Voted
  1. Best Answer
    jesijesi
    2018-11-08T18:52:26+08:002018-11-08T18:52:26+08:00

    一些测试也是如此,死锁实际上是由非聚集索引内的页面移动引起的,该更新是由更改行在索引中的位置的更新引起的。

    在此处输入图像描述

    所以最初Session 2(update)在索引行原来所在的Page2上取出了IX锁。由于更新会更改值并进而更改索引行在索引中的位置,因此会话 2 还需要在目标页上获取另一个 IX 锁,索引行将在更新后结束。

    要了解 select 想要保留 S 锁的原因,请参阅此问题。

    回购协议

    set nocount on 
    create table deadlck ( a int , b int, c char (3), constraint PKA primary key (a))
    create index idx on deadlck(b)
    --creating a big enough table so that selects use page lock instead of row lock
    declare @a int =1
    declare @b int =57800000 
    while 1=1
    begin
    insert into deadlck select @a,@b,'111'
    if (@a%521) =520 -- to make idx index pages fit one value in each page perfectly,  not necessary, just easier to simulate the issue while testing
    set @b=@b-1
    Set @a=@a+1
    if @a=52100
    break
    end
    
    ALTER INDEX idx ON deadlck
    REBUILD WITH (FILLFACTOR = 90); -- same reason as above, i did the math with a fill factor of 90 to have the pages of idx index have one value per page and and also the pages needed to have some space so when we do the update the index entry can move to another page without have to do a page split
    
    ALTER INDEX PKA ON deadlck
    REBUILD WITH (FILLFACTOR = 90); 
    
    --session 1
    while 1=1
    begin
    begin tran
    update deadlck set b=57799999 where a=2
    rollback
    end
    
    --session 2
    while 1=1
    begin
    begin tran
    select b from deadlck with(paglock) where (b =57799999 or b=57800000)
    rollback
    end
    
    • 2

相关问题

  • 死锁的主要原因是什么,可以预防吗?

  • 如何确定是否需要或需要索引

  • 我在哪里可以找到mysql慢日志?

  • 如何优化大型数据库的 mysqldump?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve