我的编程空间,编程开发者的网络收藏夹
学习永远不晚

MySQL的在RC和RR模式下的锁

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

MySQL的在RC和RR模式下的锁

InnoDB的锁机制:

数据库使用所是为了支持更好的并发,提供数据的完整性和一致性。InnoDB是一个支持锁的存储引擎,锁的类型有:共享锁(S)、排它锁(X)、意向共享锁(IS)、意向排它锁(IX)。为了支持更好的并发,InnoDB提供了非锁定读:不需要等待访问行上的锁释放,读取行的一个快照。该方法是通过InnoDB的一个特写:MVCC实现的。

InnoDB的锁分类:

  • Record Lock:行锁:单个行记录上的行锁

  • Gap Lock:间隙锁,锁定一个范围,但不包括记录本身

  • Next-Key Lock:Gap+Record Lock,锁定一个范围,并且锁定记录本身



  • 无索引+RC/RR

当对无索引的字段进行更新时(RR级别),通过锁主键的方式,来锁住所有记录,RC级别不会锁所有记录。

构建表及初始化数据:

mysql -uroot -p
USE test;
DROP TABLE IF EXISTS t_none;
CREATE TABLE `t_none` (
  `id` int(11) NOT NULL,
  `mem_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;
INSERT INTO t_none VALUES(1,1),(3,3),(5,5),(9,9),(11,11);

REPEATABLE-READ(RR)默认级别

Session A

Session B

root@localhost[zjkj]:10:53:18>prompt A>>

PROMPT set to 'A>>'

A>>select @@session.tx_isolation;

root@localhost[(none)]:11:02:58>prompt B>>

PROMPT set to 'B>>'

B>>select @@session.tx_isolation;

A>>begin;

Query OK, 0 rows affected (0.00 sec)

B>>begin;

Query OK, 0 rows affected (0.00 sec)

A>>select * from t_none;

+----+--------+

| id | mem_id |

+----+--------+

|  1 |      1 |

|  3 |      3 |

|  5 |      5 |

|  9 |      9 |

| 11 |     11 |

+----+--------+

5 rows in set (0.00 sec)

B>>select * from t_none;

+----+--------+

| id | mem_id |

+----+--------+

|  1 |      1 |

|  3 |      3 |

|  5 |      5 |

|  9 |      9 |

| 11 |     11 |

+----+--------+

5 rows in set (0.00 sec)


A>> select * from t_none where mem_id=3 for update;

+----+--------+

| id | mem_id |

+----+--------+

|  3 |      3 |

+----+--------+

1 row in set (0.01 sec)



B>>insert into t_none values(2,2);

ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

B>>delete from t_none where id=9;

ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

show engin inondb status部分输出:

------------

TRANSACTIONS

------------

Trx id counter 10661

Purge done for trx's n:o < 10659 undo n:o < 0 state: running but idle

History list length 351

Total number of lock structs in row lock hash table 2

LIST OF TRANSACTIONS FOR EACH SESSION:

---TRANSACTION 10588, not started

MySQL thread id 4, OS thread handle 0x7f6f5085c700, query id 339 localhost root init

show engine innodb status

---TRANSACTION 10660, ACTIVE 17 sec inserting

mysql tables in use 1, locked 1

LOCK WAIT 2 lock struct(s), heap size 376, 1 row lock(s)

MySQL thread id 11, OS thread handle 0x7f6f508de700, query id 338 localhost root update

insert into t_none values(2,2)

------- TRX HAS BEEN WAITING 17 SEC FOR THIS LOCK TO BE GRANTED:

RECORD LOCKS space id 68 page no 3 n bits 72 index `PRIMARY` of table `test`.`t_none` trx id 10660 lock_mode X locks gap before rec insert intention waiting

结论:通过上面很容易的看到,没有通过索引for update时,当进行增删改都会锁住,MySQL内部会通过基于默认主键方式对所有记录加X锁

下面是RC级别的实验


Read Committed级别(RC)

Session A

Session B

A>>set @@session.tx_isolation="read-committed";

Query OK, 0 rows affected (0.00 sec)

B>>set @@session.tx_isolation="read-committed";

Query OK, 0 rows affected (0.00 sec)

A>>select @@session.tx_isolation;

+------------------------+

| @@session.tx_isolation |

+------------------------+

| READ-COMMITTED         |

+------------------------+

1 row in set (0.00 sec)

B>>select @@session.tx_isolation;

+------------------------+

| @@session.tx_isolation |

+------------------------+

| READ-COMMITTED         |

+------------------------+

1 row in set (0.01 sec)

A>>begin;

Query OK, 0 rows affected (0.00 sec)

B>>begin;

Query OK, 0 rows affected (0.00 sec)

A>>select * from t_none where mem_id=3 for update;

+----+--------+

| id | mem_id |

+----+--------+

|  3 |      3 |

+----+--------+

1 row in set (0.01 sec)



B>>insert into t_none values(2,2);

Query OK, 1 row affected (0.01 sec)


B>>select * from t_none;

+----+--------+

| id | mem_id |

+----+--------+

|  1 |      1 |

|  2 |      2 |

|  3 |      3 |

|  5 |      5 |

|  9 |      9 |

| 11 |     11 |

+----+--------+

6 rows in set (0.00 sec

A>>rollback;

Query OK, 0 rows affected (0.00 sec)

B>>rollback;

Query OK, 0 rows affected (0.00 sec)

结论:在RC级别下,事务B是可以进行增删改(除被锁定的记录本身

  • 非唯一索引+RR/RC

  在RR级别下,InnoDB对于非唯一索引会加Gap Lock(也即锁定一个区间),而在RC级别下无。

构造初始化表及数据:

mysql -uroot -p
USE test;
DROP TABLE IF EXISTS t_idx;
CREATE TABLE `t_idx` (
  `id` int(11) NOT NULL,
  `mem_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
   KEY `idx_mem_id` (`mem_id`)
) ENGINE=InnoDB;
INSERT INTO t_idx VALUES(1,1),(3,3),(5,5),(9,9),(11,11);


