惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
GRAHAM CLULEY
T
Tenable Blog
Know Your Adversary
Know Your Adversary
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Privacy International News Feed
S
Security Affairs
NISL@THU
NISL@THU
O
OpenAI News
Attack and Defense Labs
Attack and Defense Labs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Hacker News: Ask HN
Hacker News: Ask HN
Webroot Blog
Webroot Blog
Schneier on Security
Schneier on Security
S
SegmentFault 最新的问题
S
Schneier on Security
G
Google Developers Blog
V
V2EX
C
Check Point Blog
U
Unit 42
Google DeepMind News
Google DeepMind News
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
Recent Announcements
Recent Announcements
M
MIT News - Artificial intelligence
S
Secure Thoughts
博客园 - 司徒正美
Recorded Future
Recorded Future
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
K
Kaspersky official blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
AI
AI
博客园 - 聂微东
N
News and Events Feed by Topic
SecWiki News
SecWiki News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Vulnerabilities – Threatpost
P
Palo Alto Networks Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
Recent Commits to openclaw:main
Recent Commits to openclaw:main
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
The Hacker News
The Hacker News
The Last Watchdog
The Last Watchdog
Project Zero
Project Zero
W
WeLiveSecurity
博客园 - Franky

博客园 - 心冰之海

最佳的免费Linux杀毒软件有哪些 源代码管理工具TFS简介 信息安全岗位技能提升路径图 IDEA,mysql,mysql workbeanch 新手开发环境搭建教程 JVM-性能优化工具 MAT Spring Boot热更新技巧:节省90%重启时间 Java spring boot 银河麒麟系统V10安装mysql5.7 Keycloak 安装 使用 DBeaver 企业版将 SQL Server 数据库迁移至达梦 DM8 DBeaver 23.2 AI 智能体 DeepSeek本地化部署超简单,比装个office还简单 数据可视化分析平台 DataEase .NET 6应用程序适配国产银河麒麟V10系统随记 Linux上使用docker部署.net8项目详细教程 Nginx的安装并配置web服务 nginxUI 安装及学习 AI编程
SQL SERVER死锁查询,死锁分析,解锁,查询占用
心冰之海 · 2025-10-23 · via 博客园 - 心冰之海

From:  https://www.cnblogs.com/K-R-/p/18431639

简单点的处理方法:

1、查询死锁的表

select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName
from sys.dm_tran_locks where resource_type='OBJECT'

2、解锁

declare @spid int
Set @spid = 79 --锁表进程
declare @sql varchar(1000)
set @sql='kill '+cast(@spid as varchar)
exec(@sql)

 

专业点的处理方法:

1.查询死锁的表:

复制代码

复制代码

SELECT
    request_session_id spid,
    OBJECT_NAME(
        resource_associated_entity_id
    ) tableName
FROM
    sys.dm_tran_locks
WHERE
    resource_type = 'OBJECT'

复制代码

复制代码

2.分析被锁死的原因:

复制代码

复制代码

select t1.resource_type                                [资源锁定类型]
     , DB_NAME(resource_database_id)                as 数据库名
     , t1.resource_associated_entity_id                锁定对象
     , t1.request_mode                              as 等待者请求的锁定模式
     , t1.request_session_id                           等待者SID
     , t2.wait_duration_ms                             等待时间
     , (select TEXT
        from sys.dm_exec_requests r
               cross apply
             sys.dm_exec_sql_text(r.sql_handle)
        where r.session_id = t1.request_session_id) as 等待者要执行的SQL
     , t2.blocking_session_id                          [锁定者SID]
     , (select TEXT
        from sys.sysprocesses p
               cross apply
             sys.dm_exec_sql_text(p.sql_handle)
        where p.spid = t2.blocking_session_id
)                                                      锁定者执行语句
from sys.dm_tran_locks t1,
     sys.dm_os_waiting_tasks t2
where t1.lock_owner_address = t2.resource_address

复制代码

复制代码

3.解锁:

复制代码

复制代码

create Proc Sp_KillAllProcessInDB
@DbName VarChar(100)
as
if db_id(@DbName) = Null
begin
Print 'DataBase dose not Exist'
end
else
 
Begin
Declare @spId Varchar(30)
 
DECLARE TmpCursor CURSOR FOR
Select 'Kill ' + convert(Varchar, spid) as spId
from master..SysProcesses
where db_Name(dbID) = @DbName
and spId <> @@SpId
and dbID <> 0
OPEN TmpCursor
 
FETCH NEXT FROM TmpCursor
INTO @spId
 
WHILE @@FETCH_STATUS = 0
 
BEGIN
 
Exec (@spId)
 
FETCH NEXT FROM TmpCursor
INTO @spId
 
END
 
 
CLOSE TmpCursor
DEALLOCATE TmpCursor
 
end

复制代码

复制代码

4、查询SQL占用资源情况:

复制代码

复制代码

SELECT TOP 20
    total_worker_time/1000 AS [总消耗CPU 时间(ms)],execution_count [运行次数],
    qs.total_worker_time/qs.execution_count/1000 AS [平均消耗CPU 时间(ms)],
    last_execution_time AS [最后一次执行时间],max_worker_time /1000 AS [最大执行时间(ms)],
    SUBSTRING(qt.text,qs.statement_start_offset/2+1, 
        (CASE WHEN qs.statement_end_offset = -1 
        THEN DATALENGTH(qt.text) 
        ELSE qs.statement_end_offset END -qs.statement_start_offset)/2 + 1) 
    AS [使用CPU的语法], qt.text [完整语法],
    dbname=db_name(qt.dbid),
    object_name(qt.objectid,qt.dbid) ObjectName
FROM sys.dm_exec_query_stats qs WITH(nolock)
CROSS apply sys.dm_exec_sql_text(qs.sql_handle) AS qt
WHERE execution_count>1
ORDER BY total_worker_time DESC

复制代码

复制代码