Overview

Dynamic management views and functions (DMVs) expose the engine’s live state. Most are cumulative since the last restart and are reset on restart, so sample them over time or persist them to a monitoring system.

Core concepts

QuestionDMV / source
What is running now, and what is it waiting for?sys.dm_exec_requests, sys.dm_exec_sessions, sys.dm_os_waiting_tasks
Who blocks whom?sys.dm_exec_requests.blocking_session_id, sys.dm_tran_locks (sqlserver-locking-blocking-deadlocks)
Top queries by CPU, reads, timesys.dm_exec_query_stats + sys.dm_exec_sql_text; history in sqlserver-query-store
Which indexes are used or never used?sys.dm_db_index_usage_stats
What indexes might help?sys.dm_db_missing_index_details and related (clues only)
Fragmentation, page densitysys.dm_db_index_physical_stats (expensive; use LIMITED)
File I/O latencysys.dm_io_virtual_file_stats
Memory, buffer pool, page life expectancysys.dm_os_performance_counters, sys.dm_os_buffer_descriptors
What is eating tempdb?sys.dm_db_file_space_usage, sys.dm_db_session_space_usage
Waitssys.dm_os_wait_stats (sqlserver-wait-stats)

Needs VIEW SERVER STATE (or VIEW SERVER PERFORMANCE STATE in 2022).

Worked example

Currently executing statements and their waits:

SELECT r.session_id, r.status, r.wait_type, r.wait_time, r.blocking_session_id,
       r.cpu_time, r.logical_reads, t.text
FROM sys.dm_exec_requests r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) t
WHERE r.session_id <> @@SPID;

Top cached statements by total logical reads:

SELECT TOP 10 qs.total_logical_reads, qs.execution_count,
       qs.total_logical_reads / qs.execution_count AS avg_reads,
       SUBSTRING(st.text, 1, 100) AS text_start
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
ORDER BY qs.total_logical_reads DESC;

Index usage on the test table (counts of seeks, scans, lookups and updates since start) shows which of its indexes actually serve queries.

Pitfalls and production notes

  • sys.dm_exec_query_stats only reflects plans still in cache; Query Store is the durable record.
  • Missing-index suggestions ignore existing similar indexes and write cost; consolidate them.
  • Page life expectancy has no universal “good” number; trend it.
  • Extended Events (not Profiler) is the supported way to trace; the system_health session is always on and holds deadlock graphs.
  • For incidents, sp_WhoIsActive (Adam Machanic) is a widely used community tool.
  • Export key metrics to Prometheus/Grafana or your APM for history and alerting; alert on blocking duration, log space, failed jobs, backup age, replica lag and tempdb use.

Practice questions

  1. Why might a heavy query not appear in dm_exec_query_stats?
  2. Which DMV shows I/O latency per file?
  3. How would you find an index that costs writes but is never read?

References

  • Microsoft Learn, Dynamic Management Views and Functions

sqlserver-wait-stats · sqlserver-query-store · sqlserver-agent-maintenance · pg-monitoring