Lesson 2 of 3 Query Performance Analysis

Dynamic Management Views for Performance

This lesson explains the DMVs used to see current activity, expensive cached queries, waits, resource use and index health.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify the DMVs for current requests, cached query statistics and waits.
  • Describe the Azure SQL Database resource statistics view.
  • Explain the limits of DMV data compared with Query Store.

Current activity

sys.dm_exec_requests lists running requests with their status, wait type, wait time, CPU, reads and blocking_session_id; joined with sys.dm_exec_sql_text(sql_handle) it shows the statement. sys.dm_exec_sessions lists sessions, including idle ones with open transactions, and sys.dm_os_waiting_tasks shows what each waiting task waits for.

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 AS r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS t
WHERE r.session_id <> @@SPID;
Match the pairs

Activity DMVs

Exam skills 2.2.3

Match each question with the DMV that answers it.

Log in to try this activity Interactive activities are free with an account.

Cached query statistics and waits

sys.dm_exec_query_stats aggregates CPU, duration, reads and executions for each cached plan since it was cached, and with sys.dm_exec_query_plan it returns the plan XML. Ordering by total CPU or reads finds the most expensive queries currently in cache, although the statistics disappear when plans leave the cache. Wait statistics explain where time goes: sys.dm_db_wait_stats in Azure SQL Database (sys.dm_os_wait_stats on SQL Server) aggregates waits such as PAGEIOLATCH (data reads), LCK_M (locks), CXPACKET (parallelism) and WRITELOG (log writes).

SELECT TOP (10) qs.total_worker_time / qs.execution_count AS avg_cpu_us,
       qs.execution_count, qs.total_logical_reads, st.text
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
ORDER BY qs.total_worker_time DESC;
Match the pairs

Wait types

Exam skills 2.2.3

Match each wait type with where time is going.

Log in to try this activity Interactive activities are free with an account.

Resource use and indexes

Other DMVs describe resource use and index health, as the following table lists.

DMVAnswers
sys.dm_db_resource_stats (Azure SQL Database)CPU, data IO, log IO and memory as a percentage of the service limit, every 15 seconds for an hour
sys.dm_db_index_usage_statsHow often each index is sought, scanned or updated
sys.dm_db_missing_index_details and related viewsIndexes the optimizer wished it had
sys.dm_db_index_physical_statsFragmentation and page density
sys.dm_tran_locksLocks currently held and requested
sys.dm_db_vector_indexesVector index state and maintenance
Match the pairs

Resource and index DMVs

Exam skills 2.2.3

Match each question with the DMV that answers it.

Log in to try this activity Interactive activities are free with an account.
Worked example

The database hits its CPU limit

An Azure SQL database reaches 100% CPU for 20 minutes each morning. The developer must find which queries consume the CPU while the problem is happening. Which DMVs should the developer query?

Confirm the resource limit with sys.dm_db_resource_stats, then list running requests with sys.dm_exec_requests and sys.dm_exec_sql_text ordered by CPU, and check sys.dm_exec_query_stats for the cached queries with the highest total CPU. To investigate yesterday's episode after the fact, Query Store is the better source because DMV statistics are lost when plans leave the cache.

Check yourself

Why can sys.dm_exec_query_stats miss yesterday's expensive query?

Show answer

Its statistics exist only while the plan is cached, so they vanish when the plan is evicted or recompiled; Query Store keeps history.

Exam Tips

Remember for the exam

  • What runs and waits now: sys.dm_exec_requests with sys.dm_exec_sql_text.
  • Expensive cached queries: sys.dm_exec_query_stats.
  • Azure SQL Database resource use against limits: sys.dm_db_resource_stats.
  • Unused or missing indexes: index usage stats and missing index DMVs.