sql server查看当前正在执行的sql

#统计某类sql执行次数,并按总体cpu消耗时间降序排序

with a as
(
select er.session_id,db_name(er.database_id) as DBNAME,sy.last_batch AS '最后执行时间',
er.cpu_time ,er.total_elapsed_time/1000 as sum_elapsed_time_s,
CAST(csql.text AS varchar(8000)) AS CallingSQL ,sy.cmd as cmd,sy.status as status,sy.lastwaittype,sy.loginame as loginame,sy.hostname as hostname,
client_net_address  as client_ip,local_net_address '服务器的IP',er.reads,er.writes,er.row_count,
'kill '+cast(er.session_id as varchar(200)) AS 'KILL-SQL'
from master.sys.dm_exec_requests er WITH (NOLOCK)
       CROSS APPLY fn_get_sql (er.sql_handle) csql
left join sysprocesses sy on er.session_id =sy.spid
left join sys.dm_exec_connections dec on dec.session_id=sy.spid
where er.session_id >50
-- AND er.session_id ='56'
and loginame <>'' and CAST(csql.text AS varchar(8000)) like '%%'
)
select DBNAME,a.CallingSQL,count(*) cnt,sum(cpu_time) as sum_cpu_time,sum(sum_elapsed_time_s) as sum_elapsed_time_s,avg(cpu_time) as avg_cpu_time,avg(sum_elapsed_time_s) as avg_elapsed_time_s,
max(cmd) as cmd,max(status) as status,max(loginame) as loginname,max(hostname) as hostname,max(client_ip) as client_ip
from a
where a.CallingSQL not like '%select er.session_id,db_name(er.database_id) as DBNAME%'
group by a.CallingSQL,a.DBNAME
order by sum_cpu_time desc

输出结果示例:

相关推荐
weisian1516 分钟前
Redis篇--常见问题篇6--缓存一致性1(Mysql和Redis缓存一致,更新数据库删除缓存策略)
数据库·redis·缓存
中草药z1 小时前
【Spring】深入解析 Spring 原理:Bean 的多方面剖析(源码阅读)
java·数据库·spring boot·spring·bean·源码阅读
地球资源数据云1 小时前
全国30米分辨率逐年植被覆盖度(FVC)数据集
大数据·运维·服务器·数据库·均值算法
Ahern_2 小时前
Oracle 普通表至分区表的分区交换
大数据·数据库·sql·oracle
夜半被帅醒2 小时前
MySQL 数据库优化详解【Java数据库调优】
java·数据库·mysql
不爱学习的啊Biao2 小时前
【13】MySQL如何选择合适的索引?
android·数据库·mysql
破 风2 小时前
SpringBoot 集成 MongoDB
数据库·mongodb
Rverdoser3 小时前
MySQL-MVCC(多版本并发控制)
数据库·mysql
m0_748233643 小时前
SQL数组常用函数记录(Map篇)
java·数据库·sql
dowhileprogramming3 小时前
Python 中的迭代器
linux·数据库·python