1. 意向锁

https://dev.mysql.com/doc/refman/5.7/en/innodb-locking.html#innodb-insert-intention-locks

官方文档,意向锁是Innodb为了支持多种粒度锁(表锁+行锁)设计的。

举例:

一个表 tab1

id, name

2, "xxxx"

id是unique key。

事务T1 select ... from tab1 where id=2 for update.

事务T2 lock tab1 .... write。

若无意向锁,事务T2需扫描表的每一行,看下是否有锁。

有了意向锁后,T2只需判断与T1的意向锁是否兼容即可。

2. Gap锁

Gap locks in InnoDB are “purely inhibitive”, which means that their only purpose is to prevent other transactions from inserting to the gap. Gap locks can co-exist. A gap lock taken by one transaction does not prevent another transaction from taking a gap lock on the same gap. There is no difference between shared and exclusive gap locks. They do not conflict with each other, and they perform the same function.

3. Next-Key Locks

是Gap锁 与 Record Locks的结合,区间为前开后闭。如一个index 有10, 11, 13, and 20几条记录,则可能存在的Next-Key Locks是:

(negative infinity, 10]

(10, 11]

(11, 13]

(13, 20]

(20, positive infinity)

Next-Key Locks用于RR事务隔离级别,解决幻读问题。

4. insert intention locks

插入意向锁是一种特殊的Gap锁,它不属于上述1. 意向锁,因为意向锁是表锁,而插入意向锁是行锁。

举例:

有一张表

mysql> CREATE TABLE child (id int(11) NOT NULL, PRIMARY KEY(id)) ENGINE=InnoDB;

mysql> INSERT INTO child (id) values (90),(102);

现在事务T1插入id=100,

事务T2插入id=101

则T1,1-获得(90,102)的gap locks, 2-获得id=100的record locks

事务T2,1-获得(90, 102)的gap locks, 2-获得id=101的record locks

两者的插入互不影响。(insert intention locks为解决并发)

但若T1

START TRANSACTION;

SELECT * FROM child WHERE id > 100 FOR UPDATE;

T1的锁包含了(100, 102)的gap锁和 id>100的X锁

此时T2

START TRANSACTION;

INSERT INTO child (id) VALUES (101);

会获得(90, 102)的Gap锁,但会等待id=101的独占锁。

最后,不同的SQL语句对应的锁如下描述:

https://dev.mysql.com/doc/refman/5.7/en/innodb-locks-set.html

注意insert 与insert on duplicate 的区别,即在检测到on duplicate后,insert加一个S锁,而insert on duplicate 在UK加一个X Next-Key locks. 在PK加一个X record locks。

以及死锁产生示例

mysql locking的更多相关文章

  1. detecting locked tables mysql (locked by LOCK TABLE)

    detecting locked tables mysql (locked by LOCK TABLE) up vote15down votefavorite 7 I would like to kn ...

  2. MySQL 之 Metadata Locking 研究

    MySQL5.5 中引入了 metadata lock. 顾名思义,metadata lock 不是为了保护表中的数据的,而是保护 database objects(元数据)的.包括表结构.schem ...

  3. 转 MYSQL SELECT ... FOR UPDATE and SELECT ... LOCK IN SHARE MODE Locking Reads

    原文: http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html In some circumstances, a consis ...

  4. 转 mysql Next-Key Locking

    原文:http://dev.mysql.com/doc/refman/5.5/en/innodb-next-key-locking.html 14.5.2.5 Avoiding the Phantom ...

  5. (笔记)MySQL 之 Metadata Locking 研究(5.5版本)

      MySQL5.5 中引入了 metadata lock. 顾名思义,metadata lock 不是为了保护表中的数据的,而是保护 database objects(元数据)的.包括表结构.sch ...

  6. MySQL 5.6 Reference Manual-14.3 InnoDB Transaction Model and Locking

    14.3 InnoDB Transaction Model and Locking 14.3.1 InnoDB Lock Modes 14.3.2 InnoDB Record, Gap, and Ne ...

  7. 转 How to Find Out Who Is Locking a Table in MySQL

    MySQL is adding more tools to monitor its internals with every new release, but one thing it still l ...

  8. Mysql加锁过程详解

    1.背景 MySQL/InnoDB的加锁分析,一直是一个比较困难的话题.我在工作过程中,经常会有同事咨询这方面的问题.同时,微博上也经常会收到MySQL锁相关的私信,让我帮助解决一些死锁的问题.本文, ...

  9. 详细介绍Mysql各种存储引擎的特性以及如何选择存储引擎

    最近业务上有要求,要实现类似oracle 的dblink   linux版本 Server version: 5.6.28-0ubuntu0.14.04.1 (Ubuntu) 修改配置文件 /etc/ ...

随机推荐

  1. Linux 基础内容

    1.linux版本有:redhat(收费),centos,ubuntu,suse(开发使用) 2./目录下的:etc配置文件目录,media挂载点,opt第三方安装目录,boot启动文件,home家, ...

  2. Linux 文件系统结构、磁盘的管理

    1.linux文件系统内没有文件的创建时间. 2.个人版RHEL8.0,RHEL9.企业版RHEL5U4,RHEL5U5. 3.cat /etc/issue查看系统版本的文件. 4.ext2无法灾难恢 ...

  3. linux下对qt编写的程序进行部署

    当我们完成程序设计之后,需要将可执行程序交付客户,而运行环境里面可能是没有相关支持库的,这个时候就涉及到部署的相关问题.对于我们在Linux下基于QT编写的图像处理程序,我们采用linuxdeploy ...

  4. Codeforces 438E The Child and Binary Tree - 生成函数 - 多项式

    题目传送门 传送点I 传送点II 传送点III 题目大意 每个点的权值$c\in {c_{1}, c_{2}, \cdots, c_{n}}$,问对于每个$1\leqslant s\leqslant ...

  5. VC++ 实现INI文件读写操作

    转载:https://blog.csdn.net/fan380485838/article/details/73188420 在实际项目开发中,会用ini配置文件,在此总结一下对ini读写操作 一:读 ...

  6. event.target.dataset

    dataset并不是典型意义上的JavaScript对象,而是个DOMStringMap对象,DOMStringMap是HTML5一种新的含有多个名-值对的交互变量. 1.event.target.d ...

  7. Lintcode40-Implement Queue by Two Stacks-Medium

    40. Implement Queue by Two Stacks As the title described, you should only use two stacks to implemen ...

  8. 模块——Getopt::Long接收客户命令行参数和Smart::Comments输出获得的命令行参数内容

     我们在linux常常用到一个程序需要加入参数,现在了解一下 perl 中的有关控制参数的模块 Getopt::Long ,比直接使用 @ARGV 的数组强大多了.我想大家知道在 Linux 中有的参 ...

  9. buntu下cutecom图像界面串口调试工具使用

    一.安装,首先下载这个软件,终端和软件中心均可下载,终端下载命令: sudo apt-get install cutecom 即可快速搞定安装问题. 软件中心: 由于我已经通过终端安装成功,所以软件中 ...

  10. Windows下命令行怎样登录MySQL

    直接cmd回车然后 “ mysql -u root -p  ”  登录时出现错误,原来是权限不够 打开cmd时需要以管理员的身份打开 然后继续使用 “ mysql -u root -p ” 还是不行, ...