阻塞分析

  1. --阻塞<BR>/******************************************************************************************************************************************************<BR>阻塞:其中一个事务阻塞,其它事务等待对方释放它们的锁,同时会导致死锁问题。  
  1. 整理人:中国风(Roy)  
  1. 日期:2008.07.20<BR>******************************************************************************************************************************************************/  
  1. --生成测试表Ta   
  2. if not object_id(&apos;Ta&apos;) is null  
  3.     drop table Ta   
  4. go   
  5. create table Ta(ID int Primary key,Col1 int,Col2 nvarchar(10))   
  6. insert Ta    
  7. select 1,101,&apos;A&apos; union all  
  8. select 2,102,&apos;B&apos; union all  
  9. select 3,103,&apos;C&apos;   
  10. go   
  11. 生成数据:   
  12. /*   
  13. 表Ta   
  14. ID          Col1        Col2   
  15. ----------- ----------- ----------   
  16. 1           101         A   
  17. 2           102         B   
  18. 3           103         C   
  19.   
  20. (3 行受影响)   
  21. */   
  22.   
  23. 将处理阻塞减到最少:   
  24. 1、事务要尽量短   
  25. 2、不要在事务中请求用户输入   
  26. 3、在读数据考虑便用行版本管理   
  27. 4、在事务中尽量访问最少量的数据   
  28. 5、尽可能地使用低的事务隔离级别   
  29.   
  30. go   
  31. 阻塞1(事务):   
  32. --测试单表   
  33.   
  34. -----------------------------连接窗口1(update\insert\delete)----------------------   
  35. begin tran   
  36. --update   
  37.     update ta set col2=&apos;BB&apos; where ID=2   
  38. --或insert   
  39. begin tran   
  40.     insert Ta values(4,104,&apos;D&apos;)   
  41. --或delete   
  42. begin tran   
  43.     delete ta where ID=1   
  44.   
  45. --rollback tran   
  46.   
  47. ------------------------------------------连接窗口2--------------------------------   
  48. begin tran   
  49.     select * from ta   
  50.   
  51. --rollback tran   
  52.   
  53. --------------分析-----------------------   
  54. select    
  55.     request_session_id as spid,   
  56.     resource_type,   
  57.     db_name(resource_database_id) as dbName,   
  58.     resource_description,   
  59.     resource_associated_entity_id,   
  60.     request_mode as mode,   
  61.     request_status as Status   
  62. from    
  63.     sys.dm_tran_locks   
  64. /*   
  65. spid        resource_type dbName resource_description resource_associated_entity_id mode  Status   
  66. ----------- ------------- ------ -------------------- ----------------------------- ----- ------   
  67. 55          DATABASE      Test   0                    S                             GRANT NULL  
  68. 54          DATABASE      Test   0                    S                             GRANT NULL  
  69. 53          DATABASE      Test   0                    S                             GRANT NULL  
  70. 55          PAGE          Test   1:201                72057594040483840             IS    GRANT  
  71. 54          PAGE          Test   1:201                72057594040483840             IX    GRANT  
  72. 55          OBJECT        Test   1774629365           IS                            GRANT NULL  
  73. 54          OBJECT        Test   1774629365           IX                            GRANT NULL  
  74. 54          KEY           Test   (020068e8b274)       72057594040483840             X     GRANT --(spID:54请求了排它锁)   
  75. 55          KEY           Test   (020068e8b274)       72057594040483840             S     WAIT  --(spID:55共享锁+等待状态)   
  76. (9 行受影响)   
  77. */   
  78.   
  79. --查连接住信息(spid:54、55)   
  80. select connect_time,last_read,last_write,most_recent_sql_handle     
  81. from sys.dm_exec_connections where session_id in(54,55)   
  82.   
  83. --查看会话信息   
  84. select login_time,host_name,program_name,login_name,last_request_start_time,last_request_end_time    
  85. from sys.dm_exec_sessions where session_id in(54,55)   
  86.   
  87. --查看阻塞正在执行的请求   
  88. select    
  89.     session_id,blocking_session_id,wait_type,wait_time,wait_resource   
  90. from    
  91.     sys.dm_exec_requests   
  92. where  
  93.     blocking_session_id>0--正在阻塞请求的会话的 ID。如果此列是 NULL,则不会阻塞请求   
  94.   
  95. --查看正在执行的SQL语句   
  96.   
  97. select    
  98.     a.session_id,sql.text,a.most_recent_sql_handle   
  99. from    
  100.     sys.dm_exec_connections a   
  101. cross apply   
  102.     sys.dm_exec_sql_text(a.most_recent_sql_handle) as SQL   --也可用函数fn_get_sql通过most_recent_sql_handle得到执行语句   
  103. where  
  104.     a.Session_id in(54,55)   
  105. /*   
  106. session_id  text   
  107. ----------- -----------------------------------------------   
  108. 54          begin tran   update ta set col2=&apos;BB&apos; where ID=2   
  109. 55          begin tran   select * from ta   
  110. */   
  111.   
  112. 处理方法:   
  113. --连接窗口2   
  114. begin tran   
  115.     select * from ta with (nolock)--用nolock:业务数据不断变化中,如销售查看当月时可用。   
  116.   
  117.   
  118.   
  119.   
  120.   
  121. 阻塞2(索引):   
  122.   
  123. -----------------------连接窗口1   
  124. SET TRANSACTION ISOLATION LEVEL SERIALIZABLE    --针对会话设置了 TRANSACTION ISOLATION LEVEL   
  125. begin tran   
  126.     update ta set col2=&apos;BB&apos; where COl1=102   
  127.   
  128. --rollback tran   
  129.   
  130.   
  131.   
  132. ------------------------连接窗口2   
  133. insert into ta(ID,Col1,Col2) values(5,105,&apos;E&apos;)   
  134.   
  135.   
  136.   
  137. 处理方法:   
  138.   
  139. create index IX_Ta_Col1 on Ta(Col1)--用COl1列上创索引,当更新时条件:COl1=102会用到索引IX_Ta_Col1上得到一个排它键的范围锁   
  140.   
  141.   
  142.   
  143. 阻塞3(会话设置):   
  144.   
  145. -------------------------------连接窗口1   
  146.   
  147. begin tran   
  148. --update   
  149.     update ta set col2=&apos;BB&apos; where ID=2   
  150.     select col2 from ta where ID=2   
  151.   
  152. --rollback tran   
  153.   
  154. --------------------------------连接窗口2   
  155.   
  156. SET TRANSACTION ISOLATION LEVEL READ COMMITTED --设置会话已提交读:指定语句不能读取已由其他事务修改但尚未提交的数据   
  157. begin tran   
  158.     select * from ta    
  159.   
  160.   
  161.   
  162. 处理方法:   
  163. --------------------------------连接窗口2(善用会话设置:业务数据不断变化中,如销售查看当月时可用)   
  164.   
  165. SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED --设置会话未提交读:指定语句可以读取已由其他事务修改但尚未提交的行   
  166. begin tran   
  167.     select * from ta 
原文地址:https://www.cnblogs.com/wangbin/p/1257363.html