MySQL事务隔离和加锁分析

MVCC(多版本并发控制)

  • 当前读 (加锁)
  • 快照读 (不加锁)

快照读

  • 
    select * from users where id=1;
    
    

当前读


select * from users where ? lock in share mode;


select * from users where ? for update;


insert into users values (…);


update users set ? where ?;


delete from users where ?;

事务隔离

  • read uncommitted(读取未提交数据)
  • read committed(可以读取其他事务提交的数据)
  • repeatable read(可重读)
  • serializable(串行化)

加锁分析


select * from users where id = 1;
delete from users where id = 1;

  • id列是主键,RC隔离级别
  • id列是二级唯一索引,RC隔离级别
  • id列是二级非唯一索引,RC隔离级别
  • id列上没有索引,RC隔离级别
  • id列是主键,RR隔离级别
  • id列是二级唯一索引,RR隔离级别
  • id列是二级非唯一索引,RR隔离级别
  • id列上没有索引,RR隔离级别
  • Serializable隔离级别

id主键+RC 和 id主键+RR

id唯一索引+RC 和 id唯一索引+RR

id非唯一索引+RC

id无索引+RC

id非唯一索引+RR

id无索引+RR

谢谢大家