MySQL--REPLACE INTO加锁测试02

测试场景

  • MySQL版本: 5.7.29
  • 事务级别: READ-COMMITTED

测试数据


DROP TABLE IF EXISTS tb1001;
CREATE TABLE `tb1001` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `c1` INT(11) NOT NULL,
  `c2` INT(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `UNI_C1` (`c1`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO TB1001(id,C1,C2)SELECT 1,1,1;
INSERT INTO TB1001(id,C1,C2)SELECT 3,3,3;
INSERT INTO TB1001(id,C1,C2)SELECT 5,5,5;
INSERT INTO TB1001(id,C1,C2)SELECT 7,7,7;

测试操作

会话1先执行:

BEGIN;
REPLACE INTO tb1001(id,c1,c2)VALUES(3,3,33);
Query OK, 2 rows affected (0.00 sec)

测试01

会话2执行(被阻塞):

SELECT C1 FROM TB1001 WHERE C1=3 FOR UPDATE;

测试02

会话2执行(被阻塞):

SELECT C1 FROM TB1001 WHERE C1=5 FOR UPDATE;

测试03

会话2执行(被阻塞):

INSERT INTO TB1001(id,C1,C2)SELECT 4,4,4;

测试04

会话2执行(执行成功):

INSERT INTO TB1001(id,C1,C2)SELECT 8,8,8;

测试结论

对于REPLACE INTO语句,当主键和唯一键都相同时,仍会触发DELETE+INSERT操作(2 rows affected),加锁与未指定自增主键的方式相同。

参考:MySQL--REPLACE INTO加锁测试01

posted @ 2021-04-08 21:50  TeyGao  阅读(153)  评论(0编辑  收藏  举报