MySQL Lock - MySQL INSERT locked learning

Prepare the test data:

## 开启InnoDB Monitor
SET GLOBAL innodb_status_output=ON;
SET GLOBAL innodb_status_output_locks=ON;

## 创建测试表
DROP TABLE IF EXISTS tb1001;

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 the DEFAULT the CHARSET = UTF8; 

## test data preparing 
the INSERT  the INTO tb1001 (order_id, order_num, ORDER_TYPE)
 the VALUES ( 10 , 10 , 10 ), ( 20 , 10 , 20 ), ( 21 , 10 , 20 ), ( 30 , 10 , 30 ), ( 40 , 10 , 40 ); 


## to view the current data table
SELECT * FROM tb1001;
+----------+-----------+------------+
| order_id | order_num | order_type |
+----------+-----------+------------+
|       10 |        10 |         10 |
|       20 |        10 |         20 |
|       21 |        10 |         20 |
|       30 |        10 |         30 |
|       40 |        10 |         40 |
+----------+-----------+------------+

 

Test 1:

## A first implementation of the transaction but does not commit
 BEGIN ;
 INSERT  INTO tb1001 (order_id, order_num, ORDER_TYPE)
 VALUES ( 19 , 20 , 10 )

After the above operation is performed, using the SHOW ENGINE INNODB STATUS view the lock information

---TRANSACTION 1454597, ACTIVE 353 sec
2 lock struct(s), heap size 1136, 1 row lock(s), undo log entries 1
MySQL thread id 83, OS thread handle 140361075443456, query id 293 127.0.0.1 admin
TABLE LOCK table `db001`.`tb1001` trx id 1454597 lock mode IX
RECORD LOCKS space id 29 page no 3 n bits 80 index PRIMARY of table `db001`.`tb1001` trx id 1454597 lock_mode X locks rec but not gap
Record lock, heap no 7 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
 0: len 4; hex 80000013; asc     ;;
 1: len 6; hex 000000163205; asc     2 ;;
 2: len 7; hex a50000001c0110; asc        ;;
 3: len 4; hex 80000014; asc     ;;
 4: len 4; hex 8000000a; asc     ;;

The above transaction plus two locks:

1, plus the intention of modifying the table lock (IX).

2, on the newly inserted record plus line lock (RECORD LOCKS ..lock_mode X locks rec but not gap)

 

Test 2:

A ## to perform a transaction but does not commit
 the BEGIN ;
 the INSERT  the INTO tb1001 (order_id, order_num, ORDER_TYPE)
 the VALUES ( . 19 , 20 is , 10 ) 

## to perform transaction B 
## B transaction is blocked 
the BEGIN ;
 the INSERT  the INTO tb1001 (order_id, order_num , ORDER_TYPE)
 the VALUES ( . 19 , 20 is , 10 )

After the above operation is performed, using the SHOW ENGINE INNODB STATUS view the lock information

---TRANSACTION 1454599, ACTIVE 4 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 82, OS thread handle 140361075709696, query id 335 127.0.0.1 admin update
INSERT INTO tb1001(order_id,order_num,order_type)
VALUES(19,20,10)
------- TRX HAS BEEN WAITING 4 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 29 page no 3 n bits 80 index PRIMARY of table `db001`.`tb1001` trx id 1454599 lock mode S waiting
Record lock, heap no 7 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
 0: len 4; hex 80000013; asc     ;;
 1: len 6; hex 000000163205; asc     2 ;;
 2: len 7; hex a50000001c0110; asc        ;;
 3: len 4; hex 80000014; asc     ;;
 4: len 4; hex 8000000a; asc     ;;

------------------
TABLE LOCK table `db001`.`tb1001` trx id 1454599 lock mode IX
RECORD LOCKS space id 29 page no 3 n bits 80 index PRIMARY of table `db001`.`tb1001` trx id 1454599 lock mode S waiting
Record lock, heap no 7 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
 0: len 4; hex 80000013; asc     ;;
 1: len 6; hex 000000163205; asc     2 ;;
 2: len 7; hex a50000001c0110; asc        ;;
 3: len 4; hex 80000014; asc     ;;
 4: len 4; hex 8000000a; asc     ;;

