innodb_locks_unsafe_for_binlog分析
mysql数据库中默认的隔离级别为repeat-read.
innodb默认使用了next-gap算法,这种算法结合了index-row锁和gap锁。正因为这样的锁算法,innodb在可重复读这样的默认隔离级别上,可以避免幻象的产生。
innodb_locks_unsafe_for_binlog最主要的作用就是控制innodb是否对gap加锁。
注意该参数如果是enable的,则是unsafe的,此时gap不会加锁;反之,如果disable掉该参数,则gap会加锁。当然对于一些和数据完整性相关的定义,如外键和唯一索引(含主键)需要对gap进行加锁,那么innodb_locks_unsafe_for_binlog的设置并不会影响gap是否加锁。
在5.1.15的时候,innodb引入了一个概念叫做“semi-consistent”,这样会在innodb_locks_unsafe_for_binlog的状态为ennable时在一定程度上提高update并发性。
幻读(Phantom Read): 是指当用户读取某一范围的数据行时,B事务在该范围内插入了新行,当用户再读取该范围的数据行时,会发现有新的“幻影”行。InnoDB和Falcon存储引擎通 过多版本并发控制机制解决了幻读问题。
Consider the following example, beginning with this table:
CREATE TABLE t (a INT NOT NULL, b INT) ENGINE = InnoDB;
INSERT INTO t VALUES (1,2),(2,3),(3,2),(4,3),(5,2);
COMMIT;
In this case, table has no indexes, so searches and index scans use the hidden clustered index for record locking (seeSection 14.8.2.1, “Clustered and Secondary Indexes”).
Suppose that one client performs an UPDATE using these statements:
SET autocommit = 0;
UPDATE t SET b = 5 WHERE b = 3;
Suppose also that a second client performs an UPDATE by executing these statements following those of the first client:
SET autocommit = 0;
UPDATE t SET b = 4 WHERE b = 2;
As InnoDB executes each UPDATE, it first acquires an exclusive lock for each row, and then determines whether to modify it. If InnoDB does not modify the row and innodb_locks_unsafe_for_binlog is enabled, it releases the lock. Otherwise, InnoDBretains the lock until the end of the transaction. This affects transaction processing as follows.
If innodb_locks_unsafe_for_binlog is disabled, the first UPDATE acquires x-locks and does not release any of them:
x-lock(1,2); retain x-lock
x-lock(2,3); update(2,3) to (2,5); retain x-lock
x-lock(3,2); retain x-lock
x-lock(4,3); update(4,3) to (4,5); retain x-lock
x-lock(5,2); retain x-lock
The second UPDATE blocks as soon as it tries to acquire any locks (because the first update has retained locks on all rows), and does not proceed until the first UPDATE commits or rolls back:
x-lock(1,2); block and wait for first UPDATE to commit or roll back
If innodb_locks_unsafe_for_binlog is enabled, the first UPDATE acquires x-locks and releases those for rows that it does not modify:
x-lock(1,2); unlock(1,2)
x-lock(2,3); update(2,3) to (2,5); retain x-lock
x-lock(3,2); unlock(3,2)
x-lock(4,3); update(4,3) to (4,5); retain x-lock
x-lock(5,2); unlock(5,2)
For the second UPDATE, InnoDB does a “semi-consistent” read, returning the latest committed version of each row to MySQL so that MySQL can determine whether the row matches the WHERE condition of the UPDATE:
x-lock(1,2); update(1,2) to (1,4); retain x-lock x-lock(2,3); unlock(2,3) x-lock(3,2); update(3,2) to (3,4); retain x-lock x-lock(4,3); unlock(4,3) x-lock(5,2); update(5,2) to (5,4); retain x-lock
---------------------
作者:彭薄
来源:CSDN
原文:https://blog.csdn.net/cxl0921/article/details/77623439
版权声明:本文为博主原创文章,转载请附上博文链接!
innodb_locks_unsafe_for_binlog分析的更多相关文章
- InnoDB锁机制分析
InnoDB锁机制常常困扰大家,不同的条件下往往表现出不同的锁竞争,在实际工作中经常要分析各种锁超时.死锁的问题.本文通过不同条件下的实验,利用InnoDB系统给出的各种信息,分析了锁的工作机制.通过 ...
- [转载] 数据库分析手记 —— InnoDB锁机制分析
作者:倪煜 InnoDB锁机制常常困扰大家,不同的条件下往往表现出不同的锁竞争,在实际工作中经常要分析各种锁超时.死锁的问题.本文通过不同条件下的实验,利用InnoDB系统给出的各种信息,分析了锁的工 ...
- MySQL 加锁处理分析
1 背景 1 1.1 MVCC:Snapshot Read vs Current Read 2 1.2 Cluster Index:聚簇索引 3 1.3 2P ...
- MySQL 加锁处理分析 转
MySQL 加锁处理分析 转 http://hedengcheng.com/?p=771 十二 13th, 2013 发表评论 | Trackback 1 背景 1 1.1 M ...
- 转载-MySQL 加锁处理分析
MySQL 加锁处理分析 发表于 2013 年 12 月 13 日 由 hedengcheng 1 背景 1 1.1 MVCC:Snapshot Read vs Current Re ...
- <转>一个最不可思议的MySQL死锁分析
1 死锁问题背景 1 1.1 一个不可思议的死锁 1 1.1.1 初步分析 3 1.2 如何阅读死锁日志 3 2 死锁原因深入剖析 4 2.1 Delete操作的加锁逻辑 4 2.2 死锁预防策略 5 ...
- MySQL 加锁处理分析-转载
来自何登成的技术博客 1.1 MVCC:Snapshot Read vs Current Read 2 1.2 Cluster Index:聚簇索引 3 1.3 ...
- MySQL 加锁处理分析<转>
1 背景 1 1.1 MVCC:Snapshot Read vs Current Read 2 1.2 Cluster Index:聚簇索引 3 1.3 2P ...
- MySQL+InnoDB semi-consitent read原理及实现分析(转)
add by zhj: 主要讲的是在MySQL在Repeatable Read和Read Committed级别下,加锁时的不同,在Read Committed隔离级别下,只对where 中满足条件的 ...
随机推荐
- java多线程下的所的概念
锁和synchronized关键字 为了同步多线程,Java语言使用监视器(monitors),一种高级的机制来限定某一 时刻只有一个线程执行一段受监视器保护的代码.监视器的行为是通过锁来实现 ...
- 更改导入项目时weblogic运行库
1.在项目上点击右键->Properties->Server->选择weblogic的server->点击Apply按钮:2.在项目上点击右键->Properties-& ...
- 微服务之kong+consul(二)
一.kong 1.使用kong来做服务网关,目前kong使用的是0.13版本现在地址:https://docs.konghq.com/install,kong的社区版没有dashboard,可以使用k ...
- How to describe the wind sprial in computer system?
How to describe the wind sprial in computer system? 2017-02-21 刘崇军 风螺旋线 If we want get the approval ...
- Python字符串与容器
[]运算符是Python中访问容器元素和切片的运算符 []有正向和反向两种下标,正向下标从0开始,向后递增: 反向下标以-1表示最后一个元素,-2表示倒数第二个字符,向前递减. []不仅可以访问单个元 ...
- http Socket长连接
文档:http://www.cocoachina.com/ios/20160602/16572.html socket(套接字)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元,包含进行网 ...
- NavicatForOracle无法连接数据库,报错ORA-28547
因为换了新项目,要用到oracle数据库,但是用Navicat连接oracle不像连接MySql那样简单,连接的时候总是报ORA-28547,最后搜了一下解决方案发现是install client没有 ...
- 最新版本Bootstrap样式很奇怪(4.1版本)
下载最新版4.1版的Bootstrap编译好的CSS,引入的样式及其怪异. 这种情况重新下载3.3.7低版本就可以了.
- Linux常用基本命令( mkdir )
mkdir: 作用:创建目录( make directories ) 命令格式: make [option] 目录 1,创建目录, 当目录存在时,再次创建会提示文件已经存在 ghostwu@dev:~ ...
- grafana-simple-json-datasource 用于连接各种grafana不支持的数据源
https://grafana.com/plugins/grafana-simple-json-datasource 1.安装方法很简单,下载后解压放到plugins目录就好. 我的是解决版的graf ...