1、索引失效行锁会变为表锁
数据库表a字段为int,b字段为varchar,a,b分别存在索引
update table_name set a=1 where b=40(varchar类型不加引号,索引失效)
此时执行以下语句会阻塞等待
update table_name set a=2 where b='50'
2、间隙锁
当我们用范围条件检索数据时,并请求共享或排他锁时,InnoDB会给符合条件的已有数据记录索引项加锁,对于键值在条件范围内但并不存在的记录,叫间隙(GAP),InnoDB也会对这个间隙加锁。
例如
update table_name set b='b' where a>1 and a<5;(注:数据库中无a=2的数据)
在update未提交前
执行插入a=2的数据时,该插入语句会阻塞。
insert into table_name(a,b) values(2,'bbb')
3、如何锁定一行
select * from table where a=8 for update(会锁定第8行)