sys.dm_exec_sessions
Returns one row per authenticated session on SQL Server. sys.dm_exec_sessions is a server-scope view that shows information about all active user connections and internal tasks. This information includes client version, client program name, client login time, login user, current session setting, and more. Use sys.dm_exec_sessions to first view the current system load and to identify a session of interest, and then learn more information about that session by using other dynamic management views or dynamic management functions.
一,重要参数
1,Status of the session. Possible values:
- Running - Currently running one or more requests
- Sleeping - Currently running no requests
- Dormant – Session has been reset because of connection pooling and is now in prelogin state.
- Preconnect - Session is in the Resource Governor classifier.
2,Request
last_request_start_time:Time at which the last request on the session began. This includes the currently executing request.
last_request_end_time:Time of the last completion of a request on the session
reads:Number of reads performed, by requests in this session, during this session
writes:Number of writes performed, by requests in this session, during this session.
logical_reads:Number of logical reads that have been performed on the session
3,事务
open_transaction_count:Number of open transactions per session.
transaction_isolation_level:
Transaction isolation level of the session:
- 0 = Unspecified
- 1 = ReadUncomitted
- 2 = ReadCommitted
- 3 = Repeatable
- 4 = Serializable
- 5 = Snapshot
4,其他
- Login
- set option
二,分析
1,Finding idle sessions that have open transactions
The following example finds sessions that have open transactions and are idle. An idle session is one that has no request currently running.
SELECT s.* FROM sys.dm_exec_sessions AS s WHERE EXISTS ( SELECT * FROM sys.dm_tran_session_transactions AS t WHERE t.session_id = s.session_id ) AND NOT EXISTS ( SELECT * FROM sys.dm_exec_requests AS r WHERE r.session_id = s.session_id );
2,查看系统中存在的Block
SELECT R.session_id AS BlockedSessionID , S.session_id AS BlockingSessionID , Q1.text AS BlockedSession_TSQL , Q2.text AS BlockingSession_TSQL , C1.most_recent_sql_handle AS BlockedSession_SQLHandle , C2.most_recent_sql_handle AS BlockingSession_SQLHandle , S.original_login_name AS BlockingSession_LoginName , S.program_name AS BlockingSession_ApplicationName , S.host_name AS BlockingSession_HostName FROM sys.dm_exec_requests AS R INNER JOIN sys.dm_exec_sessions AS S ON R.blocking_session_id = S.session_id INNER JOIN sys.dm_exec_connections AS C1 ON R.session_id = C1.most_recent_session_id INNER JOIN sys.dm_exec_connections AS C2 ON S.session_id = C2.most_recent_session_id CROSS APPLY sys.dm_exec_sql_text(C1.most_recent_sql_handle) AS Q1 CROSS APPLY sys.dm_exec_sql_text(C2.most_recent_sql_handle) AS Q2
提示:使用Connection的Most_recent_sql 来获取sql_handle,背后的实现原理是:Session使用数量不多的Connection Pool,存在一个connection轮流供多个Session使用的情况。
Appendix:
sys.dm_exec_connections:connection 是能够被多个session reuse的
most_recent_session_id:Represents the session ID for the most recent request associated with this connection. (SOAP connections can be reused by another session.)
most_recent_sql_handle:The SQL handle of the last request executed on this connection. The most_recent_sql_handle column is always in sync with the most_recent_session_id column.
参考Doc:
sys.dm_exec_sessions (Transact-SQL)
sys.dm_exec_connections (Transact-SQL)