I am asked this question by Microsoft SQL Server customers again and again: “What dynamic management views (DMV) or dynamic management functions (DMF) should I use?” The short answer is, as many as you need. The detailed answer is, well, it depends. There are well over 150 DMV/DMFs spread across 20 some categories in 2012 now, and it is a daunting challenge trying to remember all these. I don’t think I have used all of these, I don’t think I ever will. The use of these really stems from what issues you are trying to deal with or what information you need from SQL Server. Since the use for most DMVs is scenarios based, I am listing here DMV/DMFs that I think every DBA should get familiar with. Now I am not saying this is an exhaustive list, but I find it a good starting point.
SQL Server 2012 Dynamic Management Views and Functions
SQL Server Operating System
SQL Server Operating System
Instead of just giving you random SQL statements against the DMV/DMFs listed above, I am going to walk through the DMV and build the corresponding SQL statement based on information I want to collect.
For starters we will look to answer the following question: who is connected to SQL Server? We have in particular two DMVs to give us that information; sys.dm_exec_connections and sys.dm_exec_sessions. The sys.dm_exec_connections returns information for only user connections, whereas sys.dm_exec_sessions returns information for both user connections and system sessions. There is one-to-one relationship between the two DMVs. When looking at just the sys.dm_exec_sessions we can identify the system sessions by looking at the session_id column; values greater than or equal to 51 are user connections and be low are system processes.
SELECT * FROM sys.dm_exec_connections
SELECT * FROM sys.dm_exec_sessions
My default DMV returns too much information, so I am going to re-write both statements to return the information I am most interested in.
SELECT c.session_id , c.auth_scheme , c.node_affinity , s.login_name , db_name(s.database_id) AS database_name , CASE s.transaction_isolation_level WHEN 0 THEN 'Unspecified' WHEN 1 THEN 'Read Uncomitted' WHEN 2 THEN 'Read Committed' WHEN 3 THEN 'Repeatable' WHEN 4 THEN 'Serializable' WHEN 5 THEN 'Snapshot' END AS transaction_isolation_level , s.status , c.most_recent_sql_handle FROM sys.dm_exec_connections c INNER JOIN sys.dm_exec_sessions s ON c.session_id = s.session_id
Note we are only getting user connection information; in case we needed details for system sessions we will have to change INNER JOIN to RIGHT OUTER JOIN instead.
Now we know who is connected, but we also want to know what are they doing? The STATUS field in the sys.dm_exec_sessions can tell us if the session is actively running something or is sleeping. Sleeping means the last task on the session has completed and is waiting for next batch of work (aka Idle Connection). And if it’s running we can find out what is the being run by the session, by diving into sys.dm_exec_requests; again I am going to expand Stmt3 to include new DMV now.
SELECT c.session_id , c.auth_scheme , c.node_affinity , r.scheduler_id , s.login_name , db_name(s.database_id) AS database_name , CASE s.transaction_isolation_level WHEN 0 THEN 'Unspecified' WHEN 1 THEN 'Read Uncomitted' WHEN 2 THEN 'Read Committed' WHEN 3 THEN 'Repeatable' WHEN 4 THEN 'Serializable' WHEN 5 THEN 'Snapshot' END AS transaction_isolation_level , s.status AS SessionStatus , r.status AS RequestStatus , CASE WHEN r.sql_handle IS NULL THEN c.most_recent_sql_handle ELSE r.sql_handle END AS sql_handle , r.cpu_time , r.reads , r.writes , r.logical_reads , r.total_elapsed_time FROM sys.dm_exec_connections c INNER JOIN sys.dm_exec_sessions s ON c.session_id = s.session_id LEFT JOIN sys.dm_exec_requests r ON c.session_id = r.session_id
In this article we looked at getting information on who is connected and what request they are running. In my next post I’ll talk about how to get the T-SQL Code they are running and even the execution plans for those statements.
Also available on OpsVault.com and WordPress Blog.
very good explanation. really like it
Kuch khas nahi ... Thik thik che bhaila
Could you please explain how data is stored inside a DMV? and How many possible rows that we can see inside a DMV
Kishor, DMV represent the memory structures with in SQL Server. Therefore there is no hard limit on number of rows that can be stored within DMV. For example, sys.dm_os_schedulers will return 1 row per visisual CPU + internal schedulers. sys.dm_os_nodes will return 1 row per numa node with in the server + 1 for internal numa node created for sql server. Only DMV that have limitation really are sys.dm_db_missing_index_*.
I hope I answered your question. Cheers! -- Mohit.