1、查询当前正在执行的语句:
SELECT
der.[session_id],der.[blocking_session_id],
sp.lastwaittype,sp.hostname,sp.program_name,sp.loginame,
der.[start_time] AS '开始时间',
der.[status] AS '状态',
dest.[text] AS 'sql语句',
DB_NAME(der.[database_id]) AS '数据库名',
der.[wait_type] AS '等待资源类型',
der.[wait_time] AS '等待时间',
der.[wait_resource] AS '等待的资源',
der.[logical_reads] AS '逻辑读次数'
FROM sys.[dm_exec_requests] AS der
INNER JOIN master.dbo.sysprocesses AS sp ON der.session_id=sp.spid
CROSS APPLY sys.[dm_exec_sql_text](der.[sql_handle]) AS dest
--WHERE [session_id]>50 AND session_id<>@@SPID
ORDER BY der.[session_id]
GO
2、是否堵塞:
SELECT spid, blocked, waittime, waittype, waitresource
, p.dbid, cpu, physical_io, memusage, open_tran AS status
, login_time, last_batch, hostname, program_name, hostprocess
, loginame, cmd, text
FROM master.dbo.sysprocesses p
CROSS APPLY sys.dm_exec_sql_text(p.sql_handle) s
WHERE blocked > 0
OR spid IN (
SELECT blocked
FROM master.dbo.sysprocesses
WHERE blocked > 0
)
GO
3、是否有未提交事物:
SELECT spid, blocked, waittime, waittype, waitresource
, p.dbid, cpu, physical_io, memusage, open_tran
, status, login_time, last_batch, hostname, program_name
, hostprocess, loginame, cmd, text
FROM master.dbo.sysprocesses p
CROSS APPLY sys.dm_exec_sql_text(p.sql_handle) s
WHERE open_tran <> 0
GO
4、 死锁:
--drop table #deadlock
CREATE TABLE #deadlock(LogDate DATETIME,ProcessInfo VARCHAR(20),Text VARCHAR(2000))
go
INSERT INTO #deadlock EXEC xp_readerrorlog 0,1,'deadlock victim',NULL,'2016-07-20 14:00:00','2016-07-30','DESC'
go