Hai bisogno di aiuto per la risoluzione dei problemi dello scenario deadlock di SQL Server 2005


9

Sto correndo in uno scenario di deadlock in cui gli unici partecipanti al deadlock sembrano essere una singola tabella e un'unica procedura memorizzata che viene eliminata da quella tabella. Ho tratto questa conclusione sulla base della mia analisi del log degli errori sql al momento di molti di questi deadlock, usando l'articolo MSDN di seguito come linea guida per decifrare la traccia nel log degli errori.

La tabella DEXTable e la stored procedure ClearDEXTableRows sono definite di seguito. Esiste un'altra procedura memorizzata InsertDEXTableRow che inserisce le righe in DEXTable, ma che proc non sembra essere coinvolto nel deadlock in base alle voci nel log degli errori sql.

La tabella DEXTable contiene ~ 8,3 milioni di righe e tende a crescere costantemente. Anche la tabella degli intervistati è grande e tende a crescere costantemente.

Vi si accede da un sito Web ad alto traffico con pagine che spesso chiamano ClearDEXTableRows e InsertDEXTableRow in rapida successione.

Il deadlock si è verificato tra 0 e 9 volte al giorno negli ultimi 10 giorni.

Ho abilitato sql trace per 1222 (usando DBCC TRACEON 1222) e di recente ho abilitato il flag 1204. C'è una buona descrizione dell'output per questi flag su Rilevamento e chiusura di deadlock

Le mie domande sono:

Ha senso che solo questa procedura memorizzata ClearDEXTableRows sia la causa del deadlock?

In tal caso , qualcuno può offrire una buona spiegazione di come ciò può accadere e consigliare un modo per risolverlo?
Il mio sospetto è che le dichiarazioni DELETE stiano causando contese sul PK per DEXTable che deve essere ricostruito frequentemente.

In caso contrario , quale ulteriore traccia dovrei abilitare per scavare più a fondo nella causa del deadlock? (Voglio imparare qui)

