一、项目场景:
在学习mysql锁相关知识,自己操作时发现了一个现象:
对于命令select id from test where id in () lock in share mode;
当in中元素超过2个时,会锁住所有记录,升级成表锁。
二、问题描述
1. 问题复现
1.1. test表中数据

1.2. performance_schema.data_locks数据

1.3. 手动开启事务,并执行以下命令。

select id from test where id in (2) lock in share mode;


select id from test where id in (1,2) lock in share mode;


select id from test where id in (5,6) lock in share mode;


select id from test where id in (5,66) lock in share mode;


如上述可知,当in中元素在1或者2个时,能成功添加行锁。
1.4. 提交上述事务,再重新开启事务。


输入命令select id from test where id in (1,2,3) lock in share mode;


由上述可知,当in中元素超过2个时,会锁住所有记录,升级成表锁。
1.5. 拓展
并且执行命令
select id from test where (1,2,3) for update;
select id from test where id=1 or id=2 or id =3 lock in share mode;
也会升级成表锁
三、原因分析:
待补充= =
本文通过实验展示了在MySQL中,使用SELECT ... IN ... LOCK IN SHARE MODE命令时,当IN子句中的元素数量超过一定阈值,行锁会升级为表锁。这一现象在数据操作时需要注意,因为它可能影响并发性能和事务隔离级别。进一步探讨了相关查询语句的行为,并提出了问题待分析。
2834

被折叠的 条评论
为什么被折叠?



