有数据列为如下的表
abc均无索引的情况 事务A查询语句为
select * from t_test where a=15 for update;事务B执行语句为如下
update t_test t set c=10 where a=1; --不堵塞 update t_test t set c=10 where a=15; --堵塞 update t_test t set a=15 where a=9; --不堵塞当给a列加了一个任意索引的情况,我们来执行上诉语句,发现与上诉情况一致 说明:当事务的隔离级别为READ-COMMITTED时,不管有没有索引,锁住的都是具体的数据行
abc均无索引的情况 事务A查询语句为
select * from t_test where a=15 for update;事务B执行语句为如下
update t_test t set c=10 where a=1; --堵塞 update t_test t set c=10 where c=9; --堵塞猜测此时就是锁表了,表中所有的数据都不能更改
当给a列加了一个任意索引的情况 事务A查询语句为
select * from t_test t where t.a=15 for update ;事务B执行语句为如下
update t_test t set t.a=10 where t.a=1; --堵塞 update t_test t set t.a=10 where t.a=15; --堵塞 update t_test t set t.c=10 where t.a=1; --不堵塞猜测当a有索引时,当事务的隔离级别为repeatable read时,所有尝试修改a为任意值的事务都会被堵塞。所有尝试修改a为锁定值的事务也会被堵塞。 以上限制,猜测是为了防止不可重复读,当事务尝试修改这条记录的时候就会被堵塞,通过这种方式,mysql实现了可重复读。
当事务的隔离级别为可重复读时,当where的列没有索引的时候,其锁住的就是整张表,而当列有索引的时候时候,实际上只会把对应的行记录给锁住。 当事务的隔离级别为读已提交时,当where的列不管有或没有索引的时候,其锁住的都是对应行,只是