---TRANSACTION 1454597, ACTIVE 590 sec
2 lock struct(s), heap size 1136, 1 row lock(s), undo log entries 1
MySQL thread id 83, OS thread handle 140361075443456, query id 293 127.0.0.1 admin
TABLE LOCK table `db001`.`tb1001` trx id 1454597 lock mode IX
RECORD LOCKS space id 29 page no 3 n bits 80 index PRIMARY of table `db001`.`tb1001` trx id 1454597 lock_mode X locks rec but not gap
Record lock, heap no 7 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
 0: len 4; hex 80000013; asc     ;;
 1: len 6; hex 000000163205; asc     2 ;;
 2: len 7; hex a50000001c0110; asc        ;;
 3: len 4; hex 80000014; asc     ;;
 4: len 4; hex 8000000a; asc     ;;

View transaction lock information:

SELECT * 
FROM `information_schema`.`INNODB_LOCKS` \G
*************************** 1. row ***************************
    lock_id: 1454606:29:3:7
lock_trx_id: 1454606
  lock_mode: S
  lock_type: RECORD
 lock_table: `db001`.`tb1001`
 lock_index: PRIMARY
 lock_space: 29
  lock_page: 3
   lock_rec: 7
  lock_data: 19
*************************** 2. row ***************************
    lock_id: 1454605:29:3:7
lock_trx_id: 1454605
  lock_mode: X
  lock_type: RECORD
 lock_table: `db001`.`tb1001`
 lock_index: PRIMARY
 lock_space: 29
  lock_page: 3
   lock_rec: 7
  lock_data: 19

Green indicates that the lock application is complete, the yellow lock that the applicant failed to be blocked.

Detailed lock:

Insert operation locking rules 
1, INSERT operations will add the newly inserted record locking row (ROW LOCK) + exclusive lock (X LOCK), and does not produce any GAP Next-Key lock locks 
2, before the insertion recording, will to insert position where the record is inserted Gap intention to apply the lock (insertion intention Gap lOCK), the intention is the same period insert Gap locks do not conflict. 
3, for a unique index, when you insert records in the table have the same key records (to be modified by other transactions and not submitted), that there is a unique key violations, we will attempt to add a read lock on the existing record, and then wait for existence. 

Locking operation: 
1. A transaction insertion, the newly inserted record lock plus line (ROW LOCK) + exclusive lock (X LOCK), and does not add any GAP lock, so lock information: the RECORD ID LOCKS the SPACE Page 29 80 n-bits. 3 the INDEX NO of a PRIMARY TABLE `db001`.`tb1001` TRX ID 1454605 lock_mode But the NOT REC X-GAP LOCKS 
	the Record the LOCK, the RECORD HEAP ANALYSIS NO. 7: n_fields. 5; the COMPACT the FORMAT; info bits 0 
	0: len. 4; the HEX 80,000,013; order_id the ASC ;; 19 = 
2, the insert operation enforcement branch B, in the presence of a unique key violations on a recording order_id = 19, and therefore to apply the recording order_id = S locks on 19, and because the hOLD a transaction record order_id = line lock (rOW lOCK) on exclusive lock 19 + (X lOCK), a transaction application B S lock is blocked, the lock information:
	
	
	RECORD LOCKS SPACE id 29 page NO 3 n bits 80 INDEX PRIMARY of TABLE `db001`.`tb1001` trx id 1454606 LOCK MODE S waiting
	Record LOCK, HEAP NO 7 PHYSICAL RECORD: n_fields 5; COMPACT FORMAT; info bits 0
	0: len 4; HEX 80000013; ASC     ;; order_id=19

 

Guess you like

Origin www.cnblogs.com/gaogao67/p/11059619.html