REPEATABLE-READ(RR)默认级别(RR模式)

Session A

Session B

root@localhost[(none)]:06:01:59>use test;

root@localhost[zjkj]:10:53:18>prompt A>>

PROMPT set to 'A>>'

root@localhost[(none)]:06:01:59>use test;

root@localhost[(none)]:11:02:58>prompt B>>

PROMPT set to 'B>>'

A>>select @@session.tx_isolation;

+------------------------+

| @@session.tx_isolation |

+------------------------+

| REPEATABLE-READ        |

+------------------------+

1 row in set (0.00 sec)

B>>select @@session.tx_isolation;

+------------------------+

| @@session.tx_isolation |

+------------------------+

| REPEATABLE-READ        |

+------------------------+

1 row in set (0.02 sec)

A>>begin;

Query OK, 0 rows affected (0.00 sec)

B>>begin;

Query OK, 0 rows affected (0.00 sec)

A>>select * from t_idx;

+----+--------+

| id | mem_id |

+----+--------+

|  1 |      1 |

|  3 |      3 |

|  5 |      5 |

|  9 |      9 |

| 11 |     11 |

+----+--------+

5 rows in set (0.04 sec)

B>>select * from t_idx;

+----+--------+

| id | mem_id |

+----+--------+

|  1 |      1 |

|  3 |      3 |

|  5 |      5 |

|  9 |      9 |

| 11 |     11 |

+----+--------+

5 rows in set (0.00 sec)

A>>select * from t_idx where mem_id=3 for update;

+----+--------+

| id | mem_id |

+----+--------+

|  3 |      3 |

+----+--------+

1 row in set (0.05 sec)



B>>insert into t_idx values(2,2);

ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

#问题?这里为什么会出现阻塞呢?

B>>insert into t_idx values(4,4);

ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

#问题?这里为什么会出现阻塞呢?

B>>insert into t_idx values(3,3);

ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

B>>insert into t_idx values(5,5);

