--查看死锁情况
SELECTDISTINCT
'进程ID'=STR(a.spid, 4)
, '进程ID状态'=CONVERT(CHAR(10), a.status)
, '死锁进程ID'=STR(a.blocked, 2)
, '工作站名称'=CONVERT(CHAR(10), a.hostname)
, '执行命令的用户'=CONVERT(CHAR(10), SUSER_NAME(a.uid))
, '数据库名'=CONVERT(CHAR(10), DB_NAME(a.dbid))
, '应用程序名'=CONVERT(CHAR(10), a.program_name)
, '正在执行的命令'=CONVERT(CHAR(16), a.cmd)
, '登录名'= a.loginame
, '执行语句'= b.text
FROM master..sysprocesses a CROSS APPLY
sys.dm_exec_sql_text(a.sql_handle) b
WHERE a.blocked IN ( SELECT blocked
FROM master..sysprocesses )
-- and blocked <> 0
ORDERBYSTR(spid, 4)
--查连接住信息(spid:57、58)
select connect_time,last_read,last_write,most_recent_sql_handle
from sys.dm_exec_connections where session_id in(57,58)
--查看会话信息
select login_time,host_name,program_name,login_name,last_request_start_time,last_request_end_time
from sys.dm_exec_sessions where session_id in(57,58)
--查看阻塞正在执行的请求
select
session_id,blocking_session_id,wait_type,wait_time,wait_resource
from
sys.dm_exec_requests
where
blocking_session_id>0--正在阻塞请求的会话的 ID。如果此列是 NULL,则不会阻塞请求
/*
session_id,blocking_session_id,wait_type,wait_time,wait_resource
58 57 LCK_M_S 2116437 KEY: 6:72057594039435264 (020068e8b274)
*/
--查看正在执行的SQL语句
select
a.session_id,sql.text,a.most_recent_sql_handle
from
sys.dm_exec_connections a
cross apply
sys.dm_exec_sql_text(a.most_recent_sql_handle) as SQL --也可用函数fn_get_sql通过most_recent_sql_handle得到执行语句
where
a.Session_id in(57,58)
--查询锁类型
select 进程id=a.req_spid
,数据库=db_name(rsc_dbid)
,类型=case rsc_type when1then'NULL 资源(未使用)'
when2then'数据库'
when3then'文件'
when4then'索引'
when5then'表'
when6then'页'
when7then'键'
when8then'扩展盘区'
when9then'RID(行 ID)'
when10then'应用程序'
end
,对象id=rsc_objid
,对象名=b.obj_name
,rsc_indid
from master..syslockinfo a leftjoin #t b on a.req_spid=b.req_spid
----查看SA用户执行的SQL
SELECT '进程ID[SPID]'=STR(a.spid, 4)
, '进程状态'=CONVERT(CHAR(10), a.status)
, '分块进程ID'=STR(a.blocked, 2)
, '服务器名称'=CONVERT(CHAR(10), a.hostname)
, '执行用户'=CONVERT(CHAR(10), SUSER_NAME(a.uid))
, '数据库名'=CONVERT(CHAR(10), DB_NAME(a.dbid))
, '应用程序名'=CONVERT(CHAR(10), a.program_name)
, '正在执行的命令'=CONVERT(CHAR(16), a.cmd)
, '累计CPU时间'=STR(a.cpu, 7)
, 'IO'=STR(a.physical_io, 7)
, '登录名'= a.loginame
, '执行sql'= b.text
FROM master..sysprocesses a CROSS APPLY
sys.dm_exec_sql_text(a.sql_handle) b
WHERE blocked <>0OR a.loginame='sa'
ORDERBY spid
主要动态管理视图:
sys.sysprocesses(兼容sql2k)
sys.dm_exec_connections
sys.dm_exec_sessions
sys.dm_exec_requests
来源:http://www.cnblogs.com/ilovexiao/archive/2010/05/21/1740645.html