Sql Dynamic Management View Cheatsheet


sql performance

This is a cheatsheet that was created while studying for the DP-300 Administering Microsoft Azure SQL Solutions certification.

Dynamic Management Views (DMVs) Returns internal data about the state of the database or the instance.

Grant Permissions to view Performance DMVs

-- Server scoped objects
GRANT VIEW SERVER STATE TO MyUser
-- Database scoped objects
GRANT VIEW DATABASE STATE TO MyUser
-- database configurations that override server configurations
sys.database_scoped_configurations

Resource contention and blocking

DMVs to collect info about page allocation specific to tempdb

Data that can only be captured from DMVs is data and transaction log file read/write latency as exposed inĀ sys.dm_os_volume_stats

Identify IO wait problems or if you suspect blocking caused by tempdb database allocation contention

Azure SQL only queries

Monitor cpu util associated with Azure SQL process for elastic pool by examining value of avg_cpu_percent column in sys.elastic_pool_resource_stats system catalog

Collect CPU usage and storage data usage statistics for a five-day period using sys.resource_stats ONLY AZURE SQL DATABASE

SELECT *
FROM sys.resource_stats
WHERE database_name = 'MyDb' 
	AND START_TIME > DATEADD(day, -5, GETDATE())
ORDER BY START_TIME DESC;

Query Performance

ALTER DATABASE SCOPED CONFIGURATION SET LAST_QUERY_PLAN_STATS = ON;

Identify additional indexes most likely to improve query performance: Azure SQL Only sys.dm_db_mising_index_group_stats**

Query to associate session ID with a windows thread id to monitor performance of the thread in Windows Performance Monitor

SELECT s.session_id, st.os_thread_id
FROM sys.dom_os_tasks AS S
INNER JOIN sys.dm_os_threads AS st
	ON s.worker_address = st.worker_address
WHERE st.session_id IS NOT NULL
ORDER BY st.session_id;
GO

Monitor Propagation latency

Monitor propagation latency on a read only replica (think transfer speed): sys.dm_database_replica_states and columns redo_rate and redo_queue_size

DMV to monitor geo-replication lag and last replication time of the transactions committed on the primary and hardened to the transaction log on the secondary

SELECT
	link_guid
	, partner_server
	, last_replication
	, replication_lag_sec
FROM sys.dm_geo_replication_link_status;

Monitor open transactions awaiting commit or rollback:

SELECT tst.session_id, [database_name] = db_name(s.database_id)
    , tat.transaction_begin_time
    , transaction_duration_s = datediff(s, tat.transaction_begin_time, sysdatetime()) 
    , transaction_type = CASE tat.transaction_type  WHEN 1 THEN 'Read/write transaction'
        WHEN 2 THEN 'Read-only transaction'
        WHEN 3 THEN 'System transaction'
        WHEN 4 THEN 'Distributed transaction' END
    , input_buffer = ib.event_info, tat.transaction_uow     
    , transaction_state  = CASE tat.transaction_state    
        WHEN 0 THEN 'The transaction has not been completely initialized yet.'
        WHEN 1 THEN 'The transaction has been initialized but has not started.'
        WHEN 2 THEN 'The transaction is active - has not been committed or rolled back.'
        WHEN 3 THEN 'The transaction has ended. This is used for read-only transactions.'
        WHEN 4 THEN 'The commit process has been initiated on the distributed transaction.'
        WHEN 5 THEN 'The transaction is in a prepared state and waiting resolution.'
        WHEN 6 THEN 'The transaction has been committed.'
        WHEN 7 THEN 'The transaction is being rolled back.'
        WHEN 8 THEN 'The transaction has been rolled back.' END 
    , transaction_name = tat.name, request_status = r.status
    , tst.is_user_transaction, tst.is_local
    , session_open_transaction_count = tst.open_transaction_count  
    , s.host_name, s.program_name, s.client_interface_name, s.login_name, s.is_user_process
FROM sys.dm_tran_active_transactions tat 
INNER JOIN sys.dm_tran_session_transactions tst  on tat.transaction_id = tst.transaction_id
INNER JOIN sys.dm_exec_sessions s on s.session_id = tst.session_id 
LEFT OUTER JOIN sys.dm_exec_requests r on r.session_id = s.session_id
CROSS APPLY sys.dm_exec_input_buffer(s.session_id, null) AS ib
ORDER BY tat.transaction_begin_time DESC;

Monitoring solutions to identify blocking sessions