30.06.2013 Views

Performance Tuning with SQL Server Dynamic Management Views

Performance Tuning with SQL Server Dynamic Management Views

Performance Tuning with SQL Server Dynamic Management Views

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 2: Connections, Sessions and Requests<br />

Identify idle sessions <strong>with</strong> orphaned transactions<br />

The following query joins sys.dm_exec_sessions to sys.dm_exec_requests and<br />

sys.dm_tran_session_transactions. The requests DMV will be discussed in<br />

more detail shortly, and the session_transactions DMV will be introduced fully in<br />

Chapter 4.<br />

The inner join to sys.dm_tran_session_transactions returns all sessions <strong>with</strong><br />

corresponding open transactions. By performing a left join to sys.dm_exec_requests<br />

and returning results where a NULL foreign key is encountered, we limit the results to<br />

only those sessions that have open transactions, yet no corresponding request associated<br />

<strong>with</strong> the session.<br />

SELECT des.session_id ,<br />

des.login_time ,<br />

des.last_request_start_time ,<br />

des.last_request_end_time ,<br />

des.host_name ,<br />

des.login_name<br />

FROM sys.dm_exec_sessions des<br />

INNER JOIN sys.dm_tran_session_transactions dtst<br />

ON des.session_id = dtst.session_id<br />

LEFT JOIN sys.dm_exec_requests der<br />

ON dtst.session_id = der.session_id<br />

WHERE der.session_id IS NULL<br />

ORDER BY des.session_id<br />

51

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!