在并发一致性控制场景中,我们常常用for update
悲观锁来进行一致性的保证,但是如果不了解它的机制,就进行使用,很容易出现事故,比如for update
进行了锁表导致其他请求只能等待,从而拖垮系统,因此了解它的原理是非常必要的,下面我们通过一系列示例进行测试,来看看到底是什么场景下锁表什么场景下锁行
验证
示例说明
创建一个账户表,插入基础数据,以唯一索引
、普通索引
、主键
、普通字段
4 个维度进行select ... for update
查询,查看是进行锁表还是锁行
表创建
创建一个账户表,指定account_no
为唯一索引、id
为主键、user_no
为普通字段、curreny
为普通索引
create table `account_info` ( `id` int not null auto_increment comment 'id' , `account_no` int not null comment '账户编号', `user_no` varchar(32) not null comment '用户 id', `currency` varchar(10) not null comment '币种', `amount` decimal(10,2) not null comment '金额', `freeze_amount` decimal(10,2) not null comment '冻结金额', `create_time` datetime(6) not null default current_timestamp(6) comment '创建时间', `update_time` datetime(6) not null default current_timestamp(6) on update current_timestamp(6) comment '修改时间', primary key (`id`) using btree, unique key `uni_idx_account_no` (`account_no`) , key `idx_currency_` (`currency`) ) engine=innodb default charset=utf8 comment='账户信息表';
插入基础数据
insert into account_info values (1,1,'ur001','rmb',100,0,now(),now()); insert into account_info values (2,2,'ur002','rmb',1000,0,now(),now()); insert into account_info values (3,3,'ur002','dollar',200,0,now(),now());
根据主键查询
在事务 1 中,根据主键id=1 进行 for update查询时,事务2、事务 3 都进行阻塞,而事务 4 由于更新的id=2 所以成功,因此判定,根据主键进行 for update 查询时是行锁
根据唯一索引查询
在事务 1 中,根据唯一索引account_no=1 进行 for update查询时,事务2、事务 3 都进行阻塞,而事务 4 由于更新的account_no=2 所以成功,因此判定,根据唯一索引进行 for update 查询时是行锁
根据普通索引查询
在事务 1 中,根据普通索引currency='rmb' 进行 for update查询时,事务2、事务 3 都进行阻塞,而事务 4 由于更新的currency='dollar`所以成功,因此判定,根据普通索引进行 for update 查询时是行锁
根据普通字段查询
在事务 1 中,根据普通字段user_no='ur001' 进行 for update查询时,事务2、事务 3 都进行阻塞,而事务 4查询的是user_no='ur002'也进行阻塞,因此判定,根据普通字段进行 for update 查询时是表锁
总结
如果查询条件是索引/主键字段,那么select ..... for update
会进行行锁
如果查询条件是普通字段(没有索引/主键),那么select ..... for update
会进行锁表,这点一定要注意。
扫描下面的二维码关注我们的微信公众帐号,在微信公众号中共同学习。
到此这篇关于mysql for update是锁表还是锁行实例详解的文章就介绍到这了,更多相关mysql for update内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论