SQLServer常⽤性能分析语句
--查看死锁情况
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
ORDERBY STR(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_,a.most_recent_sql_handle
from
sys.dm_exec_connections a
cross apply
sys.dm_exec_sql_st_recent_sql_handle) as SQL --也可⽤函数fn_get_sql通过most_recent_sql_handle得到执⾏语句where
a.Session_id in(57,58)
--查询锁类型
select进程q_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
sqlserver备份表语句,rsc_indid
from master..syslockinfo a leftjoin #t b q_q_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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论