------- TRX HAS BEEN WAITING 28 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 6 page no 4 n bits 80 index idx_a of table `test`.`t` trx id 637972 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 4 PHYSICAL RECORD: n_fields 2; compact format; info bits 32
0: len 4; hex 8000000b; asc ;;
1: len 6; hex 000000000414; asc ;;
这里说的很清楚啊 lock_mode X意味着是排它锁 gap代表是区间锁 也就是说在insert之前该表加入了区间排他锁,为什么呢? 因为之前执行的这句delete from t where a = 11;会在(negative infinity,11]这个区间加上排他锁,为什么是排他锁而不是Record Lock呢,因为你这里的a并非唯一索引,只是一个普通的索引,具体的看http://dev.mysql.com/doc/refman/5.7/en/innodb-locking.html
这里说的很清楚啊 lock_mode X意味着是排它锁 gap代表是区间锁
也就是说在insert之前该表加入了区间排他锁,为什么呢?
因为之前执行的这句delete from t where a = 11;会在(negative infinity,11]这个区间加上排他锁,为什么是排他锁而不是Record Lock呢,因为你这里的a并非唯一索引,只是一个普通的索引,具体的看http://dev.mysql.com/doc/refman/5.7/en/innodb-locking.html
mysql默认的会话隔离级别是repeated read,会产生更多的gap锁,如果可以接受幻读,可以考虑降为read commit级别,减少锁冲突的概率。