-- Table definition
CREATE TABLE [dbo].[DEXTable](
                [ExportID] [int] NOT NULL,
                [RespondentID] [int] NOT NULL,
                [Exported] [datetime] NOT NULL,
CONSTRAINT [PK_DEXTable] PRIMARY KEY CLUSTERED
(
                [ExportID] ASC,
                [RespondentID] 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

ALTER TABLE [dbo].[DEXTable]  WITH CHECK ADD  CONSTRAINT [FK_DEXTable_Exports] FOREIGN KEY([ExportID])
REFERENCES [dbo].[Exports] ([ExportID])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[DEXTable] CHECK CONSTRAINT [FK_DEXTable_Exports]
GO

ALTER TABLE [dbo].[DEXTable]  WITH CHECK ADD  CONSTRAINT [FK_DEXTable_Respondents] FOREIGN KEY([RespondentID])
REFERENCES [dbo].[Respondents] ([RespondentID])
GO

ALTER TABLE [dbo].[DEXTable] CHECK CONSTRAINT [FK_DEXTable_Respondents]
GO

ALTER TABLE [dbo].[DEXTable] ADD  DEFAULT (getdate()) FOR [Exported]
GO

-- "ClearDEXTableRows"
-- Clear a respondent's export records to trigger re-export.
CREATE PROCEDURE [dbo].[ClearDEXTableRows]
    @RespondentID int
AS
    DELETE DEXTable WITH (ROWLOCK)
    WHERE RespondentID = @RespondentID
GO

-- "InsertDEXTableRow"
-- Insert record noting export run for a particular respondent.
CREATE PROCEDURE [dbo].[InsertDEXTableRow]
    @ExportID int,
    @RespondentID int
AS
    IF NOT EXISTS (SELECT RespondentID FROM DEXTable WHERE ExportID = @ExportID AND RespondentID = @RespondentID)
    BEGIN
      INSERT DEXTable
        (ExportID, RespondentID, Exported)
      VALUES
        (@ExportID, @RespondentID, getdate())
    END
    ELSE
    BEGIN
      UPDATE DEXTable
      SET Exported = getdate()
      WHERE ExportID = @ExportID AND RespondentID = @RespondentID
    END
GO

E qui ci sono alcune voci del registro (non sono del tutto sicuro di cosa sia utile)

-- Sql error log for one of the recent deadlocks
-- Most recent entries in the log are at the top and go further back in time as you read down

11/17/2011 00:00:58,spid18s,Unknown,This instance of SQL Server has been using a process ID of 1840 since 10/31/2011 7:19:43 PM (local) 11/1/2011 12:19:43 AM (UTC). This is an informational message only; no user action is required.

11/16/2011 20:37:59,spid20s,Unknown,waiter id=process86a6478 mode=U requestType=wait

11/16/2011 20:37:59,spid20s,Unknown,waiter-list
11/16/2011 20:37:59,spid20s,Unknown,owner id=processac352e9b8 mode=U
11/16/2011 20:37:59,spid20s,Unknown,owner-list
11/16/2011 20:37:59,spid20s,Unknown,keylock hobtid=72057594060931072 dbid=5 objectname=MyServer_Database.dbo.DEXTable indexname=PK_DEXTable id=lockd32579f80 mode=U associatedObjectId=72057594060931072
11/16/2011 20:37:59,spid20s,Unknown,waiter id=process47eda8 mode=U requestType=wait
11/16/2011 20:37:59,spid20s,Unknown,waiter-list
11/16/2011 20:37:59,spid20s,Unknown,owner id=processac352e9b8 mode=U
11/16/2011 20:37:59,spid20s,Unknown,owner-list
11/16/2011 20:37:59,spid20s,Unknown,keylock hobtid=72057594060931072 dbid=5 objectname=MyServer_Database.dbo.DEXTable indexname=PK_DEXTable id=lockc48e52780 mode=U associatedObjectId=72057594060931072
11/16/2011 20:37:59,spid20s,Unknown,waiter id=processce50ce088 mode=U requestType=wait
11/16/2011 20:37:59,spid20s,Unknown,waiter-list
11/16/2011 20:37:59,spid20s,Unknown,owner id=processac352e9b8 mode=U
11/16/2011 20:37:59,spid20s,Unknown,owner-list
11/16/2011 20:37:59,spid20s,Unknown,keylock hobtid=72057594060931072 dbid=5 objectname=MyServer_Database.dbo.DEXTable indexname=PK_DEXTable id=locka6ad4e580 mode=U associatedObjectId=72057594060931072
11/16/2011 20:37:59,spid20s,Unknown,waiter id=process8691198 mode=U requestType=wait
11/16/2011 20:37:59,spid20s,Unknown,waiter-list
11/16/2011 20:37:59,spid20s,Unknown,owner id=processcd7b1b048 mode=U
11/16/2011 20:37:59,spid20s,Unknown,owner-list
11/16/2011 20:37:59,spid20s,Unknown,keylock hobtid=72057594060931072 dbid=5 objectname=MyServer_Database.dbo.DEXTable indexname=PK_DEXTable id=lock95f600780 mode=U associatedObjectId=72057594060931072
11/16/2011 20:37:59,spid20s,Unknown,waiter id=process478da8 mode=U requestType=wait
11/16/2011 20:37:59,spid20s,Unknown,waiter-list
11/16/2011 20:37:59,spid20s,Unknown,owner id=processcd7b1b048 mode=U
11/16/2011 20:37:59,spid20s,Unknown,owner-list
11/16/2011 20:37:59,spid20s,Unknown,keylock hobtid=72057594060931072 dbid=5 objectname=MyServer_Database.dbo.DEXTable indexname=PK_DEXTable id=lock955c98200 mode=U associatedObjectId=72057594060931072
11/16/2011 20:37:59,spid20s,Unknown,waiter id=process700328 mode=U requestType=wait
11/16/2011 20:37:59,spid20s,Unknown,waiter-list
11/16/2011 20:37:59,spid20s,Unknown,owner id=processcd7b1b048 mode=U
11/16/2011 20:37:59,spid20s,Unknown,owner-list
11/16/2011 20:37:59,spid20s,Unknown,keylock hobtid=72057594060931072 dbid=5 objectname=MyServer_Database.dbo.DEXTable indexname=PK_DEXTable id=lock83fd3b200 mode=U associatedObjectId=72057594060931072
11/16/2011 20:37:59,spid20s,Unknown,waiter id=processffaef8 mode=U requestType=wait
11/16/2011 20:37:59,spid20s,Unknown,waiter-list
11/16/2011 20:37:59,spid20s,Unknown,owner id=processac352e9b8 mode=U
11/16/2011 20:37:59,spid20s,Unknown,owner-list
11/16/2011 20:37:59,spid20s,Unknown,keylock hobtid=72057594060931072 dbid=5 objectname=MyServer_Database.dbo.DEXTable indexname=PK_DEXTable id=lock77b633580 mode=U associatedObjectId=72057594060931072
11/16/2011 20:37:59,spid20s,Unknown,waiter id=process86a6ef8 mode=U requestType=wait
11/16/2011 20:37:59,spid20s,Unknown,waiter-list
11/16/2011 20:37:59,spid20s,Unknown,owner id=processcd7b1b048 mode=U
11/16/2011 20:37:59,spid20s,Unknown,owner-list
11/16/2011 20:37:59,spid20s,Unknown,keylock hobtid=72057594060931072 dbid=5 objectname=MyServer_Database.dbo.DEXTable indexname=PK_DEXTable id=lockdc536d580 mode=U associatedObjectId=72057594060931072
11/16/2011 20:37:59,spid20s,Unknown,waiter event=e_waitPipeGetRow type=consumer id=processcd7b1b048
11/16/2011 20:37:59,spid20s,Unknown,waiter-list
11/16/2011 20:37:59,spid20s,Unknown,owner event=e_waitNone type=producer id=process717198
11/16/2011 20:37:59,spid20s,Unknown,owner event=e_waitNone type=producer id=processffaef8
11/16/2011 20:37:59,spid20s,Unknown,owner event=e_waitNone type=producer id=process86a6478
11/16/2011 20:37:59,spid20s,Unknown,owner event=e_waitNone type=producer id=processdc28aeef8
11/16/2011 20:37:59,spid20s,Unknown,owner event=e_waitNone type=producer id=processce50ce088
11/16/2011 20:37:59,spid20s,Unknown,owner event=e_waitNone type=producer id=process47eda8
11/16/2011 20:37:59,spid20s,Unknown,owner-list
11/16/2011 20:37:59,spid20s,Unknown,exchangeEvent id=port80314690 nodeId=3
11/16/2011 20:37:59,spid20s,Unknown,waiter id=process717198 mode=U requestType=wait
11/16/2011 20:37:59,spid20s,Unknown,waiter-list
11/16/2011 20:37:59,spid20s,Unknown,owner id=processac352e9b8 mode=U
11/16/2011 20:37:59,spid20s,Unknown,owner-list
11/16/2011 20:37:59,spid20s,Unknown,keylock hobtid=72057594060931072 dbid=5 objectname=MyServer_Database.dbo.DEXTable indexname=PK_DEXTable id=lock68f374980 mode=U associatedObjectId=72057594060931072
11/16/2011 20:37:59,spid20s,Unknown,waiter id=process716c58 mode=U requestType=wait
11/16/2011 20:37:59,spid20s,Unknown,waiter-list
11/16/2011 20:37:59,spid20s,Unknown,owner id=processcd7b1b048 mode=U
11/16/2011 20:37:59,spid20s,Unknown,owner-list
11/16/2011 20:37:59,spid20s,Unknown,keylock hobtid=72057594060931072 dbid=5 objectname=MyServer_Database.dbo.DEXTable indexname=PK_DEXTable id=lock60e8d8a80 mode=U associatedObjectId=72057594060931072
11/16/2011 20:37:59,spid20s,Unknown,waiter id=process47f198 mode=U requestType=wait
11/16/2011 20:37:59,spid20s,Unknown,waiter-list
11/16/2011 20:37:59,spid20s,Unknown,owner id=processdc28aeef8 mode=U
11/16/2011 20:37:59,spid20s,Unknown,owner-list
11/16/2011 20:37:59,spid20s,Unknown,keylock hobtid=72057594060931072 dbid=5 objectname=MyServer_Database.dbo.DEXTable indexname=PK_DEXTable id=lockb7c7f1c00 mode=U associatedObjectId=72057594060931072
11/16/2011 20:37:59,spid20s,Unknown,waiter id=processdc28aeef8 mode=U requestType=wait
11/16/2011 20:37:59,spid20s,Unknown,waiter-list
11/16/2011 20:37:59,spid20s,Unknown,owner id=processac352e9b8 mode=U
11/16/2011 20:37:59,spid20s,Unknown,owner-list
11/16/2011 20:37:59,spid20s,Unknown,keylock hobtid=72057594060931072 dbid=5 objectname=MyServer_Database.dbo.DEXTable indexname=PK_DEXTable id=lock7797b6500 mode=U associatedObjectId=72057594060931072
11/16/2011 20:37:59,spid20s,Unknown,waiter event=e_waitPipeGetRow type=consumer id=processac352e9b8
11/16/2011 20:37:59,spid20s,Unknown,waiter-list
11/16/2011 20:37:59,spid20s,Unknown,owner event=e_waitNone type=producer id=process6d5c18
11/16/2011 20:37:59,spid20s,Unknown,owner event=e_waitNone type=producer id=process716c58
11/16/2011 20:37:59,spid20s,Unknown,owner event=e_waitNone type=producer id=process478da8
11/16/2011 20:37:59,spid20s,Unknown,owner event=e_waitNone type=producer id=process8691198
11/16/2011 20:37:59,spid20s,Unknown,owner event=e_waitNone type=producer id=process86a6ef8
11/16/2011 20:37:59,spid20s,Unknown,owner event=e_waitNone type=producer id=process700328
11/16/2011 20:37:59,spid20s,Unknown,owner event=e_waitNone type=producer id=process47f198
11/16/2011 20:37:59,spid20s,Unknown,owner-list
11/16/2011 20:37:59,spid20s,Unknown,exchangeEvent id=port80315870 nodeId=3
11/16/2011 20:37:59,spid20s,Unknown,waiter id=process6d5c18 mode=U requestType=wait
11/16/2011 20:37:59,spid20s,Unknown,waiter-list
11/16/2011 20:37:59,spid20s,Unknown,owner id=processcd7b1b048 mode=U
11/16/2011 20:37:59,spid20s,Unknown,owner-list
11/16/2011 20:37:59,spid20s,Unknown,keylock hobtid=72057594060931072 dbid=5 objectname=MyServer_Database.dbo.DEXTable indexname=PK_DEXTable id=lock46a62fe00 mode=U associatedObjectId=72057594060931072
11/16/2011 20:37:59,spid20s,Unknown,resource-list
11/16/2011 20:37:59,spid20s,Unknown,inputbuf
11/16/2011 20:37:59,spid20s,Unknown,WHERE RespondentID = @RespondentID
11/16/2011 20:37:59,spid20s,Unknown,DELETE DEXTable WITH (ROWLOCK)
11/16/2011 20:37:59,spid20s,Unknown,frame procname=MyServer_Database.dbo.ClearDEXTableRows line=7 stmtstart=334 sqlhandle=0x03000500f958193991f66b01a29e00000100000000000000
11/16/2011 20:37:59,spid20s,Unknown,executionStack
11/16/2011 20:37:59,spid20s,Unknown,process id=processdc28aeef8 taskpriority=0 logused=0 waitresource=KEY: 5:72057594060931072 (d600a7d4a467) waittime=4836 ownerId=299785428 transactionname=DELETE lasttranstarted=2011-11-16T20:37:53.820 XDES=0xce4278410 lockMode=U schedulerid=8 kpid=15756 status=suspended spid=157 sbid=0 ecid=6 priority=0 transcount=0 lastbatchstarted=2011-11-16T20:37:53.820 lastbatchcompleted=2011-11-16T20:37:53.807 clientapp=.Net SqlClient Data Provider hostname=309389-DB13 hostpid=6536 isolationlevel=read committed (2) xactid=299785428 currentdb=5 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
11/16/2011 20:37:59,spid20s,Unknown,inputbuf
11/16/2011 20:37:59,spid20s,Unknown,WHERE RespondentID = @RespondentID
11/16/2011 20:37:59,spid20s,Unknown,DELETE DEXTable WITH (ROWLOCK)
11/16/2011 20:37:59,spid20s,Unknown,frame procname=MyServer_Database.dbo.ClearDEXTableRows line=7 stmtstart=334 sqlhandle=0x03000500f958193991f66b01a29e00000100000000000000
11/16/2011 20:37:59,spid20s,Unknown,executionStack
11/16/2011 20:37:59,spid20s,Unknown,process id=processce50ce088 taskpriority=0 logused=0 waitresource=KEY: 5:72057594060931072 (d1007416f809) waittime=5538 ownerId=299785428 transactionname=DELETE lasttranstarted=2011-11-16T20:37:53.820 XDES=0x946b46d30 lockMode=U schedulerid=4 kpid=20396 status=suspended spid=157 sbid=0 ecid=3 priority=0 transcount=0 lastbatchstarted=2011-11-16T20:37:53.820 lastbatchcompleted=2011-11-16T20:37:53.807 clientapp=.Net SqlClient Data Provider hostname=309389-DB13 hostpid=6536 isolationlevel=read committed (2) xactid=299785428 currentdb=5 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
11/16/2011 20:37:59,spid20s,Unknown,Proc [Database Id = 5 Object Id = 957962489]
11/16/2011 20:37:59,spid20s,Unknown,inputbuf
11/16/2011 20:37:59,spid20s,Unknown,WHERE RespondentID = @RespondentID
11/16/2011 20:37:59,spid20s,Unknown,DELETE DEXTable WITH (ROWLOCK)
11/16/2011 20:37:59,spid20s,Unknown,frame procname=MyServer_Database.dbo.ClearDEXTableRows line=7 stmtstart=334 sqlhandle=0x03000500f958193991f66b01a29e00000100000000000000
11/16/2011 20:37:59,spid20s,Unknown,executionStack
11/16/2011 20:37:59,spid20s,Unknown,process id=processcd7b1b048 taskpriority=0 logused=20003 waittime=4118 schedulerid=3 kpid=13252 status=suspended spid=157 sbid=0 ecid=0 priority=0 transcount=2 lastbatchstarted=2011-11-16T20:37:53.820 lastbatchcompleted=2011-11-16T20:37:53.807 clientapp=.Net SqlClient Data Provider hostname=309389-DB13 hostpid=6536 loginname=IIS APPPOOL\MyServer_Database isolationlevel=read committed (2) xactid=299785428 currentdb=5 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
11/16/2011 20:37:59,spid20s,Unknown,Proc [Database Id = 5 Object Id = 957962489]
11/16/2011 20:37:59,spid20s,Unknown,inputbuf
11/16/2011 20:37:59,spid20s,Unknown,WHERE RespondentID = @RespondentID
11/16/2011 20:37:59,spid20s,Unknown,DELETE DEXTable WITH (ROWLOCK)
11/16/2011 20:37:59,spid20s,Unknown,frame procname=MyServer_Database.dbo.ClearDEXTableRows line=7 stmtstart=334 sqlhandle=0x03000500f958193991f66b01a29e00000100000000000000
11/16/2011 20:37:59,spid20s,Unknown,executionStack
11/16/2011 20:37:59,spid20s,Unknown,process id=processac352e9b8 taskpriority=0 logused=20003 waittime=4071 schedulerid=10 kpid=20384 status=suspended spid=147 sbid=0 ecid=0 priority=0 transcount=2 lastbatchstarted=2011-11-16T20:37:53.823 lastbatchcompleted=2011-11-16T20:37:53.810 clientapp=.Net SqlClient Data Provider hostname=309389-DB13 hostpid=6536 loginname=IIS APPPOOL\MyServer_Database isolationlevel=read committed (2) xactid=299785445 currentdb=5 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
11/16/2011 20:37:59,spid20s,Unknown,inputbuf
11/16/2011 20:37:59,spid20s,Unknown,WHERE RespondentID = @RespondentID
11/16/2011 20:37:59,spid20s,Unknown,DELETE DEXTable WITH (ROWLOCK)
11/16/2011 20:37:59,spid20s,Unknown,frame procname=MyServer_Database.dbo.ClearDEXTableRows line=7 stmtstart=334 sqlhandle=0x03000500f958193991f66b01a29e00000100000000000000
11/16/2011 20:37:59,spid20s,Unknown,executionStack
11/16/2011 20:37:59,spid20s,Unknown,process id=process86a6ef8 taskpriority=0 logused=0 waitresource=KEY: 5:72057594060931072 (ab0001e10f4e) waittime=6099 ownerId=299785445 transactionname=DELETE lasttranstarted=2011-11-16T20:37:53.823 XDES=0xdb9b53cc0 lockMode=U schedulerid=11 kpid=17448 status=suspended spid=147 sbid=0 ecid=4 priority=0 transcount=0 lastbatchstarted=2011-11-16T20:37:53.823 lastbatchcompleted=2011-11-16T20:37:53.810 clientapp=.Net SqlClient Data Provider hostname=309389-DB13 hostpid=6536 isolationlevel=read committed (2) xactid=299785445 currentdb=5 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
11/16/2011 20:37:59,spid20s,Unknown,inputbuf
11/16/2011 20:37:59,spid20s,Unknown,WHERE RespondentID = @RespondentID
11/16/2011 20:37:59,spid20s,Unknown,DELETE DEXTable WITH (ROWLOCK)
11/16/2011 20:37:59,spid20s,Unknown,frame procname=MyServer_Database.dbo.ClearDEXTableRows line=7 stmtstart=334 sqlhandle=0x03000500f958193991f66b01a29e00000100000000000000
11/16/2011 20:37:59,spid20s,Unknown,executionStack
11/16/2011 20:37:59,spid20s,Unknown,process id=process86a6478 taskpriority=0 logused=0 waitresource=KEY: 5:72057594060931072 (7500c3691103) waittime=6099 ownerId=299785428 transactionname=DELETE lasttranstarted=2011-11-16T20:37:53.820 XDES=0xdb9b53a80 lockMode=U schedulerid=11 kpid=19324 status=suspended spid=157 sbid=0 ecid=7 priority=0 transcount=0 lastbatchstarted=2011-11-16T20:37:53.820 lastbatchcompleted=2011-11-16T20:37:53.807 clientapp=.Net SqlClient Data Provider hostname=309389-DB13 hostpid=6536 isolationlevel=read committed (2) xactid=299785428 currentdb=5 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
11/16/2011 20:37:59,spid20s,Unknown,inputbuf
11/16/2011 20:37:59,spid20s,Unknown,WHERE RespondentID = @RespondentID
11/16/2011 20:37:59,spid20s,Unknown,DELETE DEXTable WITH (ROWLOCK)
11/16/2011 20:37:59,spid20s,Unknown,frame procname=MyServer_Database.dbo.ClearDEXTableRows line=7 stmtstart=334 sqlhandle=0x03000500f958193991f66b01a29e00000100000000000000
11/16/2011 20:37:59,spid20s,Unknown,executionStack
11/16/2011 20:37:59,spid20s,Unknown,process id=process8691198 taskpriority=0 logused=0 waitresource=KEY: 5:72057594060931072 (d20082032104) waittime=6052 ownerId=299785445 transactionname=DELETE lasttranstarted=2011-11-16T20:37:53.823 XDES=0xabdc20870 lockMode=U schedulerid=10 kpid=24760 status=suspended spid=147 sbid=0 ecid=7 priority=0 transcount=0 lastbatchstarted=2011-11-16T20:37:53.823 lastbatchcompleted=2011-11-16T20:37:53.810 clientapp=.Net SqlClient Data Provider hostname=309389-DB13 hostpid=6536 isolationlevel=read committed (2) xactid=299785445 currentdb=5 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
11/16/2011 20:37:59,spid20s,Unknown,inputbuf
11/16/2011 20:37:59,spid20s,Unknown,WHERE RespondentID = @RespondentID
11/16/2011 20:37:59,spid20s,Unknown,DELETE DEXTable WITH (ROWLOCK)
11/16/2011 20:37:59,spid20s,Unknown,frame procname=MyServer_Database.dbo.ClearDEXTableRows line=7 stmtstart=334 sqlhandle=0x03000500f958193991f66b01a29e00000100000000000000
11/16/2011 20:37:59,spid20s,Unknown,executionStack
11/16/2011 20:37:59,spid20s,Unknown,process id=processffaef8 taskpriority=0 logused=0 waitresource=KEY: 5:72057594060931072 (f900d9903a2a) waittime=6099 ownerId=299785428 transactionname=DELETE lasttranstarted=2011-11-16T20:37:53.820 XDES=0xd9f26e080 lockMode=U schedulerid=9 kpid=16712 status=suspended spid=157 sbid=0 ecid=8 priority=0 transcount=0 lastbatchstarted=2011-11-16T20:37:53.820 lastbatchcompleted=2011-11-16T20:37:53.807 clientapp=.Net SqlClient Data Provider hostname=309389-DB13 hostpid=6536 isolationlevel=read committed (2) xactid=299785428 currentdb=5 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
11/16/2011 20:37:59,spid20s,Unknown,inputbuf
11/16/2011 20:37:59,spid20s,Unknown,WHERE RespondentID = @RespondentID
11/16/2011 20:37:59,spid20s,Unknown,DELETE DEXTable WITH (ROWLOCK)
11/16/2011 20:37:59,spid20s,Unknown,frame procname=MyServer_Database.dbo.ClearDEXTableRows line=7 stmtstart=334 sqlhandle=0x03000500f958193991f66b01a29e00000100000000000000
11/16/2011 20:37:59,spid20s,Unknown,executionStack
11/16/2011 20:37:59,spid20s,Unknown,process id=process717198 taskpriority=0 logused=0 waitresource=KEY: 5:72057594060931072 (4700497f7879) waittime=5959 ownerId=299785428 transactionname=DELETE lasttranstarted=2011-11-16T20:37:53.820 XDES=0x8006dcc0 lockMode=U schedulerid=6 kpid=7420 status=suspended spid=157 sbid=0 ecid=12 priority=0 transcount=0 lastbatchstarted=2011-11-16T20:37:53.820 lastbatchcompleted=2011-11-16T20:37:53.807 clientapp=.Net SqlClient Data Provider hostname=309389-DB13 hostpid=6536 isolationlevel=read committed (2) xactid=299785428 currentdb=5 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
11/16/2011 20:37:59,spid20s,Unknown,inputbuf
11/16/2011 20:37:59,spid20s,Unknown,WHERE RespondentID = @RespondentID
11/16/2011 20:37:59,spid20s,Unknown,DELETE DEXTable WITH (ROWLOCK)
11/16/2011 20:37:59,spid20s,Unknown,frame procname=MyServer_Database.dbo.ClearDEXTableRows line=7 stmtstart=334 sqlhandle=0x03000500f958193991f66b01a29e00000100000000000000
11/16/2011 20:37:59,spid20s,Unknown,executionStack
11/16/2011 20:37:59,spid20s,Unknown,process id=process716c58 taskpriority=0 logused=0 waitresource=KEY: 5:72057594060931072 (5a00f098709d) waittime=5928 ownerId=299785445 transactionname=DELETE lasttranstarted=2011-11-16T20:37:53.823 XDES=0x6c020d880 lockMode=U schedulerid=6 kpid=17856 status=suspended spid=147 sbid=0 ecid=11 priority=0 transcount=0 lastbatchstarted=2011-11-16T20:37:53.823 lastbatchcompleted=2011-11-16T20:37:53.810 clientapp=.Net SqlClient Data Provider hostname=309389-DB13 hostpid=6536 isolationlevel=read committed (2) xactid=299785445 currentdb=5 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
11/16/2011 20:37:59,spid20s,Unknown,inputbuf
11/16/2011 20:37:59,spid20s,Unknown,WHERE RespondentID = @RespondentID
11/16/2011 20:37:59,spid20s,Unknown,DELETE DEXTable WITH (ROWLOCK)
11/16/2011 20:37:59,spid20s,Unknown,frame procname=MyServer_Database.dbo.ClearDEXTableRows line=7 stmtstart=334 sqlhandle=0x03000500f958193991f66b01a29e00000100000000000000
11/16/2011 20:37:59,spid20s,Unknown,executionStack
11/16/2011 20:37:59,spid20s,Unknown,process id=process700328 taskpriority=0 logused=0 waitresource=KEY: 5:72057594060931072 (51003376bf57) waittime=6099 ownerId=299785445 transactionname=DELETE lasttranstarted=2011-11-16T20:37:53.823 XDES=0x92beba3d0 lockMode=U schedulerid=5 kpid=7004 status=suspended spid=147 sbid=0 ecid=3 priority=0 transcount=0 lastbatchstarted=2011-11-16T20:37:53.823 lastbatchcompleted=2011-11-16T20:37:53.810 clientapp=.Net SqlClient Data Provider hostname=309389-DB13 hostpid=6536 isolationlevel=read committed (2) xactid=299785445 currentdb=5 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
11/16/2011 20:37:59,spid20s,Unknown,inputbuf
11/16/2011 20:37:59,spid20s,Unknown,WHERE RespondentID = @RespondentID
11/16/2011 20:37:59,spid20s,Unknown,DELETE DEXTable WITH (ROWLOCK)
11/16/2011 20:37:59,spid20s,Unknown,frame procname=MyServer_Database.dbo.ClearDEXTableRows line=7 stmtstart=334 sqlhandle=0x03000500f958193991f66b01a29e00000100000000000000
11/16/2011 20:37:59,spid20s,Unknown,executionStack
11/16/2011 20:37:59,spid20s,Unknown,process id=process6d5c18 taskpriority=0 logused=0 waitresource=KEY: 5:72057594060931072 (150048fb6c35) waittime=5803 ownerId=299785445 transactionname=DELETE lasttranstarted=2011-11-16T20:37:53.823 XDES=0xdbadb2560 lockMode=U schedulerid=3 kpid=3824 status=suspended spid=147 sbid=0 ecid=12 priority=0 transcount=0 lastbatchstarted=2011-11-16T20:37:53.823 lastbatchcompleted=2011-11-16T20:37:53.810 clientapp=.Net SqlClient Data Provider hostname=309389-DB13 hostpid=6536 isolationlevel=read committed (2) xactid=299785445 currentdb=5 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
11/16/2011 20:37:59,spid20s,Unknown,inputbuf
11/16/2011 20:37:59,spid20s,Unknown,WHERE RespondentID = @RespondentID
11/16/2011 20:37:59,spid20s,Unknown,DELETE DEXTable WITH (ROWLOCK)
11/16/2011 20:37:59,spid20s,Unknown,frame procname=MyServer_Database.dbo.ClearDEXTableRows line=7 stmtstart=334 sqlhandle=0x03000500f958193991f66b01a29e00000100000000000000
11/16/2011 20:37:59,spid20s,Unknown,executionStack
11/16/2011 20:37:59,spid20s,Unknown,process id=process47f198 taskpriority=0 logused=0 waitresource=KEY: 5:72057594060931072 (4700c2a10b35) waittime=6037 ownerId=299785445 transactionname=DELETE lasttranstarted=2011-11-16T20:37:53.823 XDES=0x6c2da4080 lockMode=U schedulerid=2 kpid=8564 status=suspended spid=147 sbid=0 ecid=1 priority=0 transcount=0 lastbatchstarted=2011-11-16T20:37:53.823 lastbatchcompleted=2011-11-16T20:37:53.810 clientapp=.Net SqlClient Data Provider hostname=309389-DB13 hostpid=6536 isolationlevel=read committed (2) xactid=299785445 currentdb=5 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
11/16/2011 20:37:59,spid20s,Unknown,inputbuf
11/16/2011 20:37:59,spid20s,Unknown,WHERE RespondentID = @RespondentID
11/16/2011 20:37:59,spid20s,Unknown,DELETE DEXTable WITH (ROWLOCK)
11/16/2011 20:37:59,spid20s,Unknown,frame procname=MyServer_Database.dbo.ClearDEXTableRows line=7 stmtstart=334 sqlhandle=0x03000500f958193991f66b01a29e00000100000000000000
11/16/2011 20:37:59,spid20s,Unknown,executionStack
11/16/2011 20:37:59,spid20s,Unknown,process id=process47eda8 taskpriority=0 logused=0 waitresource=KEY: 5:72057594060931072 (2a004ee465b9) waittime=6099 ownerId=299785428 transactionname=DELETE lasttranstarted=2011-11-16T20:37:53.820 XDES=0x6c2da4870 lockMode=U schedulerid=2 kpid=24652 status=suspended spid=157 sbid=0 ecid=1 priority=0 transcount=0 lastbatchstarted=2011-11-16T20:37:53.820 lastbatchcompleted=2011-11-16T20:37:53.807 clientapp=.Net SqlClient Data Provider hostname=309389-DB13 hostpid=6536 isolationlevel=read committed (2) xactid=299785428 currentdb=5 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
11/16/2011 20:37:59,spid20s,Unknown,inputbuf
11/16/2011 20:37:59,spid20s,Unknown,WHERE RespondentID = @RespondentID
11/16/2011 20:37:59,spid20s,Unknown,DELETE DEXTable WITH (ROWLOCK)
11/16/2011 20:37:59,spid20s,Unknown,frame procname=MyServer_Database.dbo.ClearDEXTableRows line=7 stmtstart=334 sqlhandle=0x03000500f958193991f66b01a29e00000100000000000000
11/16/2011 20:37:59,spid20s,Unknown,executionStack
11/16/2011 20:37:59,spid20s,Unknown,process id=process478da8 taskpriority=0 logused=0 waitresource=KEY: 5:72057594060931072 (1400c876e809) waittime=5709 ownerId=299785445 transactionname=DELETE lasttranstarted=2011-11-16T20:37:53.823 XDES=0x857272d30 lockMode=U schedulerid=1 kpid=7804 status=suspended spid=147 sbid=0 ecid=9 priority=0 transcount=0 lastbatchstarted=2011-11-16T20:37:53.823 lastbatchcompleted=2011-11-16T20:37:53.810 clientapp=.Net SqlClient Data Provider hostname=309389-DB13 hostpid=6536 isolationlevel=read committed (2) xactid=299785445 currentdb=5 lockTimeout=4294967295 clientoption1=671088672 clientoption2=128056
11/16/2011 20:37:59,spid20s,Unknown,process-list
11/16/2011 20:37:59,spid20s,Unknown,deadlock victim=processdc28aeef8
11/16/2011 20:37:59,spid20s,Unknown,deadlock-list
11/16/2011 16:40:12,spid83,Unknown,DBCC TRACEON 1222<c/> server process ID (SPID) 83. This is an informational message only; no user action is required.

2
Puoi assicurarti che il flag di traccia 1204 sia attivato a livello globale "dbcc traceon (1204, -1)" e pubblicare quell'acquisizione dopo il successivo deadlock.
mrdenny,

Grazie a tutti per il feedback rapido e utile! È molto apprezzato Ho abilitato il flag di traccia 1204 e sto andando avanti invertendo l'ordine PK in (RespondentID, ExportID) e rimuovendo il suggerimento DELETE WITH (ROWLOCK). Per quanto riguarda l'aggiunta del suggerimento MAXDOP 1, intendete con la procedura InsertDEXTableRow o ClearDEXTableRows? E se dovesse entrare nella procedura InsertDEXTableRow, pensi che sarà più efficace all'interno di un blocco UPSERT rispetto a un altro? (sono state suggerite 3 alternative UPSERT).
Bobby R-1of4 del

Ancora una volta, i miei ringraziamenti per l'ottimo feedback al mio post: sono andato avanti con i seguenti passaggi: • Invertito il PK sulla tabella DEXTable (prima RespondentID, quindi ExportID) per evitare scansioni di tabelle. • È stato eliminato un suggerimento ROWLOCK non necessario nella procedura ClearDEXTableRows. • Nella procedura InsertDEXTableRow è stato utilizzato un modello di aggiornamento sicuro (aggiornamento o inserimento) della concorrenza (opzione 1 consigliata da Mark Storey-Smith).
BobbyR-1 del 4

continua ... Prima di apportare le modifiche, ho confermato che la mia applicazione Web non stava eseguendo alcuna query sulla tabella DEXTable con solo ExportID nella clausola where o INNER JOINs senza prima filtrare per RespondentID. Ho cercato sia il DB del server sql che la base di codice (per i riferimenti linq2sql, ecc.). L'ordine dei riferimenti di colonna nella clausola WHERE non dovrebbe avere importanza (ovvero WHERE ExportID = at-ExportID AND RespondentID = at-RespondentID contro WHERE RespondentID = at-RespondentID AND ExportID = at-ExportID), in base alle informazioni che ho trovato su Google quello.
Bobby R-1 del 4

continua ... Ho confermato che i piani di esecuzione sql sono passati dalle scansioni alle ricerche dopo aver invertito l'ordine delle colonne FK all'interno del PK. Non ho usato il suggerimento del suggerimento MAXDOP 1, dal momento che non sono sicuro di quale procedura dovrebbe usarlo (ClearDEXTableRows o InsertDEXTableRow) e se ha ancora senso data l'opzione che ho scelto (annotato sopra).
BobbyR-1 del 4

Risposte:


7

Tre cose saltano fuori:

  1. Il tuo DELETE è sulla seconda colonna (RespondentID) dell'attuale PK che significa una scansione, non una ricerca.

  2. Suggerimento ROWLOCK inutile

  3. Il modello "UPSERT" non è sicuro per la concorrenza. Il test di esistenza può passare per 2 thread simultanei sovrapposti (nel tempo) che generano un errore.

Aggiustare

  1. Invertire l'ordine PK in DEXTable a (RespondentID, ExportID). Oppure aggiungi un indice separato solo su RespondentID. Personalmente, probabilmente invertirei l'ordine PK.

  2. Rimuovere il suggerimento ROWLOCK. Se continua dopo le modifiche all'indice e UPSERT suggerite qui, provare UPDLOCK, ma solo dopo aver verificato il parallelismo

  3. Verifica il parallelismo nel piano: prova a limitare MAXDOP 1. Prova questo prima di UPDLOCK

  4. Utilizzare il modello UPSERT "JFDI". Cioè, INSERT verificherà comunque l'unicità, quindi solo INSERT, se fallisce, quindi UPDATE.

Per SQL Server 2005 (utilizzeresti MERGE su SQL Server 2008+)

BEGIN TRY

    BEGIN TRY
        INSERT DEXTable (ExportID, RespondentID, Exported)
        VALUES (@ExportID, @RespondentID, getdate())
    END TRY
    BEGIN CATCH
        IF ERROR_NUMBER() = 2627
            UPDATE DEXTable
            SET Exported = getdate()
            WHERE ExportID = @ExportID AND RespondentID = @RespondentID
        ELSE
        BEGIN
           ..process real error
           RAISERROR (...)
        END
    END CATCH

END TRY
BEGIN CATCH
    ..process
    RAISERROR (...)
END CATCH

Infine, MSDN consiglia di intercettare gli errori di deadlock e riprovare sul lato client. Questo dovrebbe far parte del livello di accesso ai dati che gestisce tutte le chiamate SQL.


5

+1 per la spiegazione di @ gbn della probabile causa, ma non sono un fan del modello INSERT / UPDATE suggerito, a meno che un aggiornamento non sia davvero il caso eccezionale (ovvero il 99,99% delle volte in cui si verifica un inserimento). Il mio approccio preferito è sempre stato:

BEGIN TRANSACTION

UPDATE DEXTable WITH (UPDLOCK, HOLDLOCK)
SET Exported = getdate()
WHERE ExportID = @ExportID AND RespondentID = @RespondentID

IF (@@ROWCOUNT = 0)
BEGIN
    INSERT DEXTable (ExportID, RespondentID, Exported)
    VALUES (@ExportID, @RespondentID, getdate())
END

COMMIT TRANSACTION

È inoltre possibile alleviare il deadlock aggiungendo suggerimenti alla query corrente:

BEGIN TRANSACTION

IF NOT EXISTS (SELECT RespondentID FROM DEXTable WITH (UPDLOCK, HOLDLOCK) WHERE ExportID = @ExportID AND RespondentID = @RespondentID)
    BEGIN
      INSERT DEXTable
        (ExportID, RespondentID, Exported)
      VALUES
        (@ExportID, @RespondentID, getdate())
    END
    ELSE
    BEGIN
      UPDATE DEXTable
      SET Exported = getdate()
      WHERE ExportID = @ExportID AND RespondentID = @RespondentID
    END

COMMIT TRANSACTION

2

Senza ulteriori informazioni sulla situazione di stallo, sto solo indovinando qui ma ...

Non ci sono indici sulla colonna che stai usando Trova le righe da eliminare. Ciò significa una scansione di indice cluster ogni volta, il che significa molti blocchi all'avvio dell'eliminazione. Anche se hai WITH (ROWLOCK) lì dentro, ci sono ancora dei blocchi di schema sulla tabella per assicurarti che non stia succedendo nulla.

Prova ad aggiungere un indice sulla colonna RespondentID e verifica se il problema è stato risolto. Guardare il piano di esecuzione prima e dopo la creazione di tale indice dovrebbe dare qualche indicazione sul problema da risolvere.

Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.