SQL Server阻塞查询语句


SQL Server阻塞查询语句 

SQL Server的阻塞查询主要来自sys.sysprocesses。通常在处理时需要加入其它相关的视图或表,例如如sys.dm_exec_connectionssys.dm_exec_sql_text。通过如下几个语句的查询,可以找到阻塞的语句。


点击(此处)折叠或打开

  1. SELECT BL.SPID BLOCKING_SESSION,
  2.        BL.BLOCKED BLOCKED_SESSION,
  3.        ST.TEXT BLOCKEDTEXT
  4.   FROM (SELECT SPID, BLOCKED
  5.           FROM SYS.SYSPROCESSES A
  6.          WHERE BLOCKED > 0
  7.            AND NOT EXISTS (SELECT 1
  8.                   FROM SYS.SYSPROCESSES B
  9.                  WHERE BLOCKED > 0
  10.                    AND A.BLOCKED = B.SPID)
  11.         UNION
  12.         SELECT SPID, BLOCKED
  13.           FROM SYS.SYSPROCESSES
  14.          WHERE BLOCKED > 0) BL,
  15.        (SELECT T.TEXT, C.SESSION_ID
  16.           FROM SYS.DM_EXEC_CONNECTIONS C
  17.          CROSS APPLY SYS.DM_EXEC_SQL_TEXT(C.MOST_RECENT_SQL_HANDLE) T) ST
  18.  WHERE BL.BLOCKED = ST.SESSION_ID;



点击(此处)折叠或打开

  1. SELECT A.BLOCKING_SESSION_ID, A.WAIT_DURATION_MS, A.SESSION_ID, B.TEXT
  2.   FROM SYS.DM_OS_WAITING_TASKS A,
  3.        (SELECT T.TEXT, C.SESSION_ID
  4.           FROM SYS.DM_EXEC_CONNECTIONS C
  5.          CROSS APPLY SYS.DM_EXEC_SQL_TEXT(C.MOST_RECENT_SQL_HANDLE) T) B
  6.  WHERE A.SESSION_ID = B.SESSION_ID
  7.    AND A.BLOCKING_SESSION_ID IS NOT NULL;


包含阻塞与被阻塞的SQL脚本

点击(此处)折叠或打开

  1. SELECT BL.SPID BLOCKING_SESSION,
  2.        BL.BLOCKED BLOCKED_SESSION,
  3.        ST.TEXT BLOCKEDTEXT,
  4.        SB.TEXT BLOCKINGTEXT
  5.   FROM (SELECT SPID, BLOCKED
  6.           FROM SYS.SYSPROCESSES A
  7.          WHERE BLOCKED > 0
  8.            AND NOT EXISTS (SELECT 1
  9.                   FROM SYS.SYSPROCESSES B
  10.                  WHERE BLOCKED > 0
  11.                    AND A.BLOCKED = B.SPID)
  12.         UNION
  13.         SELECT SPID, BLOCKED
  14.           FROM SYS.SYSPROCESSES
  15.          WHERE BLOCKED > 0) BL,
  16.        (SELECT T.TEXT, C.SESSION_ID
  17.           FROM SYS.DM_EXEC_CONNECTIONS C
  18.          CROSS APPLY SYS.DM_EXEC_SQL_TEXT(C.MOST_RECENT_SQL_HANDLE) T) ST,
  19.        (SELECT T.TEXT, C.SESSION_ID
  20.           FROM SYS.DM_EXEC_CONNECTIONS C
  21.          CROSS APPLY SYS.DM_EXEC_SQL_TEXT(C.MOST_RECENT_SQL_HANDLE) T) SB
  22.  WHERE BL.BLOCKED = ST.SESSION_ID
  23.    AND BL.SPID = SB.SESSION_ID;


  查询死锁:

点击(此处)折叠或打开

  1. SELECT *
  2.   FROM MASTER ..SYSPROCESSES
  3.  WHERE DB_NAME(DBID) = '数据库名'
  4.    AND SPID <> @@SPID
  5.    AND DBID <> 0
  6.    AND BLOCKED > 0;








About Me

...............................................................................................................................

本文整理自网络

本文在itpubhttp://blog.itpub.net/26736162)、博客园http://www.cnblogs.com/lhrbest和个人微信公众号(xiaomaimiaolhr)上有同步更新

本文pdf小麦苗云盘地址:http://blog.itpub.net/26736162/viewspace-1624453/

● QQ群:230161599     微信群:私聊

联系我请加QQ好友(642808185),注明添加缘由

文章内容来源于小麦苗的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解

版权所有,欢迎分享本文,转载请保留出处

...............................................................................................................................

拿起手机使用微信客户端扫描下边的左边图片来关注小麦苗的微信公众号:xiaomaimiaolhr,扫描右边的二维码加入小麦苗的QQ群,学习最实用的数据库技术。

SQL Server阻塞查询语句SQL Server阻塞查询语句

 SQL Server阻塞查询语句  SQL Server阻塞查询语句

上一篇:Jquery 数组操作


下一篇:C#~异步编程再续~await与async引起的w3wp.exe崩溃-问题友好的解决