MySQL insert into select锁表的问题(上)
2017-12-02 18:09
91 查看
在MySQL中使用insert into table1 select * from table2时,会对table2进行加锁,这个加锁分三种情况:
确认实验条件:
一、后面不带查询条件,不带排序方式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逐行加锁。
确认实验条件:
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逐行加锁。
相关文章推荐
- 编译安装php容易出现的问题以及解决办法
- 项目中应用eventbus解决的问题
- 方法和变量在继承时的覆盖和隐藏问题
- 40个Java多线程问题总结
- Android开发中常见的5大内存泄漏问题及解决办法
- 解决问题:ORA-01846--周中的日无效
- 20160917iOS音频开发之资源(AVAsset)与元数据,解决获取资源属性问题(二)
- JSP中乱码问题
- 单源最短路问题 Bellman-Ford算法
- 【转载】应用程序脱机app_offline.html的问题
- LINUX SSH客户端的中文乱码问题
- 路径分析返回信息是英文的问题
- 求比指定数大且最小的“不重复数”问题
- hadoop scheduler查看问题
- 权限模块-修改操作存在问题
- Response.ContentType 控制输出文件类型(讨论下载文件问题)
- Spark生态之Alluxio学习23--alluxio-0.7.1解决数据本地化的问题
- 在table中tr的display:block在firefox下显示布局错乱问题
- SpringMVC 学习-返回字符串中文乱码问题解决
- Raspberry Pi3 常见问题