MySQL DDL锁表情况分析
admin
2023-04-19 12:03:16
0

版本5.7.22,隔离级别RR

当DDL的表存在慢查询时,此时对该表做DDL,由于无法获得metadata锁,所以会等待该锁,造成锁表,后续DML操作全部进入等待状态。
session1:
MySQL DDL锁表情况分析
session2:
MySQL DDL锁表情况分析
session3:
MySQL DDL锁表情况分析
session4:
MySQL DDL锁表情况分析

MySQL DDL锁表情况分析

tips:
select sleep(N) from t;表示查询t的时间为t中的行数*N,如下:
MySQL DDL锁表情况分析

Before an online DDL operation can finish, it must wait for transactions that hold metadata locks on the table to commit or roll back. An online DDL operation may briefly require an exclusive metadata lock on the table during its execution phase, and always requires one in the final phase of the operation when updating the table definition. Consequently, transactions holding metadata locks on the table can cause an online DDL operation to block. The transactions that hold metadata locks on the table may have been started before or during the online DDL operation. A long running or inactive transaction that holds a metadata lock on the table can cause an online DDL operation to timeout.
在线DDL操作完成之前,必须等待持有表上的元数据锁的事务提交或回滚。在线DDL操作在执行阶段可能会短暂地需要表上的独占元数据锁,并且在更新表定义时总是在操作的最后阶段需要一个锁。因此,持有表上的元数据锁的事务可能导致在线DDL操作阻塞。表上持有元数据锁的事务可能在DDL在线操作之前或期间启动。在表上持有元数据锁的长时间运行或不活动的事务可能导致在线DDL操作超时。
https://dev.mysql.com/doc/refman/5.6/en/innodb-online-ddl-limitations.html
Online DDL and Metadata Locks
Online DDL operations can be viewed as having three phases:    DDL的在线操作可以分为三个阶段:

  • Phase 1: Initialization  初始化
    In the initialization phase, the server determines how much concurrency is permitted during the operation, taking into account storage engine capabilities, operations specified in the statement, and user-specified ALGORITHM and LOCK options. During this phase, a shared upgradeable metadata lock is taken to protect the current table definition.
    在初始化阶段,服务器考虑存储引擎功能、语句中指定的操作以及用户指定的算法和锁选项,确定操作期间允许多少并发性。在此阶段,使用共享的可升级元数据锁来保护当前表定义。

  • Phase 2: Execution
    In this phase, the statement is prepared and executed. Whether the metadata lock is upgraded to exclusive depends on the factors assessed in the initialization phase. If an exclusive metadata lock is required, it is only taken briefly during statement preparation.
    在这个阶段,语句被准备好并执行。元数据锁是否升级为exclusive取决于初始化阶段评估的因素。如果需要独占元数据锁,则只在语句准备期间进行短暂的锁定。

  • Phase 3: Commit Table Definition 提交表定义
    In the commit table definition phase, the metadata lock is upgraded to exclusive to evict the old table definition and commit the new one. Once granted, the duration of the exclusive metadata lock is brief.
    在提交表定义阶段,将元数据锁升级为exclusive,以删除旧表定义并提交新表定义。一旦获得授权,独占元数据锁的持续时间很短。
    Due to the exclusive metadata lock requirements outlined above, an online DDL operation may have to wait for concurrent transactions that hold metadata locks on the table to commit or rollback. Transactions started before or during the DDL operation can hold metadata locks on the table being altered. In the case of a long running or inactive transaction, an online DDL operation can time out waiting for an exclusive metadata lock. Additionally, a pending exclusive metadata lock requested by an online DDL operation blocks subsequent transactions on the table.
    由于上面列出的独占元数据锁需求,在线DDL操作可能必须等待持有表上的元数据锁的并发事务提交或回滚。DDL操作之前或期间启动的事务可以在被修改的表上持有元数据锁。在长时间运行或不活动的事务的情况下,在线DDL操作可能会超时,等待独占元数据锁。此外,在线DDL操作请求的挂起的独占元数据锁会阻塞表上的后续事务。

相关内容

热门资讯

终于懂了“新九天牛牛可以开挂吗... 终于懂了“新九天牛牛可以开挂吗?”(太坑了果然有挂)您好,新九天牛牛这个游戏其实有挂的,确实是有挂的...
今日重大发现“新起点开挂神器?... 家人们!今天小编来为大家解答新起点透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里买很多...
【第一财经】“新皇豪炸/金/花... 网上科普关于“新皇豪炸/金/花有没有挂”话题很是火热,小编也是针对新皇豪炸/金/花作*弊开挂的方法以...
终于明白“广西老友玩到底是不是... 家人们!今天小编来为大家解答广西老友玩透视挂怎么安装这个问题咨询软件客服徽9784099的挂在哪里买...
今日重大发现“网易棋牌开挂器?... 您好:网易棋牌这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这款游戏...
终于了解“人海牛牛可以开挂吗?... 有 亲,根据资深记者爆料人海牛牛是可以开挂的,确实有挂(咨询软件无需打开...
【第一财经】“蛮王炸/金/花开... 有 亲,根据资深记者爆料蛮王炸/金/花是可以开挂的,确实有挂(咨询软件无...
玩家攻略科普“友谊互娱辅助器?... 家人们!今天小编来为大家解答友谊互娱透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪里买很...
年末炸场!荣耀WIN系列硬核实... 2025年末的手机市场,被荣耀WIN系列的首秀彻底点燃。这款从GT系列全面升级而来的电竞旗舰,以“不...
【第一财经】“白金岛三打哈辅助... 【第一财经】“白金岛三打哈辅助器?”(必胜开挂神器)您好,白金岛三打哈这个游戏其实有挂的,确实是有挂...