ERROR 1062 (23000): Duplicate entry '5' for key 'PRIMARY'

B>>insert into t_idx values(1,1);

ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

#######下面插入全部可以######

B>>insert into t_idx values(6,6);

Query OK, 1 row affected (0.00 sec)

B>>insert into t_idx values(7,7);

B>>insert into t_idx values(8,8);

Query OK, 1 row affected (0.01 sec)

B>>insert into t_idx values(12,12);

Query OK, 1 row affected (0.00 sec)


B>>select * from t_idx;

+----+--------+

| id | mem_id |

+----+--------+

|  1 |      1 |

|  3 |      3 |

|  5 |      5 |

|  6 |      6 |

|  7 |      7 |

|  8 |      8 |

|  9 |      9 |

| 11 |     11 |

| 12 |     12 |

+----+--------+

9 rows in set (0.00 sec)

show engine inondb status部分输出:

------------

TRANSACTIONS

------------

Trx id counter 11044

Purge done for trx's n:o < 11041 undo n:o < 0 state: running but idle

History list length 372

Total number of lock structs in row lock hash table 5

LIST OF TRANSACTIONS FOR EACH SESSION:

---TRANSACTION 0, not started

MySQL thread id 3, OS thread handle 0x7fd0430df700, query id 47 localhost root init

show engine innodb status

---TRANSACTION 11039, ACTIVE 228 sec inserting

mysql tables in use 1, locked 1

LOCK WAIT 3 lock struct(s), heap size 1248, 3 row lock(s), undo log entries 4

MySQL thread id 1, OS thread handle 0x7fd064099700, query id 45 localhost root update

insert into t_idx values(4,4)

Trx read view will not see trx with id >= 11040, sees < 11038

------- TRX HAS BEEN WAITING 22 SEC FOR THIS LOCK TO BE GRANTED:

RECORD LOCKS space id 70 page no 4 n bits 80 index `idx_mem_id` of table `test`.`t_idx` trx id 11039 lock_mode X locks gap before rec insert intention waitin

结论:通过上面可以看到,通过非唯一索引字段进行更新时,在进行增删改时,有的记录会出现阻塞,为什么会出现阻塞呢?其实就是用到了MySQL的间隙锁。那MySQL这里为什么要用间隙锁呢?目的主要是防止幻读。 那为什么有的记录可以插入有的不可以,因为InnoDB对于行的查询时采用了Next-Key Lock的算法,锁定的是一个范围(GAP)如下:(∞,1],(1,3],(3,5],(5,9],(9,11],(11, ∞)。InnoDB对辅助索引下一个键值也要加上Gap Lock,例如上面进行插入2、4、1、3、5时,就可以看出,其实锁住的区间是(1,5)。
Read Committed级别(RC)

Session A

Session B

A>>rollback;

Query OK, 0 rows affected (0.00 sec)

B>>rollback;

Query OK, 0 rows affected (0.00 sec)

A>>set @@session.tx_isolation="read-committed";

Query OK, 0 rows affected (0.00 sec)

B>>set @@session.tx_isolation="read-committed";

Query OK, 0 rows affected (0.00 sec)

A>>select @@session.tx_isolation;

+------------------------+

| @@session.tx_isolation |

+------------------------+

| READ-COMMITTED         |

+------------------------+

1 row in set (0.00 sec)

B>>select @@session.tx_isolation;

+------------------------+

| @@session.tx_isolation |

+------------------------+

| READ-COMMITTED         |

+------------------------+

1 row in set (0.01 sec)

A>>begin;

Query OK, 0 rows affected (0.00 sec)

B>>begin;

Query OK, 0 rows affected (0.00 sec)

A>>select * from t_idx where mem_id=3 for update;

+----+--------+

| id | mem_id |

+----+--------+

|  1 |      3 |

|  3 |      3 |

+----+--------+

2 rows in set (0.00 sec)



B>>insert into t_idx values(1,1);

ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

B>>insert into t_idx values(2,2);

