I give performance presentations at many different events to all levels of SQL Server professionals. Over time I’ve noticed that some DBAs and developers have never looked at the dynamic management views (dmv’s) that are available within SQL Server starting with SQL Server 2005. These DMVs are useful means to determine what is going on with a particular SQL Server instance. So, in this post I am going to list a few of my very favorite DMVs as a reference for those who may be new to them.
Performance Tuning
These dynamic management views are what I use first when looking to any performance issues or I need to know what is going on right now with my SQL Server instance. Usage examples borrowed from docs.microsoft.com.
- SELECT login_name ,COUNT(session_id) AS session_count
- FROM sys.dm_exec_sessions
- GROUP BY login_name;
- SELECT c.session_id, c.net_transport, c.encrypt_option, c.auth_scheme, s.host_name, s.program_name,
- s.client_interface_name, s.login_name, s.nt_domain, s.nt_user_name, s.original_login_name, c.connect_time,
- s.login_time
- FROM sys.dm_exec_connections AS c
- JOIN sys.dm_exec_sessions AS s
- ON c.session_id = s.session_id
- WHERE c.session_id = @@SPID -- @@SPID returns your current session SPID
- USE master
- GO
- SELECT * FROM sys.dm_exec_requests
- WHERE session_id = 54;
- GO
- USE master
- GO
- SELECT * FROM sys.dm_os_wait_stats
- GO
sys.dm_os_performance_counters
- USE master
- GO
- SELECT * FROM sys.dm_os_performance_counters
- GO
Query Specific Tuning
When starting to look at query tuning you need to dive into query plans statements. These are what I used to find out what queries are running and to get to their query plans.
- SELECT TOP 5 query_stats.query_hash AS "Query Hash",
- SUM(query_stats.total_worker_time) / SUM(query_stats.execution_count) AS "Avg CPU Time",
- MIN(query_stats.statement_text) AS "Statement Text"
- FROM (SELECT QS.*,
- SUBSTRING(ST.text, (QS.statement_start_offset/2) + 1,
- ((CASE statement_end_offset
- WHEN -1 THEN DATALENGTH(ST.text)
- ELSE QS.statement_end_offset END
- - QS.statement_start_offset)/2) + 1) AS statement_text
- FROM sys.dm_exec_query_stats AS QS
- CROSS APPLY sys.dm_exec_sql_text(QS.sql_handle) as ST) as query_stats
- GROUP BY query_stats.query_hash
- ORDER BY 2 DESC;
- USE master
- GO
- SELECT *FROM sys.dm_exec_query_plan (your plan handle goes here)
- GO
- USE master; GO SELECT *FROM sys.dm_exec_cached_plans AS cpCROSS APPLY sys.dm_exec_query_plan(cp.plan_handle); GO
- -- acquire sql_handle
- SELECT sql_handle FROM sys.dm_exec_requests WHERE session_id = 59 -- modify this value with your actual spid
- -- pass sql_handle to sys.dm_exec_sql_text
- SELECT *
- FROM sys.dm_exec_sql_text(your plan handle goes here)
Index
- USE master
- GO
- SELECT * FROM sys.dm_db_index_usage_stats
sys.dm_db_missing_index_details
- USE master;
- GO
- SELECT * FROM sys.dm_db_missing_index_details
These are just a few of many that SQL Server has to offer. But if you are just starting out these are definitely DMVs you should take a look at and add to your arsenal for performance tuning and monitoring your SQL Servers. Lastly, one thing to keep in mind when reading the data from these queries. Like many other things inside SQL Server this data is a good as your last reboot or service restart.

Join the conversation! Your thoughts help the community grow.