由delete引起的锁扩大
阿里云月报中的一句话,出处:http://mysql.taobao.org/monthly/2022/01/01/
但是Ghost Record是可以跟正常的Record一样作为Key Range Lock的加锁对象的。可以看出这相当于把删除操作变成了更新操作,因此删除事务不在需要持有Next Key Lock
这句话意思是:假设delete语句物理删除数据,那么delete事务会持有gap lock,那么会造成锁扩大,而实际上delete操作会转为update操作,最终delete事务持有的gap lock退化为record lock,不会造成锁扩大
下面用SQL Server和MySQL做测试,看一下锁的情况
SQL Server 2012
use test
go
CREATE TABLE t (
id int NOT NULL primary key,
c int DEFAULT NULL,
d int DEFAULT NULL
)
CREATE NONCLUSTERED INDEX [ix_t_c] ON [dbo].[t]
(
[c] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
insert into t values(5,5,5),(10,10,10),(20,20,20),(25,25,25);
来源:https://www.cnblogs.com/lyhabc/p/15883562.html
图文来源于网络,如有侵权请联系删除。