今天学习的是mysq45讲
主要是讲锁规则.主要有以下5条锁规则,两个原则,两个优化,一个bug
- 原则1: 如果是非唯一索引,会加next-key lock锁,这个一个前开后闭的锁,组成就是行锁加上间隙锁
select * from student where age = 10
假设student只有8岁 10岁 20岁的,那么会锁住(8,10]这个区间的next-key lock
- 原则2: 只有访问到的对象才会枷锁,如果你写出了一条覆盖索引的语句,那么当你根据id去修改值的时候,不会有任何的锁阻碍.
add index idx_age age btree
select age from student where age = 10;
update student set age = 11 where id 10
- 优化1: 如果遇到了唯一索引等值查询,next-key lock会变成行锁
select * from student where id = 10.
那么就只会锁10这一行
- 优化2: 等值查询向右遍历,找到第一个不符合条件的值,next-key lock会退化为间隙锁.
select * from student where age = 9
会变成(5,10)加上锁
- bug1 : 如果是范围查询唯一索引,那么会从左向右遍历查询到不满足要求的行为止
select * from student where id > 10
(0,15)
网友评论