innodb锁-插入意向锁

前言:
Insert Intention Locks译称插入意向锁,首先强调插入意向锁是间隙锁的一种,本文参考官方文档进行学习说明

数据库版本:

SELECT VERSION();
±-----------+
| version() |
±-----------+
| 5.6.34-log |
±-----------+

数据库引擎:

show variables like ‘%engine%’;
±---------------------------±-------+
| Variable_name | Value |
±---------------------------±-------+
| default_storage_engine | InnoDB |
| default_tmp_storage_engine | InnoDB |
| storage_engine | InnoDB |
±---------------------------±-------+

根据版本信息参考官方文档:

1.MySQL 5.6 Reference Manual-InnoDB Locking-Insert Intention Locks(插入意向锁)

插入意向锁

Gap Lock中存在一种插入意向锁(Insert Intention Lock),在insert操作时产生。在多事务同时写入不同数据至同一索引间隙的时候,并不需要等待其他事务完成,不会发生锁等待。
假设有一个记录索引包含键值4和7,不同的事务分别插入5和6,每个事务都会产生一个加在4-7之间的插入意向锁,获取在插入行上的排它锁,但是不会被互相锁住,因为数据行并不冲突。

An insert intention lock is a type of gap lock set by INSERT operations prior to row insertion. This lock signals the intent to insert in such a way that multiple transactions inserting into the same index gap need not wait for each other if they are not inserting at the same position within the gap. Suppose that there are index records with values of 4 and 7. Separate transactions that attempt to insert values of 5 and 6, respectively, each lock the gap between 4 and 7 with insert intention locks prior to obtaining the exclusive lock on the inserted row, but do not block each other because the rows are nonconflicting.

测试验证插入意向锁是否存在
1.创建表:

CREATE TABLE `test_user` (
  `user_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` char(10) NOT NULL,
  PRIMARY KEY (`user_id`),
  KEY `index_user` (`name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

2.插入数据:

INSERT INTO `test_user` VALUES (1,'a');
INSERT INTO `test_user` VALUES (3,'c');

事务1:

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test_user where name='a' lock in share mode;
+---------+------+
| user_id | name |
+---------+------+
|       1 | a    |
+---------+------+
1 row in set (0.00 sec)

事物2:

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into test_user(user_id,name) values(2,'b');

通过"show engine innodb status"输出innodb监控可以查看到插入意向锁的信息:

insert into test_user(user_id,name,age) values(2,'b',10)
------- TRX HAS BEEN WAITING 18 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 6628 page no 4 n bits 72 index `index_user` of table `test`.`test_user` trx id 117851203 
插入意向锁
lock_mode X insert intention waiting
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
 0: len 8; hex 73757072656d756d; asc supremum;;

个人总结:
1.插入意向锁目的是为了提高插入性能,多个事务,在同一个索引,同一个范围区间插入记录时,如果插入的位置不冲突,不会阻塞彼此,主要是不需要去申请排他锁。
2.排它锁是很强的锁,不与其他类型的锁兼容。这也很好理解,修改和删除某一行的时候,必须获得强锁,禁止这一行上的其他并发,以保障数据的一致性

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值