mysql 开发进阶篇系列 12 锁问题(隔离级别下锁的差异)
1. innodb在不同隔离级别下的一致性读及锁的差异
不同的隔离级别下,innodb处理sql 时采用的一致性读策略和需要的锁是不同的,同时,数据恢复和复制机制的特点,也对一些sql的一致性读策略和锁策略有很大影响。对于许多sql, 隔离级别越高,innodb给记录集的锁就越严格(龙其是使用范围条件的时候),产生的锁冲突的可能性也就越高,对并发性事务处理性能的影响也就越大。因此,在应用中,应该尽量使用较低的隔离级别,减少锁争用。通常使用Read Commited隔离级别就足够了, 对于一些确实需要更高隔离级别的事务,可能在程序中执行 SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ 或SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE 动态来改变隔离级别。
下面重点看下REPEATABLE READ与Read commited 锁申请的不同区别,在增删改查上申请的锁都是一致的,但在事务中锁释放的时间是不一样的这点需要注意。
SQL |
条件 |
Read uncommited |
Read commited |
Repeatable read |
serializable |
Select
|
= |
None locks |
Consisten read/ None locks |
Consisten read/ None locks |
Share locks |
范围 |
None locks |
Consisten read/ None locks |
Consisten read/ None locks |
Share next-key |
|
Update |
= |
X(排它锁) |
X |
X |
X |
范围 |
X next-key |
X next-key |
X next-key |
X next-key |
|
Insert |
|
X |
X |
X |
X |
REPLACE |
无键冲突 |
X |
X |
X |
X |
键冲突 |
X next-key |
X next-key |
X next-key |
X next-key |
|
Delete
|
= |
X |
X |
X |
X |
范围 |
X next-key |
X next-key |
X next-key |
X next-key |
|
Select ..from Lock in share mode |
= |
Share locks |
Share locks |
Share locks |
Share locks |
范围 |
Share locks |
Share locks |
Share next-key |
Share next-key |
|
Select ..from For update |
= |
X |
X |
X |
X |
范围 |
X |
Share locks |
X next-key |
X next-key |
|
Insert into.. Select .. |
Innodb_locks_unsafe _for_binlog=off |
Share next-key |
Share next-key |
Share next-key |
Share next-key |
Innodb_locks_unsafe _for_binlog=on |
None locks |
Consisten read/ None locks |
Consisten read/ None locks |
Share next-key |
|
Create table.. Select .. |
Innodb_locks_unsafe _for_binlog=off |
Share next-key |
Share next-key |
Share next-key |
Share next-key |
Innodb_locks_unsafe _for_binlog=on |
None locks |
Consisten read/ None locks |
Consisten read/ None locks |
Share next-key |
上一篇: matlab从视频中批量截取图片
下一篇: myql简单语法测试