Preface
 
    As we know,InnoDB is index organized table.InnoDB engine supports row-level lock base on indexes,if there're no indexes on a certain table the record locks will upgrade to "table-level"(not really table lock,just locks all the records in the table) locks.Furthe more,in RR transaction isolation mode,It's more complicated.'cause there're gap locks(together with record locks,we call them next key locks) to prevent phantom read between multiple tansactions.Let's do some test watch the locking conflicts.
 
Procedure
 
Crete a test table as below.
 zlm@192.168.56.100: [zlm]>create table t1(
-> c1 int unsigned not null default '',
-> c2 int unsigned not null default '',
-> c3 int unsigned not null default '',
-> c4 int unsigned not null default '',
-> primary key(c1),
-> key(c2)
-> ) engine=innodb;
Query OK, rows affected (0.02 sec) zlm@192.168.56.100: [zlm]>insert into t1(c1,c2,c3,c4) values(,,,),(,,,),(,,,),(,,,),(,,,),(,,,),(,,,);
Query OK, rows affected (0.01 sec)
Records: Duplicates: Warnings: zlm@192.168.56.100: [zlm]>select * from t1;
+----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
+----+----+----+----+
rows in set (0.01 sec) zlm@192.168.56.100: [(none)]>select @@transaction_isolation;
+-------------------------+
| @@transaction_isolation |
+-------------------------+
| REPEATABLE-READ | //Make surej in RR transaction isolation level.
+-------------------------+
row in set (0.00 sec) zlm@192.168.56.100: [(none)]>show variables like 'innodb_status_output_locks';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| innodb_status_output_locks | ON |
+----------------------------+-------+
row in set (0.00 sec)
Test 1. session1 executes "select ...  for update" and session2 executes "select ... lock in share mode".(conflict)
 //Session1:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c1= for update;
Query OK, rows affected (0.00 sec) +----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
+----+----+----+----+
row in set (0.00 sec) //Session2:
monitor@192.168.56.100: [zlm]>begin;select * from t1 where c1= lock in share mode;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //Session2 requested a "S" record lock on the primary key column where c1=3 while session1 has holded the "X" record lock on the same position,so session2 was blocked util lock timeout.
Test 2. session1 executes "select ...  for update" and session2 executes ordinary query.(compatible)
 //Session1:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c1= for update;
Query OK, rows affected (0.00 sec) +----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
+----+----+----+----+
row in set (0.00 sec) //Session2:
monitor@192.168.56.100: [zlm]>select * from t1 where c1=;
+----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
+----+----+----+----+
row in set (0.00 sec) //Session1 didn't change this time and session2 request for non-lock consistent read.It read records from a consistent snapshop without locking.
Test 3. session1 executes "select ...  lock in share mode" and session2 executes "select ... for update".(conflict)
 //Session1:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c3= lock in share mode;
Query OK, rows affected (0.01 sec) Empty set (0.00 sec) //Session2:
monitor@192.168.56.100: [zlm]>begin;select * from t1 where c3= for update;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //Although there's no record satisfied with c3=7 but notice that there's no index on c3 column.Therefore,the session1 has holded a "S" record for all the records on column c1 in table t1.Then session2 asked for the "X" record lock for "c3=10"(even it does not exixt),it was blocked.
Test 4. session1 executes "select ...  lock in share mode" and session2 executes "select ... for update".(conflict)
 //Session1:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c3= lock in share mode;
Query OK, rows affected (0.00 sec) Empty set (0.00 sec) //Session2:
monitor@192.168.56.100: [zlm]>begin;select * from t1 where c1= for update;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //This is similar with "test 3".Session1 has holded a "S" record lock of all records on column c3.The record where c1=6 means c3=5,it's also in the range of all records.So session2 was blocked.
Test 5. session1 executes "select ...  for update" and session2 executes "select ... for update".(conflict)
 
 //Session1:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c2= and c3= for update;