Query OK, 1 row affected (0.00 sec)

B>>insert into t_idx values(3,3);

ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

B>>insert into t_idx values(4,4);

Query OK, 1 row affected (0.01 sec)

结论:在RC级别下,事务B是可以进行增删改(除被锁定的记录本身),没有出现间隙锁的现象


  • 唯一索引+RR/RC

构造初始化表及数据:

mysql -uroot –p
use test;
DROP TABLE IF EXISTS t_pk;
CREATE TABLE `t_pk` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `mem_id` int(11) NOT NULL ,
  PRIMARY KEY (`id`),
  UNIQUE  `uq_mem_id` (`mem_id`)
) ENGINE=InnoDB;
INSERT INTO t_pk VALUES(1,1),(3,3),(5,5),(9,9),(11,11);
REPEATABLE READ(RR级别)

root@localhost[(none)]:10:04:34>use test;

root@localhost[test]:10:04:41>prompt A>>

PROMPT set to 'A>>'

root@localhost[(none)]:10:04:37>use test;

root@localhost[test]:10:04:52>prompt B>>

PROMPT set to 'B>>'

A>>select @@session.tx_isolation;

+------------------------+

| @@session.tx_isolation |

+------------------------+

| REPEATABLE-READ        |

+------------------------+

1 row in set (0.01 sec)

B>>select @@session.tx_isolation;

+------------------------+

| @@session.tx_isolation |

+------------------------+

| REPEATABLE-READ        |

+------------------------+

1 row in set (0.00 sec)

A>>begin;

Query OK, 0 rows affected (0.00 sec)

B>>begin;

Query OK, 0 rows affected (0.00 sec)

A>>select * from t_pk;

+----+--------+

| id | mem_id |

+----+--------+

|  1 |      1 |

|  3 |      3 |

|  5 |      5 |

|  9 |      9 |

| 11 |     11 |

+----+--------+

5 rows in set (0.00 sec)

B>>select * from t_pk;

+----+--------+

| id | mem_id |

+----+--------+

|  1 |      1 |

|  3 |      3 |

|  5 |      5 |

|  9 |      9 |

| 11 |     11 |

+----+--------+

5 rows in set (0.00 sec)

A>>select * from t_pk where mem_id=3 for update;

+----+--------+

| id | mem_id |

+----+--------+

|  3 |      3 |

+----+--------+

1 row in set (0.00 sec)



B>>insert into t_pk values(2,2);

Query OK, 1 row affected (0.00 sec)

B>>insert into t_pk values(4,4);

Query OK, 1 row affected (0.00 sec)

B>>insert into t_pk values(3,3);

ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

B>>insert into t_pk values(5,5);

ERROR 1062 (23000): Duplicate entry '5' for key 'PRIMARY'

B>>insert into t_pk values(7,7);

Query OK, 1 row affected (0.00 sec)

结论:从这里可以看到,对于基于唯一索引的更新,MySQL只是锁定了记录本身。

同理,我们可以推导出主键也是一样的。实验的话我就略了,其实就是将上面的mem_id改成id即可。

基于主键的Record Lock,还是RR级别

A>>rollback;

Query OK, 0 rows affected (0.00 sec)

B>>rollback;

Query OK, 0 rows affected (0.00 sec)

A>>begin;

