デッドロック分析

4040 ワード

来た:http://blog.csdn.net/roy_88/articale/detail/2686724 
--  
/**********************************************************************************************************************
                         ,SQL Server                           ,
            。

   :   (Roy)

  :2008.07.20
***********************************************************************************************************************/

set nocount on ;
if object_id('T1') is not null
	drop table T1
go
create table T1(ID int primary key,Col1 int,Col2 nvarchar(20))
insert T1 select 1,101,'A'
insert T1 select 2,102,'B'
insert T1 select 3,103,'C'
go

if object_id('T2') is not null
	drop table T2
go
create table T2(ID int primary key,Col1 int,Col2 nvarchar(20))
insert T2 select 1,201,'X'
insert T2 select 2,202,'Y'
insert T2 select 3,203,'Z'


go
     :
/*
T1:
ID          Col1        Col2
----------- ----------- --------------------
1           101         A
2           101         B
3           101         C

T2:
ID          Col1        Col2
----------- ----------- --------------------
1           201         X
2           201         Y
3           201         Z
*/

    :
1、	     。    ,        
2、	          (    :  2)
3、	            1205             
4、	                       
5、	       (    :  1、  3)
      ,      ,          


  1(  ):
--    1
--1 :
begin tran
	update t1 set col2=col2+'A' where col1=101

--3 :
	select * from t2 where col1=201
commit tran


--    2

--2 :
begin tran
	update t2 set col2=col2+'B' where col1=203

--4 :
	select * from t1 where col1=103
commit tran



--    1:      ,    2    :

/*
   1205,   13,   51,  3
   (       53)                               。        。
*/

--    2:    

/*
----------- ----------- --------------------
3           103         C
*/

    :
-- t1、t2  col1      
create index IX_t1_col1 on t1(col1)
create index IX_t2_col1 on t2(col1)
go

--    1
--1 :
begin tran
	update t1 set col2=col2+'A' where col1=101

--3 :
select * from t2 with(index=IX_t2_col1)where col1=201	--     ,            SQL Server    
commit tran



--    2

--2 :
begin tran
	update t2 set col2=col2+'B' where col1=203


--4 :
select * from t1 with(index=IX_t1_col1) where col1=103	--     ,            SQL Server    
commit tran



--    1:
/*
ID          Col1        Col2
----------- ----------- --------------------
1           201         X

(1         )

*/
--    2
/*
ID          Col1        Col2
----------- ----------- --------------------
3           103         C

(1         )
*/


  2(     ):

--    1:
--1 :
begin tran
	update t1 set col1=col1+1 where ID=1

--3 :
select col1 from t2 where ID=1
commit tran



--    2:
--2 :
begin tran
	update t2 set col1=col1+1 where ID=1

--4 
select col1 from t1 where ID=1
commit tran


--    1:

/*
col1
-----------
201

(1         )
*/

--    2:

/*
col1
-----------
   1205,   13,   51,  1
   (       54)                               。        。
*/

    :

--        

--    1:
--1 :
begin tran
	update t1 set col1=col1+1 where ID=1

--3 :
	select col1 from t2 where ID=1
commit tran

--    2:
--2 :
begin tran
	select col1 from t1 where ID=1--       1  
--4 
	update t2 set col1=col1+1 where ID=1
commit tran

  3(  ):

--    1:

while 1=1
	update T1 set col1=203-col1 where ID=2

--    2:
declare @i  nvarchar(20)
while 1=1
	set @i=(select col2 from T1 with(index=IX_t1_col1)where Col1=102);--     ,            SQL Server    

--    1
/*
   1205,   13,   51,  4
   (       53)                               。        。
*/


    :
1、  col1       ,    SELECT  ,   .
	drop index IX_t1_col1 on t1
2、       
	A、drop index IX_t1_col1 on t1
	B、create index IX_t1_col1_col2 on t1(col1,col2)


  SQL Server Profiler     :

  SQL Server Profiler——    ——      ——      
   :
TSQL——SQL:StmtStarting
Locks——Deadlock graph(  SQL2005    ,         xml )
     ——Lock:DeadlockChain             ,        ID     
	 ——Lock:Deadlock         
<pre></pre>