Query OK, rows affected (0.00 sec) +----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
+----+----+----+----+
row in set (0.00 sec) //Session2:
monitor@192.168.56.100: [zlm]>begin;select * from t1 where c2= and c3= for update;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //Because of the secondary index key on column c2,it generated a "X" record lock and a gap lock(record + gap = next key lock).Although the gap lock between two sessions can be coexistent,but record locks do not.So session2 was blocked.
Test 6. session1 executes "select ...  for update" and session2 executes "select ... for update".(compatible)
 //Session1:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c2= and c3= for update;
Query OK, rows affected (0.00 sec) +----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
+----+----+----+----+
row in set (0.00 sec) //Session2:
monitor@192.168.56.100: [zlm]>begin;select * from t1 where c2= and c3= for update;
Query OK, rows affected (0.00 sec) Empty set (0.00 sec) //This time session2 was not blocked.They've requested a different "X" record lock individually even they still hold the gap lock.
Test 7. session1 executes "select ...  for update" and session2 executes "select ... for update".(conflict)
 //Session1:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c2= and c3= for update;
Query OK, rows affected (0.00 sec) +----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
+----+----+----+----+
row in set (0.00 sec) //Session2:
monitor@192.168.56.100: [zlm]>begin;select * from t1 where c1= and c3= for update;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //The query condition in session2 is c1=4.It means c2=2,this is similar with test 5(asked for the equal line).
Test 8. session1 executes "select ...  for update" and session2 executes "select ... for update".(compatible)
 //Session1:
zlm@192.168.56.100: [zlm]>begin;update t1 set c4= where c2>=;
Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec)
Rows matched: Changed: Warnings: //Session2:
monitor@192.168.56.100: [zlm]>begin;select * from t1 where c1= for update;
Query OK, rows affected (0.00 sec) Empty set (0.00 sec) //The records according to the query condition c2>=4 were c1=8 and c1=10.
//Even though there's a index key on c1 but it's a primary key which doesn't generate gap lock.So session2 's asking for "X" record lock of c1=7 was not blocked.
Summary
  • We should pay more attention to innodb row-level locks.If there's no key on the relevant column,the locks will be escalated to "table-level"(all records will be locked) locks.
  • In the RR transaction isolation level,Secondary index generates gap locks(LOCK_ORDINARY) to prevent phantom read while primary index and unique index do not.They only hold record locks(LOCK_REC_NOT_GAP).
  • In the RC transaction isolation level,there're no gap locks.Therefore,it's concurrency is better than that in RR mode,but the consistency is poor as well.
  • As for which transaction isolation level we should choose is depend on your purpose:for more consistency or for more concurrency.
 

