sqlserver查看正在执行的语句
SELECT
[Spid] = session_Id ,ecid ,[Database] = DB_NAME(sp.dbid) ,[User] =
nt_username ,[Status] = er.STATUS ,[Wait] = wait_type ,[Individual
Query] = SUBSTRING(qt.TEXT, er.statement_start_offset / 2, ( CASE
WHEN er.statement_end_offset = - 1 THEN LEN(CONVERT(NVARCHAR(MAX),
qt.TEXT)) * 2 ELSE er.statement_end_offset END -
er.statement_start_offset ) / 2) ,[Parent Query] = qt.TEXT ,Program
= program_name ,Hostname ,nt_domain ,start_time FROM
sys.dm_exec_requests er INNER JOIN sys.sysprocesses sp ON
er.session_id = sp.spid CROSS APPLY
sys.dm_exec_sql_text(er.sql_handle) AS qt WHERE session_Id > 50
AND session_Id NOT IN (@@SPID)
sqlserver查询消耗CPU最多的语句
select spid,cmd,cpu,physical_io,memusage,
(select top 1 [text] from ::fn_get_sql(sql_handle)) sql_text
from master..sysprocesses order by cpu desc,physical_io desc
sqlserver查看正在执行的sql锁阻塞,CPU占用高
SELECT TOP 10[session_id],[request_id],[start_time] AS '开始时间',[status] AS '状态',[command] AS '命令',dest.[text] AS 'sql语句', DB_NAME([database_id]) AS '数据库名',[blocking_session_id] AS '正在阻塞其他会话的会话ID',der.[wait_type] AS '等待资源类型',[wait_time] AS '等待时间',[wait_resource] AS '等待的资源',[dows].[waiting_tasks_count] AS '当前正在进行等待的任务数',[reads] AS '物理读次数',[writes] AS '写次数',[logical_reads] AS '逻辑读次数',[row_count] AS '返回结果行数'FROM sys.[dm_exec_requests] AS der INNER JOIN [sys].[dm_os_wait_stats] AS dows ON der.[wait_type]=[dows].[wait_type]CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest WHERE [session_id]>50 ORDER BY [cpu_time] DESCkill blocking_session_id查看占用CPU高的SQL
SELECT TOP 100 cpu_time,
dest.[text] AS 'sql语句'
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
查询SQLSERVER执行过的SQL记录
SELECT TOP 2000
--创建时间
QS.creation_time,
--查询语句
SUBSTRING(ST.text,(QS.statement_start_offset/2)+1,
((CASE QS.statement_end_offset WHEN -1 THEN DATALENGTH(st.text)
ELSE QS.statement_end_offset END - QS.statement_start_offset)/2) + 1
) AS statement_text,
--执行文本
ST.text,
--执行计划
QS.total_worker_time,
QS.last_worker_time,
QS.max_worker_time,
QS.min_worker_time
FROM
sys.dm_exec_query_stats QS
--关键字
CROSS APPLY
sys.dm_exec_sql_text(QS.sql_handle) ST
WHERE
QS.creation_time BETWEEN '2015-01-03 09:00:00' AND '2016-04-01 11:00:00'
AND ST.text LIKE 'select%'
ORDER BY
QS.creation_time DESC
相关参考:
SQLSERVER排查CPU占用高的情况 - 桦仔 - 博客园
如何快速发现和处理sqlserver数据库的死锁_数据库死锁查询和处理 sqlserver-CSDN博客
sql server 当前内存占用、阻塞查询,以及索引优化_sqlserver查看内存使用情况-CSDN博客