Shard Lock: 共享锁 官方: permits the transaction that holds the locktoread a row. eg. select * fromtablewhereid=1lockinsharemode;
Exclusive Locks: 排他锁 官方: permits the transaction that holds the locktoupdateordelete a row eg. select * fromtablewhereid=1forupdate;
Intention Locks
1 2 3 4 5
锁是加在table上的、表示要对下一个层级(记录)加锁 Intention shared(IS): Transaction T intends toset S locks on individual rowsintable t Intention exclusive(IX): Transaction T intends toset X lockon those rows 在db层看到的是: TableLocktable `db`.`table` trx_id 12121212lock mode IX
Record Locks
1 2 3 4 5 6
在db层看到的是: Record Locks space id in 281 page no 3 n bits 72 indexPRIMARY of table `db`.`table` trx id 12121212 lock_mode X rec but not gap 锁是加在索引上的(从 indexprimary of table `db`.`table` ) 可以看出 记录锁有两种类型: 1. lock_mode X locks rec but not gap 2. lock_mode S locks rect bot not gap
Gap Locks
1 2 3 4
在db层看到的是: Record Locks space id 281 pages no 5 n bits 72 index idx_c of table `lc_3`.`a` trx id 133588125 lock_mode X locks gap before rec gap锁是用来防止insert的 锁的不是记录、而是记录之间的间隙、eg. (10,20)
Insert intention Locks
1 2
在db层看到的是: Record Locks space id 279 page no 3 n bits 72 index primary of table `lc_3`.`t1` trx id 133587907 lock_mode X insert intention waiting
next-Key locks
1 2 3
在db看到的是: Record Locks space id 281 page no 5 n bits index idx_c of table `lc_3`.`t1` trx id 133587907 lock_mode X Next-Key Locks = Gap Locks + Record Locks会同时锁住记录和间隙
Anto-inc Locks
1 2 3
在db层看到的结果是: Table Locktable xx trx id7498948lockmodeauto-inc waiting 属于表级别的锁 http://keithlan.github.io/2017/03/03/auto_increment_lock/
显式锁 vs 隐式锁
1 2 3 4 5 6 7 8 9 10 11 12
显式锁(explicit lock) 显式加的锁, 在 show engine innoDB status 中能够看到、会在内存中产生对象、占用内存 eg. select ... for update, select ... lock in share mode ....
隐式锁(implicit lock) 是在索引中对记录逻辑的加锁、但实际上不产生锁对象、不占用内存空间 eg. insert into xx values(xx) update xx set t=t+1where id=1;