Dead ends for long transactions

I am using EntityFramework 6 on SQL Server 2012 and .NET 4.5.1.

During long transactions, the second user performs deadlocks. The problem is that the first user locks records for PayrollListHumanResourceID=90FA9981-AFD3-43BF-AD92-AAE5E2A42B5A, and the second wants to take data for PayrollListHumanResourceID=6CFE74C3-F180-497C-8DDA-BCA8D075FF59.

The code below shows a transaction from SQL Profiler for an Entity Framework client. For the second user, the last (sometimes the penultimate) execgoes to a standstill. For example, I removed most of the functions that work fine. I set a breakpoint for the first user right after the section DELETEand before COMMITin the C # code. The first user has a different meaning p__linq.

set quoted_identifier on
set arithabort off
set numeric_roundabort off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language us_english
set dateformat mdy
set datefirst 7
set transaction isolation level read uncommitted


begin tran;
 (...)
 exec sp_executesql N'DELETE [Extent1] FROM [dbo].[PayrollListErrors] AS [Extent1] WHERE [Extent1].[PayrollListHumanResourceID] = @p__linq__0',N'@p__linq__0 uniqueidentifier',@p__linq__0='6CFE74C3-F180-497C-8DDA-BCA8D075FF59'
 exec sp_executesql N'DELETE [Extent1] FROM [dbo].[PayrollListElementRelations] AS [Extent1] WHERE [Extent1].[PayrollListHumanResourceID] = @p__linq__0',N'@p__linq__0 uniqueidentifier',@p__linq__0='6CFE74C3-F180-497C-8DDA-BCA8D075FF59'
 exec sp_executesql N'DELETE [Extent1] FROM [dbo].[PayrollListElements] AS [Extent1] WHERE [Extent1].[PayrollListHumanResourceID] = @p__linq__0',N'@p__linq__0 uniqueidentifier',@p__linq__0='6CFE74C3-F180-497C-8DDA-BCA8D075FF59'

commit;

Below are images with table keys and indexes. PayrollListElements table

, :

  • INT ( , ),
  • GUID UNIQUEIDENTIFIER, PRIMARY KEY, ,
  • UNIQUEIDENTIFIER, (, GUID),
  • ( 2 , , ).

. . / ID - :

DELETE [Extent1] FROM [dbo].[PayrollListElements] AS [Extent1] WHERE [Extent1].ID = 30

, . , . , , index seek. , . , . FORCESEEK .

Delete execution plan

Deadlock graph.xdl

plan.sql

Trace profiler.trc

+4
1

, . , FK.

, . . , ; . , - . .

, , : , SqlException.Number == 1205. . .

- , . , .

+2

Source: https://habr.com/ru/post/1612367/


All Articles