帮忙解决一个锁的问题 | mysql | mysql 技术论坛-江南app体育官方入口

现在有个accounts表,ddl为

create table `accounts`
(
    `account_id`     int(11) not null auto_increment,
    `account_number` varchar(20) collate utf8mb4_unicode_ci default null,
    `mt4_server_id`  int(11)                                default null,
    `balance`        decimal(10, 2)                         default null,
    `update_date`    date                                   default null,
    primary key (`account_id`),
    key `idx_account_number_mt4_server_id_account_id` (`account_number`, `mt4_server_id`, `account_id`)
) engine = innodb
  auto_increment = 6
  default charset = utf8mb4
  collate = utf8mb4_unicode_ci;

然后对下面的两个update语句进行测试

// update1
update accounts set balance = 0 where account_number not in ('115') and mt4_server_id = 14 order by account_id;
// update2
update accounts set balance = 0 where account_number in ('115') order by account_id;

测试1 事务隔离级别为 repeatable read:

  • update1执行期间,update2会遇到锁!

测试2 事务隔离级别为 read committed:

  • update1执行期间,update2能够执行成功,因为115所在行没有被上锁。当然update2的115换成其他114或者其他会呗上锁。
  • 进一步测试将idx_account_number_mt4_server_id_account_id的index删除掉,再次执行,update2执行失败,因为115行也被上锁了

我的问题是

当前生产环境的mysql隔离级别是repeatable read,在不更改隔离级别的前提下。是否能够实现以下需求。

  • update1更新期间,udpate2也能被成功执行
    也就是说update1执行的时候不要将115的所在行上锁!进而成功执行update2操作!如果可能那么我该如何实现,如果不可能那就算了吧!

问题很简单,有兴趣的朋友也可以在本地试一试!

最佳答案

该问题已解决! 根据需要调整index。使用explain语句调整。尽量只锁定更新的行。

5个月前
讨论数量: 7

我觉得你需要检查一下你的 mysql 版本。

我查找到的资料:

6个月前
(楼主) 6个月前

用 explan 看下用了什么索引

我觉得是触发了区间锁。

可以尝试使用


// update1
update accounts set balance = 0 
where account_id in (select account_id from accounts where account_number not in ('115') and mt4_server_id = 14 ) 
order by account_id;
copy
// update2
update accounts set balance = 0 
where account_id in (select account_id from accounts where account_number in ('115') )
order by account_id;

所有数据都建议用 id 来更新

6个月前
(楼主) 6个月前

你这就是我之前写的一篇文章deadlock, 解除死锁的关键就在于不能并发影响同一行的数据 rc 虽然能缓解,但是还是存在数据风险

6个月前
(楼主) 5个月前

该问题已解决! 根据需要调整index。使用explain语句调整。尽量只锁定更新的行。

5个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
网站地图