Adam Machanic : administrationhttp://www2.sqlblog.com/blogs/adam_machanic/archive/tags/administration/default.aspxTags: administrationenCommunityServer 2.1 SP2 (Build: 61129.1)A Gift of Script for 2008: Who's Active, What Are They Doing, and Who is Blocked?http://www2.sqlblog.com/blogs/adam_machanic/archive/2007/12/31/a-gift-of-script-for-2008-who-s-active-what-are-they-doing-and-who-is-blocked.aspxMon, 31 Dec 2007 22:32:00 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:4300Adam Machanic16http://www2.sqlblog.com/blogs/adam_machanic/comments/4300.aspxhttp://www2.sqlblog.com/blogs/adam_machanic/commentrss.aspx?PostID=4300<p>As with all of the blog posts I keep <i>meaning</i> to write -- I keep a list and given the infrequency with which I've been posting lately, it's getting quite large -- this script has been on the queue for quite some time. So here I find myself with a spare moment right on the cusp of the new year, and figured what better way to end the year than with a script that, at least for me, has been quite useful these last few months.</p><p>The driving force behind my writing this script is that I found myself endlessly calling <b>sp_who2 'active'</b> to see who was doing what on servers I needed to take a look at. Then I would have to sort through the results, and end up calling <b>DBCC INPUTBUFFER</b> to take a look at the SQL being used. This was a serious pain, and I finally caved a few months back and decided to end the madness once and for all with the help of some DMVs.</p><p>The following script primarily uses the <b>sys.dm_exec_requests</b> view, and finds all "active" requests -- i.e., those that are running, about to start running, or suspended. It also finds some other useful information, including the host name, login name, the start time of the batch, and whether or not the batch is currently blocked. In the outer query I use the <b>sys.dm_exec_sql_text</b> function to get the text of the SQL that all of the active requests are running, in addition to the SQL being run by the blocking sessions, if applicable. This way I don't have to do two lookups to chase down what's blocking what.</p><p>You'll notice that I use <b>FOR XML PATH</b> in the subqueries that pull the SQL text. This gives us a nice little bonus: instead of copying the text out of the cell in SSMS and pasting it somewhere else, you can simply click on it -- and it maintains whatever formatting, including white space and carriage returns, that it originally had. This is much, much nicer than getting the batch on a single line. The only problem is that certain characters, such as greater-than and less-than, get "entitized" when the text is converted to XML. This means that some queries won't be able to be run without a bit of editing. A small price to pay for nicer output, in my opinion. If anyone out there has a solution for the entitization, please let me know! The only way I know to solve it is to convert back to VARCHAR, and that defeats the whole purpose...</p><p>Anyway, thanks all for a great 2007. Here's to an even better 2008! Without further ado, the script:</p><blockquote>&nbsp;</blockquote><blockquote>SELECT<br>&nbsp;&nbsp;&nbsp; x.session_id,<br>&nbsp;&nbsp;&nbsp; x.host_name,<br>&nbsp;&nbsp;&nbsp; x.login_name,<br>&nbsp;&nbsp;&nbsp; x.start_time,<br>&nbsp;&nbsp;&nbsp; x.totalReads,<br>&nbsp;&nbsp;&nbsp; x.totalWrites,<br>&nbsp;&nbsp;&nbsp; x.totalCPU,<br>&nbsp;&nbsp;&nbsp; x.writes_in_tempdb,<br>&nbsp;&nbsp;&nbsp; (<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SELECT <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; text AS [text()]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM sys.dm_exec_sql_text(x.sql_handle)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FOR XML PATH(''), TYPE<br>&nbsp;&nbsp;&nbsp; ) AS sql_text,<br>&nbsp;&nbsp;&nbsp; COALESCE(x.blocking_session_id, 0) AS blocking_session_id,<br>&nbsp;&nbsp;&nbsp; (<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SELECT <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p.text<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SELECT <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MIN(sql_handle) AS sql_handle<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM sys.dm_exec_requests r2<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r2.session_id = x.blocking_session_id<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ) AS r_blocking<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CROSS APPLY<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SELECT <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; text AS [text()]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM sys.dm_exec_sql_text(r_blocking.sql_handle)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FOR XML PATH(''), TYPE<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ) p (text)<br>&nbsp;&nbsp;&nbsp; ) AS blocking_text<br>FROM<br>(<br>&nbsp;&nbsp;&nbsp; SELECT<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.session_id,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.host_name,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.login_name,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.start_time,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.sql_handle,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.blocking_session_id,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SUM(r.reads) AS totalReads,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SUM(r.writes) AS totalWrites,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SUM(r.cpu_time) AS totalCPU,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SUM(tsu.user_objects_alloc_page_count + tsu.internal_objects_alloc_page_count) AS writes_in_tempdb<br>&nbsp;&nbsp;&nbsp; FROM sys.dm_exec_requests r<br>&nbsp;&nbsp;&nbsp; JOIN sys.dm_exec_sessions s ON s.session_id = r.session_id<br>&nbsp;&nbsp;&nbsp; JOIN sys.dm_db_task_space_usage tsu ON s.session_id = tsu.session_id and r.request_id = tsu.request_id<br>&nbsp;&nbsp;&nbsp; WHERE r.status IN ('running', 'runnable', 'suspended')<br>&nbsp;&nbsp;&nbsp; GROUP BY<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.session_id,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.host_name,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.login_name,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.start_time,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.sql_handle,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.blocking_session_id<br>) x<br></blockquote><p>&nbsp;</p><p>Enjoy!<br></p><img src="http://www2.sqlblog.com/aggbug.aspx?PostID=4300" width="1" height="1">administrationDMVsScripts