Key Concepts SQL Optimization: Deadlock

: Original Key Concepts SQL Optimization: Deadlock

 

The above mentioned articles transaction, locking, blocking, and finally there is a more extreme case, is the deadlock, which is locked, a situation blocked.

Deadlock is when two transactions were locked resources, but continue to request other resources acquired, it will produce a deadlock.


Deadlock occurs because of:
A, sequential access session to a different table.
B, Session long-running transactions, update in one transaction or a lot of table rows, thus increasing the potential for conflict.
C, Session 1 apply some row locks, session 2 to apply some row locks, then decided to escalate to a table lock.
   If these rows of the same data page, and two sessions to upgrade the lock granularity in the same page, it will deadlocks.

 

1, Session 1


   
   
  1. set lock_timeout 1000
  2. --跟踪死锁--会话1
  3. set transaction isolation level serializable
  4. begin tran
  5. update t
  6. set v = '563'
  7. where idd = 2
  8. waitfor delay '00:00:10'
  9. update t
  10. set v = '963'
  11. where idd = 1
  12. COMMIT

2, Session 2


   
   
  1. set transaction isolation level serializable
  2. begin tran
  3. update t
  4. set v = '234'
  5. where idd = 1
  6. waitfor delay '00:00:10'
  7. update t
  8. set v = '987'
  9. where idd= 2
  10. commit

3, then open a conversation, open track

Open trace flag:
              the DBCC TRACEON (the trace # [, ... n-], -. 1) [With NO_INFOMSGS]

Check one or some flag is on or off:
              the DBCC TRACESTATUS (the trace # [, ... n-], -. 1) [With NO_INFOMSGS]

1.trace #: specify one or more of the need to open or to check the status of the trace flags digit
2 -1: If -1 is specified globally open places one or some trace flag
3.with No_InfoMsgs: when this parameter is included in the command, DBCC output messages is prohibited


   
   
  1. --跟踪1222能把详细的死锁信息返回到SQL Server的日志中
  2. --标志位-1表示跟踪标志位1222应该对所有SQL Server连接全局启用
  3. DBCC TraceOn(1222,-1)
  4. go
  5. --验证标志位是否启动
  6. DBCC TraceStatus
  7. go
  8. --关闭标志位
  9. DBCC TraceOff(1222,-1)
  10. go

4, set the deadlock priority - the possibility of setting deadlock priority, adjust a query session is terminated due to a deadlock running

SET DeadLock_Priority  Low | Normal | High | numeric-priority


   
   
  1. --是当前连接很有可能被终止运行
  2. set deadlock_priority Low
  3. --SQL Server终止回滚代价较小的连接
  4. set deadlock_priority Normal
  5. --减少连接被终止的可能性,除非另一个连接也是High或数值优先级大于5
  6. set deadlock_priority High
  7. --数值优先级:-10到10的值,-10最有可能被终止运行,10最不可能被终止运行,
  8. --两个数字谁大,谁就越不可能在死锁中被终止
  9. set deadlock_priority 10

 

发布了416 篇原创文章 · 获赞 135 · 访问量 94万+

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12019933.html