Query OK, 0 rows affected (0.00 sec

B>>begin;

Query OK, 0 rows affected (0.00 sec)

A>>select * from t_pk where id=3 for update;

+----+--------+

| id | mem_id |

+----+--------+

|  3 |      3 |

+----+--------+

1 row in set (0.00 sec)



B>>insert into t_pk values(2,2);

Query OK, 1 row affected (0.00 sec)

B>>insert into t_pk values(4,4);

Query OK, 1 row affected (0.00 sec)

结论:说明上面的推导正确。
Read-Committed级别(RC)

A>>rollback;

Query OK, 0 rows affected (0.00 sec)

B>>rollback;

Query OK, 0 rows affected (0.00 sec)

A>>set @@session.tx_isolation="read-committed";

Query OK, 0 rows affected (0.01 sec)

B>>set @@session.tx_isolation="read-committed";

Query OK, 0 rows affected (0.00 sec)

A>>select @@session.tx_isolation;

+------------------------+

| @@session.tx_isolation |

+------------------------+

| READ-COMMITTED         |

+------------------------+

1 row in set (0.00 sec)

B>>select @@session.tx_isolation;

+------------------------+

| @@session.tx_isolation |

+------------------------+

| READ-COMMITTED         |

+------------------------+

1 row in set (0.00 sec)

A>>begin;

Query OK, 0 rows affected (0.00 sec)

B>>begin;

Query OK, 0 rows affected (0.00 sec)

A>>select * from t_pk;

+----+--------+

| id | mem_id |

+----+--------+

|  1 |      1 |

|  3 |      3 |

|  5 |      5 |

|  9 |      9 |

| 11 |     11 |

+----+--------+

5 rows in set (0.00 sec)

B>>select * from t_pk;

+----+--------+

| id | mem_id |

+----+--------+

|  1 |      1 |

|  3 |      3 |

|  5 |      5 |

|  9 |      9 |

| 11 |     11 |

+----+--------+

5 rows in set (0.00 sec)

A>>select * from t_pk where mem_id=3 for update;

+----+--------+

| id | mem_id |

+----+--------+

|  3 |      3 |

+----+--------+

1 row in set (0.00 sec)



B>>insert into t_pk values(2,2);

Query OK, 1 row affected (0.00 sec)

B>>insert into t_pk values(4,4),(6,6),(10,10);

Query OK, 3 rows affected (0.00 sec)

Records: 3  Duplicates: 0  Warnings: 0

结论:说明RC级别下,没有间隙锁存在。
  • 主键+RR/RC

这跟唯一索引+RR/RC是一样的,请参看上面的唯一索引+RR/RC。






免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

MySQL的在RC和RR模式下的锁

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

mysql表级锁的模式有几种

本篇内容介绍了“mysql表级锁的模式有几种”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!1、表共享读锁,添加共享读锁的表不会阻塞其他ses
2023-06-20

RMAN 下NOARCHIVELOG和ARCHIVE模式的恢复

恢复处于NOARCHIVELOG模式的数据库当数据库处于NOARCHIVELOG模式时,如果出现介质故障 ,则最后一次备份之后对数据库所做的任何操作都将丢失。通过RMAN执行恢复时,只需要执行restore命令将数据库文件修复到正确的位置,然后就可以打开数据库
RMAN 下NOARCHIVELOG和ARCHIVE模式的恢复
2020-03-25

【Gap锁】Mysql的Gap锁在中文列下间隙怎样确定?

通过本文记录一次Gaplock的验证,网上大多gaplock是基于明确是数字型列来测试gaplock的,这里不再重复,随便贴个相关地址:https://www.cnblogs.com/crazylqy/p/7821481.html 我的疑问是如果所在的列是非数
【Gap锁】Mysql的Gap锁在中文列下间隙怎样确定?
2020-04-01

MySQL插入锁与数据库访问模式的优化

MySQL中的插入锁是一种特殊类型的锁,用于在进行INSERT操作时保护表的数据完整性。当一个线程正在向表中插入数据时,MySQL会自动获取一个插入锁,防止其他线程对同一表进行并发插入操作,从而避免数据错乱或丢失。在实际应用中,插入锁的使
MySQL插入锁与数据库访问模式的优化
2024-08-14

在 MySQL 中,如何检查表达式中不存在的模式?

MySQL NOT RLIKE 运算符可用于检查表达式中不存在的模式。 NOT RLIKE 的语法如下 -语法NOT RLIKE Pat_not_for_match这里Pat_not_for_match是不与表达式匹配的模式。示例mys
2023-10-22

android在root模式下接听来电的方法

使用了Root Tools 来招待命令。https://code.google.com/p/roottools/代码如下:private void answerRingingCall(){ try {
2022-06-06

编程热搜

目录