简体   繁体   English

SQL Server死锁

[英]SQL Server DeadLocks

In my production server i am keep on facing deadlock issue. 在生产服务器中,我一直面临死锁问题。 I have gone through many links but I am unable to find a solution. 我已经通过许多链接,但是找不到解决方案。

Please find trace 请找到踪影

deadlock-list
 deadlock victim=process500ce08
  process-list
   process id=process500ce08 taskpriority=0 logused=0 waitresource=PAGE: 7:1:3509698 waittime=5074 ownerId=53243840 
   transactionname=implicit_transaction lasttranstarted=2015-10-20T04:00:02.037 XDES=0x2af523970 lockMode=IU schedulerid=6 kpid=7072 
   status=suspended spid=81 sbid=0 ecid=0 priority=0 trancount=2 lastbatchstarted=2015-10-20T04:00:02.037 lastbatchcompleted=2015-10-20T04:00:02.033 
   hostname=10.9.52.12 hostpid=0 loginname=prod isolationlevel=read committed (2) xactid=53243840 currentdb=7 lockTimeout=4294967295 
   clientoption1=671088928 clientoption2=128058
    executionStack
     frame procname=adhoc line=1 stmtstart=116 sqlhandle=0x020000008d082701122cdc931bffce58ad37dec1a2f23e9d
UPDATE dbo.InterfaceTable SET ERRORMESSAGE = @P1 , ACK_DATE = @P2  WHERE (SSID = @P3 )     
     frame procname=unknown line=1 sqlhandle=0x000000000000000000000000000000000000000000000000
unknown     
    inputbuf
(@P1 nvarchar(4000),@P2 datetimeoffset,@P3 nvarchar(4000))UPDATE dbo.InterfaceTable SET ERRORMESSAGE = @P1 , ACK_DATE = @P2  WHERE (SSID = @P3 )    
   process id=process6ca8748 taskpriority=0 logused=112 waitresource=PAGE: 7:1:6987355 waittime=4931 ownerId=53242925 
   transactionname=DELETE lasttranstarted=2015-10-20T04:00:01.320 XDES=0x4408a8080 lockMode=IX schedulerid=13 kpid=5820 
   status=suspended spid=123 sbid=0 ecid=0 priority=0 trancount=2 lastbatchstarted=2015-10-20T04:00:01.043 lastbatchcompleted=2015-10-20T04:00:01.043 
   clientapp=SQLAgent - TSQL JobStep (Job 0x12C15DD2EBA4CD4DAFEE3482987A95C6 : Step 1) hostname=APP01 hostpid=2972 
   loginname=APP01\sqlsupport isolationlevel=read uncommitted (1) xactid=53242925 currentdb=7 lockTimeout=4294967295 clientoption1=671088928 
   clientoption2=128056
    executionStack
     frame procname=adhoc line=1 sqlhandle=0x02000000bede8d1322a460d49c8399173b5b500cd397607c
Delete from INTERFACE_TABLE
            where ( ERRORMESSAGE is null  or  ERRORMESSAGE like '%AGREEMENT NOT FOUND.;%'  or  ERRORMESSAGE like '%UNI:REALLOCATED%')
            and ACK_DATE is not null
            and ACKID is not null and SSID in(select SSID from INTERFACE_TABLE_2015 with(Nolock) )     
     frame procname=Data_Purging line=91 stmtstart=8514 stmtend=8574 sqlhandle=0x03000700dcdb321339b16c0034a500000100000000000000
exec(@Interface_Execution1)     
     frame procname=adhoc line=1 sqlhandle=0x01000700910a2006e0a057ab040000000000000000000000
exec Data_Purging     
    inputbuf
exec Data_Purging    
  resource-list
   pagelock fileid=1 pageid=3509698 dbid=7 objectname=chola.dbo.INTERFACE_TABLE id=lockc1c6ec00 mode=U associatedObjectId=72057594778353664
    owner-list
     owner id=process6ca8748 mode=U
    waiter-list
     waiter id=process500ce08 mode=IU requestType=wait
   pagelock fileid=1 pageid=6987355 dbid=7 objectname=chola.dbo.INTERFACE_TABLE id=lock25717c080 mode=U associatedObjectId=72057594781958144
    owner-list
     owner id=process500ce08 mode=U
     waiter id=process6ca8748 mode=IX requestType=wait
    waiter-list

Update InterfaceTable is a view. 更新InterfaceTable是一个视图。 while updating this I am updating INTERFACE_TABLE table. 在更新此时,我正在更新INTERFACE_TABLE表。

INTERFACE_TABLE table has primary key SSID INTERFACE_TABLE表具有主键SSID

Please help me to avoid deadlock. 请帮助我避免死锁。 Thanks in advance. 提前致谢。

Going by this: 通过这个:

https://www.mssqltips.com/sqlservertip/2130/finding-sql-server-deadlocks-using-trace-flag-1222/ https://www.mssqltips.com/sqlservertip/2130/finding-sql-server-deadlocks-using-trace-flag-1222/

you can see two spids in there (81,123) 您可以在那里看到两个spid(81,123)

81 was the victim. 81是受害者。 it was running: 它正在运行:

 UPDATE dbo.InterfaceTable SET ERRORMESSAGE = @P1 , ACK_DATE = @P2  WHERE (SSID = @P3 )

it was run from hostname=10.9.52.12, loginname=prod 它是从主机名= 1.0.9.52.12,登录名= prod运行的

123 was the winner ,it was running: 123是获胜者,它正在运行:

Delete from INTERFACE_TABLE
where ( ERRORMESSAGE is null  or  ERRORMESSAGE 
like '%AGREEMENT NOT FOUND.;%'  
or  ERRORMESSAGE like '%UNI:REALLOCATED%')
and ACK_DATE is not null
and ACKID is not null and SSID in(
     select SSID from INTERFACE_TABLE_2015 with(Nolock) 
)     

it was from from hostname=APP01 hostpid=2972, loginname=APP01\\sqlsupport in a SQL Agent job . 它来自SQL Agent作业中的主机名= APP01 hostpid = 2972​​,登录名= APP01 \\ sqlsupport。

There are a few ways to attack this: 有几种方法可以解决此问题:

  1. Understand what these two things were doing and change the process. 了解这两件事在做什么,并更改流程。 It looks to me like the first one is a client app updating a single error log line and the second one a SQL Agent job that cleans up the logs. 在我看来,第一个是客户端应用程序,用于更新单个错误日志行,第二个是用于清理日志的SQL Agent作业。 You could set the deadlock priority of the SQL Agent cleanup job so that it will fail rather than the application log (which is what you want) 您可以设置SQL Agent清理作业的死锁优先级,以使其失败,而不是应用程序日志(这是您想要的)

  2. Long transactions encourage deadlocks. 长时间的交易会导致僵局。 Add indexes or optimise queries (why are you comparing to INTERFACE_TABLE_2015 every time?) so they don't take so long 添加索引或优化查询(为什么每次都要与INTERFACE_TABLE_2015进行比较?),所以它们不会花很长时间

  3. Change your database to use SNAPSHOT ISOLATION and magically fix a lot of deadlock issues 更改数据库以使用快照隔离并神奇地修复许多死锁问题

Someone with more experience reading these could deduce exactly what happened here but that's a start. 拥有更多阅读经验的人可以确切推断出这里发生的事情,但这只是一个开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM