MySQL insert into select锁表的问题(上)

在MySQL中使用insert into table1 select * from table2时,会对table2进行加锁,这个加锁分三种情况:

确认实验条件:

select @@global.tx_isolation,@@session.tx_isolation;
查询结果如下:
@@global.tx_isolation @@session.tx_isolation
REPEATABLE-READ REPEATABLE-READ
REPEATABLE-READ是MySQL默认的事务隔离级别

一、后面不带查询条件,不带排序方式insert into table1 select * from table2:

1、直接执行该SQL,使用show engine innodb status;查询锁情况如下

---TRANSACTION 281833671682712, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 31480, ACTIVE 5 sec inserting
mysql tables in use 2, locked 2
36 lock struct(s), heap size 3520, 1041 row lock(s), undo log entries 1008

---TRANSACTION 281833671682712, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 31480, ACTIVE 12 sec inserting
mysql tables in use 2, locked 2
856 lock struct(s), heap size 90320, 27223 row lock(s), undo log entries 26370


2、结论:此时MySQL是逐行加锁。


二、查询使用主键排序,insert into table1 select * from table2 order by id:

1、直接执行该SQL,使用show engine innodb status;查询锁情况如下

LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 281833671682712, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 31520, ACTIVE 2 sec inserting
mysql tables in use 2, locked 2
21 lock struct(s), heap size 1136, 561 row lock(s), undo log entries 543

LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 281833671682712, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 31520, ACTIVE 11 sec inserting
mysql tables in use 2, locked 2
591 lock struct(s), heap size 57552, 18780 row lock(s), undo log entries 18191


2、结论:使用主键排序时,MySQL逐行加锁。


三、使用非主键排序insert into table1 select * from table2 order by modified_date:

1、直接执行该SQL,使用show engine innodb status;查询锁情况如下

①LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 281833671682712, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 31540, ACTIVE 2 sec inserting
mysql tables in use 2, locked 2
1013 lock struct(s), heap size 90320, 32254 row lock(s), undo log entries 564

LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 281833671682712, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 31540, ACTIVE 11 sec inserting
mysql tables in use 2, locked 2
1013 lock struct(s), heap size 90320, 32254 row lock(s), undo log entries 16148

LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 281833671682712, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 31540, ACTIVE 20 sec inserting
mysql tables in use 2, locked 2
1013 lock struct(s), heap size 90320, 32254 row lock(s), undo log entries 26747  


2、结论:使用非主键排序时,MySQL锁整个表。 


四、在查询条件中使用非主键筛选条件,insert into table1 select * from table2 where modified_date>='2017-10-01 00:00:00'

1、执行该SQL,使用show engine innodb status;查询锁状况

LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 281833671682712, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 31564, ACTIVE 1 sec inserting
mysql tables in use 2, locked 2
15 lock struct(s), heap size 1136, 386 row lock(s), undo log entries 374

LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 281833671682712, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 31564, ACTIVE 9 sec inserting
mysql tables in use 2, locked 2
548 lock struct(s), heap size 57552, 17381 row lock(s), undo log entries 16836

2、结论:使用非主键筛选条件时,MySQL逐行加锁。

你可能感兴趣的:(MySQL insert into select锁表的问题(上))