Monday, October 27, 2008

[DBA] What's killing my server _right now_?

I'm trying to find/create some code that will tell me at a glance what's pummelling my servers. You know, the ones where you get phone calls saying "what the F is going on?!". This will tell you what current connections are using the most resources. The obvious improvement would be to do a WAITFOR to wait 5 seconds, then compare before/after.

Here's my first stab at it.

(remember to select the beginning to end - there's code hidden off to the sides of the blog columns - grrr.


SELECT
s.login_name,
[spid] = r.session_id,
[database] = DB_NAME(r.database_id),
r.start_time,
r.[status],
r.command,
r.wait_type, r.wait_time, r.wait_resource, r.cpu_time, r.reads,
r.writes, r.logical_reads, s.HOST_NAME, s.program_name,
s.client_interface_name, s.nt_user_name, s.original_login_name,
r.USER_ID, c.client_net_address, c.client_tcp_port,
/* add other interesting columns here */
[obj] = QUOTENAME(OBJECT_SCHEMA_NAME(t.objectid, t.[dbid]))
+ '.' + QUOTENAME(OBJECT_NAME(t.objectid, t.[dbid])),
t.[text]
FROM
sys.dm_exec_requests AS r
LEFT OUTER JOIN sys.dm_exec_connections c
ON c.session_id = r.session_id
INNER JOIN sys.dm_exec_sessions s
ON r.session_id = s.session_id
AND r.connection_id = c.connection_id
CROSS APPLY
sys.dm_exec_sql_text(r.[sql_handle]) AS t
WHERE
r.session_id <> @@SPID
AND r.session_id > 50 --system SPIDs are below 50
-- AND s.[host_name] NOT IN ('baking')
-- AND login_name not in ('the_baking_dba')
-- AND DB_NAME(r.database_id) <> 'pies'
ORDER BY logical_reads DESC, reads DESC --or you could use cpu_time

No comments: