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

输出结果示例:

相关推荐
敲敲敲-敲代码4 分钟前
【SQL实验】视图操作(菜单操作和命令操作)
数据库·sql·sqlserver
2401_8570262333 分钟前
Spring Boot技术在实验室信息管理中的应用
数据库·spring boot·php
"追风者"1 小时前
mysql数据库(六)pymysql、视图、触发器、存储过程、函数、流程控制、数据库连接池
数据库·mysql
guokanglun1 小时前
数据库视图
数据库
远歌已逝1 小时前
Oracle维护控制文件
数据库·oracle
liuxin334455661 小时前
科研实验室的数字化转型:Spring Boot系统
数据库·spring boot·php
guokanglun1 小时前
数据库视图-多表
数据库
时间_已被注册1 小时前
Oracle - 多区间按权重取值逻辑 ,分时区-多层级-取配置方案(二)
数据库·oracle
HilariousDog1 小时前
Mysql数据库1——基本原理和基础操作
数据库·mysql·oracle
小小不董2 小时前
Oracle OCP认证考试考点详解082系列22
linux·运维·服务器·数据库·oracle·dba