InnoDB锁冲突案例演示的更多相关文章

  1. InnoDB锁冲突案例演示(续)

      Preface       I've demontstrated several InnoDB locking cases in my previous blog.I'm gonna do the ...

  2. MySQL记录锁、间隙锁、临键锁小案例演示

    生成间隙(gap)锁.临键(next-key)锁的前提条件 是在 RR 隔离级别下. 有关Mysql记录锁.间隙(gap)锁.临键锁(next-key)锁的一些理论知识之前有写过,详细内容可以看这篇文 ...

  3. KingbaseES R6 集群主机锁冲突导致的主备切换案例

    ​ 案例说明: 主库在业务高峰期间,客户执行建表等DDL操作,主库产生"AccessExclusiveLock "锁,导致大量的事务产生锁冲突,大量的会话堆积,客户端session ...

  4. InnoDB锁机制分析

    InnoDB锁机制常常困扰大家,不同的条件下往往表现出不同的锁竞争,在实际工作中经常要分析各种锁超时.死锁的问题.本文通过不同条件下的实验,利用InnoDB系统给出的各种信息,分析了锁的工作机制.通过 ...

  5. [转载] 数据库分析手记 —— InnoDB锁机制分析

    作者:倪煜 InnoDB锁机制常常困扰大家,不同的条件下往往表现出不同的锁竞争,在实际工作中经常要分析各种锁超时.死锁的问题.本文通过不同条件下的实验,利用InnoDB系统给出的各种信息,分析了锁的工 ...

  6. MySQL- InnoDB锁机制

    InnoDB与MyISAM的最大不同有两点:一是支持事务(TRANSACTION):二是采用了行级锁.行级锁与表级锁本来就有许多不同之处,另外,事务的引入也带来了一些新问题.下面我们先介绍一点背景知识 ...

  7. MySQL InnoDB锁机制

    概述: 锁机制在程序中是最常用的机制之一,当一个程序需要多线程并行访问同一资源时,为了避免一致性问题,通常采用锁机制来处理.在数据库的操作中也有相同的问题,当两个线程同时对一条数据进行操作,为了保证数 ...

  8. 【锁】Innodb锁

    InnoDB与MyISAM的最大不同有两点:一是支持事务(TRANSACTION):二是采用了行级锁.行级锁与表级锁本来就有许多不同之处,另外,事务的引入也带来了一些新问题.下面我们先介绍一点背景知识 ...

  9. MySQL基础篇(06):事务管理,锁机制案例详解

    本文源码:GitHub·点这里 || GitEE·点这里 一.锁概念简介 1.基础描述 锁机制核心功能是用来协调多个会话中多线程并发访问相同资源时,资源的占用问题.锁机制是一个非常大的模块,贯彻MyS ...

随机推荐

  1. 使用jsonp获取天气情况

    在这里使用的是百度天气: 整体代码如下: js: <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js ...

  2. hbase添加大文件

    一直使用hbase作大容量存储,因为hbase易于存取. 今天,在录入数据的时候,突然报出一个KeyValue size too large.很是奇怪. 后来发现,该数据特别大,经查源码 privat ...

  3. Java 分支结构

    Java 分支结构 - if...else/switch 顺序结构只能顺序执行,不能进行判断和选择,因此需要分支结构. Java 有两种分支结构: if 语句 switch 语句 if 语句 一个 i ...

  4. 常用的layer弹出层

    本文来自 松耦合 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/dunegao/article/details/78978448?utm_source=copy 常用 ...

  5. Java项目排查cpu负载高

    背景 我负责的其中一个项目在空负载的情况下,CPU占用率依然保持着100%左右,线上.测试.开发的服务都一样:是什么导致的呢?在开发环境我查看了请求流量,这个流量可以忽略但CPU占用率一直在60%-1 ...

  6. Vue--- 一点车项目 连接数据库 数据使用

    Vue--- 一点车项目  连接数据库 数据使用 后台服务器 返回数据 处理 created 这个钩子在实例被创建之后被调用: async created(){ // 分类 catelist { le ...

  7. Const 关键字详解

    const 标识符 在c++中作为常量修饰符 用来修饰 函数 变量  指针 const 修饰的变量不可以改变值 const 在修饰指针的时候 const 标识符出现在*的左边表示 指向的变量为常量不能 ...

  8. Flask入门数据库的查询集与过滤器(十一)

    1 查询集 : 指数据查询的集合 原始查询集: 不经过任何过滤返回的结果为原始查询集 数据查询集: 将原始查询集经过条件的筛选最终返回的结果 查询过滤器: 过滤器 功能 cls.query.filte ...

  9. windows10上安装mysql

    环境:windwos 10(1511) 64bit.mysql 5.7.14 一.下载mysql 1. 在浏览器里打开mysql的官网http://www.mysql.com/ 2. 进入页面顶部的& ...

  10. NoSQL数据库浅析

    NoSQL(NoSQL = Not Only SQL ):非关系型的数据库.NoSQL有时也称作Not Only SQL的缩写,是对不同于传统的关系型数据库的数据库管理系统的统称. 今天我们可以通过第 ...