首页 > 数据库 >SQL SERVER 查询死锁

SQL SERVER 查询死锁

时间:2023-12-04 21:32:55浏览次数:43  
标签:Target File SessionName SERVER 死锁 file SQL event SELECT

DECLARE @SessionName SysName 
SELECT @SessionName = 'system_health'

IF OBJECT_ID('tempdb..#Events') IS NOT NULL BEGIN
    DROP TABLE #Events
END
 
DECLARE @Target_File NVarChar(1000)
    , @Target_Dir NVarChar(1000)
    , @Target_File_WildCard NVarChar(1000)
 
SELECT @Target_File = CAST(t.target_data as XML).value('EventFileTarget[1]/File[1]/@name', 'NVARCHAR(256)')
FROM sys.dm_xe_session_targets t
    INNER JOIN sys.dm_xe_sessions s ON s.address = t.event_session_address
WHERE s.name = @SessionName
    AND t.target_name = 'event_file'
 
SELECT @Target_Dir = LEFT(@Target_File, Len(@Target_File) - CHARINDEX('\', REVERSE(@Target_File))) 
 
SELECT @Target_File_WildCard = @Target_Dir + '\'  + @SessionName + '_*.xel'
 
--Keep this as a separate table because it's called twice in the next query.  You don't want this running twice.
SELECT DeadlockGraph = CAST(event_data AS XML)
    , DeadlockID = Row_Number() OVER(ORDER BY file_name, file_offset)
INTO #Events
FROM sys.fn_xe_file_target_read_file(@Target_File_WildCard, null, null, null) AS F
WHERE event_data like '<event name="xml_deadlock_report%'
 
;WITH Victims AS
(
    SELECT VictimID = Deadlock.Victims.value('@id', 'varchar(50)')
        , e.DeadlockID 
    FROM #Events e
        CROSS APPLY e.DeadlockGraph.nodes('/event/data/value/deadlock/victim-list/victimProcess') as Deadlock(Victims)
)
, DeadlockObjects AS
(
    SELECT DISTINCT e.DeadlockID
        , ObjectName = Deadlock.Resources.value('@objectname', 'nvarchar(256)')
    FROM #Events e
        CROSS APPLY e.DeadlockGraph.nodes('/event/data/value/deadlock/resource-list/*') as Deadlock(Resources)
)
SELECT *
FROM
(
    SELECT e.DeadlockID
        , TransactionTime = Deadlock.Process.value('@lasttranstarted', 'datetime')
        , DeadlockGraph
        , DeadlockObjects = substring((SELECT (', ' + o.ObjectName)
                            FROM DeadlockObjects o
                            WHERE o.DeadlockID = e.DeadlockID
                            ORDER BY o.ObjectName
                            FOR XML PATH ('')
                            ), 3, 4000)
        , Victim = CASE WHEN v.VictimID IS NOT NULL 
                            THEN 1 
                        ELSE 0 
                        END
        , SPID = Deadlock.Process.value('@spid', 'int')
        , ProcedureName = Deadlock.Process.value('executionStack[1]/frame[1]/@procname[1]', 'varchar(200)')
        , LockMode = Deadlock.Process.value('@lockMode', 'char(1)')
        , Code = Deadlock.Process.value('executionStack[1]/frame[1]', 'varchar(1000)')
        , ClientApp = CASE LEFT(Deadlock.Process.value('@clientapp', 'varchar(100)'), 29)
                        WHEN 'SQLAgent - TSQL JobStep (Job '
                            THEN 'SQLAgent Job: ' + (SELECT name FROM msdb..sysjobs sj WHERE substring(Deadlock.Process.value('@clientapp', 'varchar(100)'),32,32)=(substring(sys.fn_varbintohexstr(sj.job_id),3,100))) + ' - ' + SUBSTRING(Deadlock.Process.value('@clientapp', 'varchar(100)'), 67, len(Deadlock.Process.value('@clientapp', 'varchar(100)'))-67)
                        ELSE Deadlock.Process.value('@clientapp', 'varchar(100)')
                        END 
        , HostName = Deadlock.Process.value('@hostname', 'varchar(20)')
        , LoginName = Deadlock.Process.value('@loginname', 'varchar(20)')
        , InputBuffer = Deadlock.Process.value('inputbuf[1]', 'varchar(1000)')
    FROM #Events e
        CROSS APPLY e.DeadlockGraph.nodes('/event/data/value/deadlock/process-list/process') as Deadlock(Process)
        LEFT JOIN Victims v ON v.DeadlockID = e.DeadlockID AND v.VictimID = Deadlock.Process.value('@id', 'varchar(50)')
) X
ORDER BY DeadlockID DESC

SQL SERVER 查询死锁_SQL SERVER  死锁



标签:Target,File,SessionName,SERVER,死锁,file,SQL,event,SELECT
From: https://blog.51cto.com/u_2333657/8681731

相关文章

  • Django和sqlite3版本不匹配解决 Django-django.core.exceptions.ImproperlyConfigured
    1.修改django源文件配置2升级sqlite下载sqlite3wgethttps://www.sqlite.org/2019/sqlite-autoconf-3270200.tar.gz 解压并安装sqlite3tar-zxvfsqlite-autoconf-3270200.tar.gzcdsqlite-autoconf-3270200./configure--prefix=/usr/localmake&&makeinstall......
  • 4.1 配置Mysql与注册登录模块(上)
    点击查看sql代码createdatabasekob;usekob;createtableuser(idint,usernamevarchar(100),passwordvarchar(100));mysql>showtables;+---------------+|Tables_in_kob|+---------------+|user|+---------------+1rowinset(0.00sec)......
  • mysql8.1 安装
    1、下载8.1.0 2、创建目录,并解压#cd/usr/local#mkdirmysql8#cdmysql8#wgethttp...#tar-xvfmysql-8.1.0-1.el8.x86_64.rpm-bundle.tar 3、安装mysql安装时,只要安装6个包就行,因为有依赖关系,必须按照顺序安装:#rpm-ivhmysql-community-common-8.1.0-1.el8.x......
  • MySQL 配置选项和变量间的关系
    MySQL变量MySQL中的变量主要分为两大类,用户变量和系统变量。1、用户变量用户变量是用户自定义的变量,用户变量以@符号开头,通过set或者select可以给用户变量赋值。用户变量是属于会话级别的变量,变量的值只在当前会话中有效,当会话结束时,这些变量的值就会丢失。例如:SET@myVar......
  • 虚拟机mysql
    importjava.sql.*;publicclassMysqltest{staticfinalStringDRIVER="com.mysql.jdbc.Driver";staticfinalStringDB="jdbc:mysql://192.168.88.161:3306/bigfly";//DatabaseauthstaticfinalStringUSER="root";......
  • python连接mysql数据库
    说明:1.如果你使用的是其他数据库,例如PostgreSQL,你可以使用psycopg2库来连接和获取数据库数据。使用方法类似,只需要根据你的实际情况修改连接参数和SQL语句即可。2.首先确保本地数据库可以查询到数据,比如:若没有登陆SVN,本地数据库无法查询数据,那么python代码也会执行失败 一、......
  • MySQL系列之读写分离架构——Atlas介绍、安装配置、Atlas功能测试、生产用户要求、Atl
    文章目录1.Atlas介绍2.安装配置3.Atlas功能测试4.生产用户要求5.Atlas基本管理6.自动分表7.关于读写分离建议1.Atlas介绍Atlas是由Qihoo360,Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目。它是在mysql-proxy0.8.2版本的基础上,对其......
  • mybatis sql查询后,返回回来的字段顺序变了;在项目中通过mybatis查询数据库,同样的查询
    问题描述:过程就不看了直接上结果查询语句中的字段顺序信息和返回的字段信息不一致如图:realSql是查询语句,result是查询结果查询语句中的字段顺序信息和返回的字段信息不一致解决方案:转载地址这里复制一份防删......
  • runsql
    CustomSqlMapper.xml<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.runsh......
  • mysql和Oracle 查询某个时间之内的数据
    查询最近一个月的数据mysqlSELECTid,title,endtimefromclassroomwhereyears=2022anddate_sub(CURDATE(),interval1month)<endtime;DATE_SUB()函数从日期减去指定的时间间隔,本文是从当前时间减去一个月。CURDATE()函数表示当前年月日,NOW()函数表示当前年月......