尝试了解在调用存储过程时使用可重复读取隔离级别发生的死锁。
这是死锁 xml:
<TextData>
<deadlock-list>
<deadlock victim="process6fac029848">
<process-list>
<process id="process6fac029848" taskpriority="0" logused="0" waitresource="KEY: 8:72078600230076416 (d3c590b351cd)" waittime="1171"
ownerId="26116633497" transactionname="delete" lasttranstarted="2021-01-07T10:35:38.317" XDES="0x12553cbe3b0"
lockMode="U" schedulerid="3" kpid="19776" status="suspended" spid="92" sbid="0" ecid="0" priority="0" trancount="2"
lastbatchstarted="2021-01-07T10:35:38.310" lastbatchcompleted="2021-01-07T10:35:38.310" lastattention="1900-01-01T00:00:00.310"
clientapp=".Net SqlClient Data Provider" hostname="web01" hostpid="34852" loginname="someuser"
isolationlevel="repeatable read (3)" xactid="26116633497" currentdb="8" currentdbname="db1" lockTimeout="4294967295" clientoption1="673316896"
clientoption2="128056">
<executionStack>
<frame procname="db1.schema.deleteSP" line="124" stmtstart="8308" stmtend="8870"
sqlhandle="somehandle">
UPDATE [schema].[TableName] WITH(UPDLOCK)
SET
Status = 1 /* STATUS_DELETED */,
userid = @id,
UpdateTime = GETUTCDATE()
FROM @oldatt oa
WHERE [TableName].[Id] = oa.Id </frame>
</executionStack>
<inputbuf>
Proc [Database Id = 8 Object Id = 1688915952] </inputbuf>
</process>
<process id="process6fab017088" taskpriority="0" logused="284" waitresource="KEY: 8:72064801531101184 (4b819525e255)" waittime="1283"
ownerId="26116632474" transactionname="delete" lasttranstarted="2021-01-07T10:35:38.290" XDES="0x44037ec3b0"
lockMode="X" schedulerid="25" kpid="25152" status="suspended" spid="53" sbid="0" ecid="0" priority="0" trancount="2"
lastbatchstarted="2021-01-07T10:35:38.287" lastbatchcompleted="2021-01-07T10:35:38.283" lastattention="1900-01-01T00:00:00.283"
clientapp=".Net SqlClient Data Provider" hostname="web02" hostpid="57552" loginname="someuser"
isolationlevel="repeatable read (3)" xactid="26116632474" currentdb="8" currentdbname="db1" lockTimeout="4294967295" clientoption1="673316896" clientoption2="128056">
<executionStack>
<frame procname="db1.schema.deleteSP" line="124" stmtstart="8308" stmtend="8870" sqlhandle="somehandle">
UPDATE [schema].[TableName] WITH(UPDLOCK)
SET
Status = 1 /* STATUS_DELETED */,
userid = @id,
UpdateTime = GETUTCDATE()
FROM @oldatt oa
WHERE [TableName].[Id] = oa.Id </frame>
</executionStack>
<inputbuf>
Proc [Database Id = 8 Object Id = 1688915952] </inputbuf>
</process>
</process-list>
<resource-list>
<keylock hobtid="72078600230076416" dbid="8" objectname="db1.schema.TableName" indexname="IX_TableName_Id_Status_userid_UpdateTime" id="lock287c93a100" mode="U" associatedObjectId="72078600230076416">
<owner-list>
<owner id="process6fab017088" mode="U" />
</owner-list>
<waiter-list>
<waiter id="process6fac029848" mode="U" requestType="wait" />
</waiter-list>
</keylock>
<keylock hobtid="72064801531101184" dbid="8" objectname="db1.schema.TableName" indexname="IX_TableName_SomeId_Id" id="lock55fc471880" mode="S" associatedObjectId="72064801531101184">
<owner-list>
<owner id="process6fac029848" mode="S" />
</owner-list>
<waiter-list>
<waiter id="process6fab017088" mode="X" requestType="convert" />
</waiter-list>
</keylock>
</resource-list>
</deadlock>
</deadlock-list>
</TextData>
具有相关索引的创建表:
CREATE TABLE [schema].[TableName](
[Id] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[SomeId] [int] NOT NULL,
[Userid] [int] NOT NULL,
[UpdateTime] [datetime] NOT NULL,
[RecordStatus] [tinyint] NOT NULL,
CONSTRAINT [PK_Id] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
--- 2 indexes that are part of deadlock
IX_TableName_SomeId_Id = filtered Index - SomeId, Id (include status) where status = 0
IX_TableName_Id_Status_userid_UpdateTime - non clustered index - Id include(status, userid, updatetime)
SP调用是:
- 隔离级别可重复读
- 开始交易
- 从基表中进行选择并将受影响的行插入到临时变量中。
- 执行另一个 SP
- 更新基表。<<--- 这是发生死锁的地方
- 将数据与一些业务逻辑合并
- 如果没有错误,则提交事务
基表具有对审计表进行插入和更新以跟踪更改的触发器。
我只是想弄清楚为什么我会遇到太多死锁。
更新 :
declare @oldtable(
Id bigint,
someId int
PRIMARY key clustered (id)
)
insert into @oldtable (
id,
someid
)
select id, someid
from [schema].[TableName]
where id = @id and status = 0 and someid = @someid
存储过程流控制:
variables
@id
@someid
begin
set nocount on
set transaction isolation level repeatable read
set xact_abort on
begin tran delete
begin try
declare @oldtable(
Id bigint,
someId int
PRIMARY key clustered (id)
)
insert into @oldtable (
id,
someid
)
select id, someid
from [schema].[TableName]
where id = @id and status = 0 and someid = @someid
some SP runs
-- update statememt that causes deadlock
-- it basically sets status = 1 -- deleted (soft delete)
-- merge data into tables using CTE and MERGE
END TRY
begin catch
-- error handling
-- roll back tran
end catch
if @@trancount > 0
begin
commit tran delete
END
END