对mysql的各种sql语句如何对表加锁的实验
TableA
+----+------+
| c1 | c2?? |
+----+------+
|? 3 | NULL |
|? 4 | NULL |
|? 5 | NULL |
| 11 |?? 12 |
| 12 |?? 13 |
+----+------+
一 在c1上無索引,innodb_locks_unsafe_for_binlog開關(guān)被關(guān)閉。
1? 1.0 select * from ta where c1=11在read_repatable? isolation level的時候,另一個session是可以隨便插入的任何值。
?? 1.1 如果是select * from ta where c1=11 lock in share mode它是對所有的行加S lock,另外一個session任何值都是不能插入的。
2? update ta set c1=17 where c1=11在read_repatable? isolation level的時候,對表中所有行加X行鎖,另外一個session不能插入任何值。
二 當(dāng)c1上有索引的時候,非cluster的,非unique的,innodb_locks_unsafe_for_binlog開關(guān)被關(guān)閉。
1? 1.0 select * from ta where c1=11在read_repatable? isolation level的時候,另一個session是可以隨便插入的任何值。
?? 1.1 如果是select * from ta where c1=11 lock in share mode 會對c1=11這一行加S鎖,在C1<12上所有的行加上S gap鎖,
?? 即另外一個session插入c1>=12是可以的,但是插入c1<12的值不成功。
2? update ta set c1=17 where c1=11在在read_repatable? isolation level的時候,對表中c1=11這一行加上X鎖,對c1<12這個區(qū)間插入X gap鎖,
?? ?另外一個session插入c1>=12是成功的,但是插入c1<12的都是不可以的。
三 innodb_locks_unsafe_for_binlog開關(guān)被打開,index為clustered和unique的。。等等 分別去實(shí)驗(yàn),好多,不寫了。。
?
一個問題:>select * from tb; +------+------+ | c1 | c2 | +------+------+ | 3 | NULL | | 4 | NULL | | 5 | NULL | | 6 | 11 | | 11 | 12 | | 14 | 13 | | 15 | 16 | | 7 | 10 | | 8 | 10 | | 9 | 10 | +------+------+>show create table tb;| tb | CREATE TABLE `tb` (`c1` int(11) DEFAULT NULL,`c2` int(12) DEFAULT NULL,KEY `c1` (`c1`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |>set autocommit=0; >select * from tb where c1=11;經(jīng)過驗(yàn)證的結(jié)果是:lock monitor顯示的加鎖: 11加S鎖,11的下一條加了S gap鎖, 實(shí)際的操作是: 【11前的一條,11,11后的一條)不能被插入?
轉(zhuǎn)載于:https://www.cnblogs.com/jack204/archive/2012/08/20/2647962.html
總結(jié)
以上是生活随笔為你收集整理的对mysql的各种sql语句如何对表加锁的实验的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 超赞!解锁微距慢动作,挖掘手机新玩法
- 下一篇: 2021年科技体制改革实施要点3篇