返回顶部
首页 > 资讯 > 数据库 >Sql Server中通过sql命令获取cpu占用及产生锁的sql
  • 146
分享到

Sql Server中通过sql命令获取cpu占用及产生锁的sql

2024-04-02 19:04:59 146人浏览 独家记忆
摘要

获取sqlSERVER中产生锁的SQL语句 SELECT SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,((CASE statemen

获取sqlSERVER中产生的SQL语句

SELECT 
    SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,((CASE statement_end_offset  WHEN -1 THEN DATALENGTH(st.text)  ELSE qs.statement_end_offset END - qs.statement_start_offset)/2) + 1) as statement_text
FROM sys.dm_exec_query_stats as qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) as st where qs.sql_handle in (select distinct sql_handle from sys.dm_exec_requests where session_id in (SELECT request_session_id as Spid
FROM sys.dm_tran_locks l
LEFT JOIN sys.partitions p
ON l.resource_associated_entity_id = p.hobt_id
LEFT JOIN sys.indexes i
ON p.object_id = i.object_id
AND p.index_id = i.index_id
LEFT JOIN sys.objects o
ON p.object_id = o.object_id
LEFT JOIN sys.schemas s
ON o.schema_id = s.schema_id
LEFT JOIN sys.objects o2
ON l.resource_associated_entity_id = o2.object_id
LEFT JOIN sys.schemas s2
ON o2.schema_id = s2.schema_id
LEFT JOIN sys.databases db
ON l.resource_database_id = db.database_id
WHERE resource_database_id = DB_ID() and request_mode in ('X')
))

查询 某个时间点的,所有执行中的sql语句的CPU占用时间(倒排序

use master;SELECT [session_id], [cpu_time], [start_time], dest.[text] AS 'sql',  DB_NAME([database_id]) AS 'dbname', [row_count] FROM sys.[dm_exec_requests] AS der CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest WHERE [session_id]>50 ORDER BY [cpu_time] DESC

查询 不同sql语句产生阻塞的情况(主要是由于锁产生的等待)

use master;SELECT top 50 dest.[text] AS 'sql', wtt.blocking_session_id, wtt.wait_duration_ms, wtt.session_id FROM sys.dm_os_waiting_tasks wtt LEFT JOIN sys.dm_exec_requests req ON wtt.blocking_session_id = req.session_id CROSS APPLY sys.[dm_exec_sql_text](req.[sql_handle]) AS dest where wtt.blocking_session_id is not null and wtt.wait_duration_ms>2000 order by wait_duration_ms desc

到此这篇关于SQL Server中通过sql命令获取cpu占用及产生锁的sql的文章就介绍到这了,更多相关cpu占用及产生锁内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

您可能感兴趣的文档:

--结束END--

本文标题: Sql Server中通过sql命令获取cpu占用及产生锁的sql

本文链接: https://lsjlt.com/news/116965.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作