出现这个问题主要是 两个事务同时 update 同一个表导致;
例如:
1,用Navicat 打开两个命令窗口
2,在第一个命令窗口输入:
begin;
update `test1` a set a.`stus`='2',a.`crt_tm`=now(),a.`upd_tm`=now() where id in (50);
3,在第二个窗口输入:
update `test1` a set a.type='1' where a.`stus`='2';
就会报错:
Lock wait timeout exceeded; try restarting transaction
此时 没有索引 如果给 stus加上索引 并且 id是50的这条数据的stus不是2 就可以update成功;
否则会报同样的错误;
所以 不加索引是锁表,加了索引是锁行;