准备测试数据:
## 创建测试表 CREATE TABLE `tb1001` ( `order_id` int(11) NOT NULL, `order_num` int(11) DEFAULT NULL, `order_type` int(11) DEFAULT NULL, PRIMARY KEY (`order_id`), KEY `idx_order_type` (`order_type`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ## 准备测试数据 insert into tb1001(order_id,order_num,order_type) values(1,10,1),(2,10,2),(3,10,1),(4,10,2);
先执行事务(事务132868):
BEGIN; update tb1001 set order_num=20 where order_type=2;
再执行事务(事务ID 132869):
BEGIN; update tb1001 set order_type=2 where order_id=3;
查看事务锁和阻塞信息
SELECT * FROM INNODB_LOCK_WAITS \G *************************** 1. row *************************** requesting_trx_id: 132869 requested_lock_id: 132869:41:4:5 blocking_trx_id: 132868 blocking_lock_id: 132868:41:4:5 select * from INNODB_LOCKS \G *************************** 1. row *************************** lock_id: 132869:41:4:5 lock_trx_id: 132869 lock_mode: X,GAP lock_type: RECORD lock_table: `db002`.`tb1001` lock_index: idx_order_type lock_space: 41 lock_page: 4 lock_rec: 5 lock_data: 2, 4 *************************** 2. row *************************** lock_id: 132868:41:4:5 lock_trx_id: 132868 lock_mode: X lock_type: RECORD lock_table: `db002`.`tb1001` lock_index: idx_order_type lock_space: 41 lock_page: 4 lock_rec: 5 lock_data: 2, 4
事务132868上锁信息:
---TRANSACTION 132868, ACTIVE 177 sec 3 lock struct(s), heap size 1136, 5 row lock(s), undo log entries 2 MySQL thread id 3, OS thread handle 140397285517056, query id 14 127.0.0.1 admin TABLE LOCK table `db002`.`tb1001` trx id 132868 lock mode IX RECORD LOCKS space id 41 page no 4 n bits 72 index idx_order_type of table `db002`.`tb1001` trx id 132868 lock_mode X Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0 0: len 8; hex 73757072656d756d; asc supremum;; Record lock, heap no 3 PHYSICAL RECORD: n_fields 2; compact format; info bits 0 0: len 4; hex 80000002; asc ;; 1: len 4; hex 80000002; asc ;; Record lock, heap no 5 PHYSICAL RECORD: n_fields 2; compact format; info bits 0 0: len 4; hex 80000002; asc ;; 1: len 4; hex 80000004; asc ;; RECORD LOCKS space id 41 page no 3 n bits 72 index PRIMARY of table `db002`.`tb1001` trx id 132868 lock_mode X locks rec but not gap Record lock, heap no 3 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 4; hex 80000002; asc ;; 1: len 6; hex 000000020704; asc ;; 2: len 7; hex 24000000230a28; asc $ # (;; 3: len 4; hex 80000014; asc ;; 4: len 4; hex 80000002; asc ;; Record lock, heap no 5 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 4; hex 80000004; asc ;; 1: len 6; hex 000000020704; asc ;; 2: len 7; hex 24000000230a49; asc $ # I;; 3: len 4; hex 80000014; asc ;; 4: len 4; hex 80000002; asc ;;
事务132869上锁信息:
---TRANSACTION 132869, ACTIVE 167 sec updating or deleting mysql tables in use 1, locked 1 LOCK WAIT 3 lock struct(s), heap size 1136, 3 row lock(s), undo log entries 1 MySQL thread id 4, OS thread handle 140397285246720, query id 20 127.0.0.1 admin updating update tb1001 set order_type=2 where order_id=3 ------- TRX HAS BEEN WAITING 6 SEC FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 41 page no 4 n bits 72 index idx_order_type of table `db002`.`tb1001` trx id 132869 lock_mode X locks gap before rec insert intention waiting Record lock, heap no 5 PHYSICAL RECORD: n_fields 2; compact format; info bits 0 0: len 4; hex 80000002; asc ;; 1: len 4; hex 80000004; asc ;; ------------------ TABLE LOCK table `db002`.`tb1001` trx id 132869 lock mode IX RECORD LOCKS space id 41 page no 3 n bits 72 index PRIMARY of table `db002`.`tb1001` trx id 132869 lock_mode X locks rec but not gap Record lock, heap no 4 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 4; hex 80000003; asc ;; 1: len 6; hex 000000020705; asc ;; 2: len 7; hex 250000002407c6; asc % $ ;; 3: len 4; hex 8000000a; asc ;; 4: len 4; hex 80000002; asc ;; RECORD LOCKS space id 41 page no 4 n bits 72 index idx_order_type of table `db002`.`tb1001` trx id 132869 lock_mode X locks gap before rec insert intention waiting Record lock, heap no 5 PHYSICAL RECORD: n_fields 2; compact format; info bits 0 0: len 4; hex 80000002; asc ;; 1: len 4; hex 80000004; asc ;;