---------------------------- END OF INNODB MONITOR OUTPUT ============================
打开锁监控,可以确定一下常用语句的锁信息
执行完一条SQL,使用SHOW ENGINE INNODB STATUS打印出锁信息
1 2 3 4
delete from invoice_collection_info where id=1275244823997059072
TABLE LOCK table `assist`.`invoice_collection_info` trx id 1636893 lock mode IX RECORD LOCKS space id 18491 page no 211 n bits 104 index `PRIMARY` of table `assist`.`invoice_collection_info` trx id 1636893 lock_mode X locks rec but not gap
delete语句,根据主键删除操作;可以明显看出上了表锁IX model,还有一个主键索引锁
其实如果有二级索引,还会有二级索引锁,但那是隐式锁,所以没有显示出来,后面会有试验让隐式锁显示化
使用二级索引删除操作
1 2 3 4 5 6 7 8 9 10
delete FROM invoice_item WHERE ( collection_id = 1275244823997059072 );
TABLE LOCK table `assist`.`invoice_item` trx id 1636964 lock mode IX RECORD LOCKS space id 18493 page no 4 n bits 784 index `idx_collection_id` of table `assist`.`invoice_item` trx id 1636964 lock_mode X locks rec but not gap Record lock, heap no 563 PHYSICAL RECORD: n_fields 2; compact format; info bits 0 0: len 8; hex 91b2946930001000; asc i0 ;; 1: len 8; hex 91b2ae5d61401000; asc ]a@ ;;
RECORD LOCKS space id 18493 page no 12 n bits 96 index `PRIMARY` of table `assist`.`invoice_item` trx id 1636964 lock_mode X locks rec but not gap Record lock, heap no 28 PHYSICAL RECORD: n_fields 23; compact format; info bits 0
先在二级索引上加锁,再在对应的主键索引上加锁
使用二级索引查询
1 2 3 4 5 6
select * from invoice_collection_info where invoice_uiq_flag = '031001900104-62079412' for update
TABLE LOCK table `assist`.`invoice_collection_info` trx id 1636947 lock mode IX RECORD LOCKS space id 18491 page no 642 n bits 344 index `idx_uniflag` of table `assist`.`invoice_collection_info` trx id 1636947 lock_mode X locks rec but not gap
RECORD LOCKS space id 18491 page no 496 n bits 96 index `PRIMARY` of table `assist`.`invoice_collection_info` trx id 1636947 lock_mode X locks rec but not gap
从日志中看出,先在invoice_uiq_flg二级索引上加锁,再在主键索引加锁
使用主键更新操作
1 2 3 4
update invoice_collection_info set invoice_uiq_flag = '031200190010-62079412' WHERE ( id = 1275244823997059072 ); TABLE LOCK table `assist`.`invoice_collection_info` trx id 1636958 lock mode IX RECORD LOCKS space id 18491 page no 741 n bits 88 index `PRIMARY` of table `assist`.`invoice_collection_info` trx id 1636958 lock_mode X locks rec but not gap