5,446,542 members and growing! (19,270 online)
Email Password   helpLost your password?
Database » Database » General     Advanced

Dynamic Management Views [DMV] – A SQL Server 2005 Feature

By Hari Prasad K

The DMV’s; newly introduced in SQL Server 2005 gives database administrator about the current state of the SQL Server machine.
C++, SQLWin2003, Windows, Visual Studio, SQL Server, SQL 2005, DBA, Dev

Posted: 21 Dec 2006
Updated: 29 Dec 2006
Views: 24,058
Bookmarked: 10 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
11 votes for this Article.
Popularity: 4.37 Rating: 4.20 out of 5
2 votes, 18.2%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
9 votes, 81.8%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

The DMV’s; newly introduced in SQL Server 2005 gives database administrator information about the current state of the SQL Server machine. These values will help the administrator to diagnose problems and tune the server for optimal performance. DMV’s are designed to be used instead of system tables and various other functions provided in SQL Server 2000. In this article I will be explaining only about the frequently used DMV’s.

Two types of dynamic management views:-

  1. Server-scoped DMV: Stored in Master Database
  2. Database-scoped DMV: Specific to each database

Permission to Execute DMV [Security]

To query a Server scoped DMV the database user must have SELECT privilege on VIEW SERVER STATE and for database scoped DMV the user must have SELECT privilege on VIEW DATABASE STATE.

  • GRANT VIEW SERVER STATE to <Login>
  • GRANT VIEW DATABASE STATE to <User>

If you want to deny a user permission to query certain DMV’s you can use the DENY command to restrict access to specific DMV.

Getting Started

All the DMV’s exits in SYS schema and its name start with DM_. So when you need to query a DMV then you should prefix the view name with SYS. As an example if you need to see the total physical memory of the SQL Server machine; then execute the below TSQL command:-

SELECT 
(Physical_memory_in_bytes/1024.0)/1024.0 AS Physical_memory_in_Mb 
FROM 
sys.dm_os_sys_info

In this article I will be explaining some of the DMV’s which can be used frequently to understand the current behavior of SQL Server.

  1. SQL Server related [Hardware Resources] DMV
  2. Database related DMV
  3. Index related DMV
  4. Execution related DMV

1. SQL Server related DMV

This section details the DMV’s associated with SQL Server system. SQL DMV is responsible to manage server level resources specific to a SQL Server instance.

This section covers DMV’s related to OS, Disk and Memory

a. sys.dm_os_sys_info

This view returns the information about the SQL Server machine, available resources and the resource consumption.

This view returns information’s like:-

1. CPU Count: Number of logical CPUs in the server

2. Hyperthread-ratio: Ratio of logical and physical CPU’s

3. Physical_memory_in_bytes: Amount of physical memory available

4. Virtual_memory_in_bytes: Amount of virtual memory available

5. Bpool_commited: Commited physical memory in buffer pool

6. OS_Priority_class: Priority class for SQL Server process

7. Max_workers_thread: Maximum number of workers which can be created

b. sys.dm_os_hosts

This view returns all the hosts registered with SQL Server 2005. This view also provides the resouces used by each host.

  1. Name: Name of the host registered
  2. Type: Type of hosted component [SQL Native Interface/OLE DB/MSDART]
  3. Active_tasks_count: Number active tasks host a placed

4. Active_ios_count: I/O requests from host waiting

c. sys.dm_os_schedulers

Sys.dm_os_schedulers view will help you identify if there is any CPU bottleneck in the SQL Server machine. The number of runnable tasks is generally a nonzero value; a nonzero value indicates that tasks have to wait for their time slice to run. If the runnable task counts show high values then there is a symptom of CPU bottleneck.

SELECT 
scheduler_id,current_tasks_count,runnable_tasks_count 
FROM sys.dm_os_schedulers 
WHERE scheduler_id < 255

The above query will lists all the available schedulers in the SQL Server machine and the number of runnable tasks for each scheduler.

d. sys.dm_io_pending_io_requests

This dynamic view will return the I/O requests pending in SQL Server side. It gives you information’s like:-

1. Io_type: Type of pending I/O request.

2. Io_pending: Indicates whether the I/O request is pending or has been completed by

Windows.

3. Scheduler_address: Scheduler on which this I/O request was issued.

e. sys.dm_io_virtual_file_stats

This view returns I/O statistics for data and log files [MDF and LDF file]. This view is one of the commonly used views and will help you to identify I/O file level. This will return information’s like:-

1. Sample_ms: Number of milliseconds since the instance of SQL Server has

Started.

2. Num_of_reads: Number of reads issued on the file.

3. Num_of_bytes_read: Total number of bytes read on this file.

4. Io_stall_read_ms: Total time, in milliseconds, that the users waited for reads

Issued on the file.

5. Num_of_writes: Number of writes made on this file.

6. Num_of_bytes_written: Total number of bytes written to the file.

7. Io_stall_write_ms: Total time, in milliseconds, that users waited for writes to

be completed on the file.

8. Io_stall: Total time, in milliseconds, that users waited for I/O to be completed

9. Size_on_disk_bytes: Number of bytes used on the disk for this file.

f. sys.dm_os_memory_clerks

This DMV will help how much memory SQL Server has allocated through AWE.

SELECT 
SUM(awe_allocated_kb) / 1024 as [AWE allocated, Mb] 
FROM sys.dm_os_memory_clerks

The Same DMV can be used to get the memory consumption by internal components of SQL Server 2005.

SELECT TOP 10 type, 
SUM(single_pages_kb) as [SPA Mem, Kb] 
FROM sys.dm_os_memory_clerks 
GROUP BY type 
ORDER BY SUM(single_pages_kb) DESC

g. sys.dm_os_ring_buffers

This DMV uses RING_BUFFER_RESOURCE_MONITOR and gives information from resource monitor notifications to identify memory state changes. Internally, SQL Server has a framework that monitors different memory pressures. When the memory state changes, the resource monitor task generates a notification. This notification is used internally by the components to adjust their memory usage according to the memory state.

SELECT 
Record FROM sys.dm_os_ring_buffers 
WHERE ring_buffer_type = 'RING_BUFFER_RESOURCE_MONITOR'

The output of the above query will be in XML format. The output will help you detecting any low memory notification.

RING_BUFFER_OOM: Ring buffer oom contains records indicating server out-of-memory conditions.

SELECT
record FROM sys.dm_os_ring_buffers 
WHERE ring_buffer_type = 'RING_BUFFER_OOM'

2. Database Related DMV

This section details the DMV’s associated with SQL Server Databases. This DMV’s will help to identify database space usages, Partition usages, Session information usages, etc...

a. sys.dm_db_file_space_usage

This DMV provides the space usage information of TEMPDB database.

b. sys.dm_db_session_space_usage

This DMV provides the number of pages allocated and de-allocated by each session for the database

c. sys.dm_db_partition_stats

This DMV provides page and row-count information for every partition in the current database.

The below Query shows all counts for all partitions of all indexes and heaps in the MSDB database

USE MSDB;
GO
SELECT * FROM sys.dm_db_partition_stats;

The following query shows all counts for all partitions of Backup set table and its indexes

USE MSDB
GO
SELECT * FROM sys.dm_db_partition_stats 
WHERE object_id = OBJECT_ID('backupset');

d. sys.dm_os_performance_counters

Returns the SQL Server / Database related counters maintained by the server.

The below sample query uses the dm_os_performance_counters DMV to get the Log file usage for all databases in KB.

SELECT instance_name
,cntr_value 'Log File(s) Used Size (KB)'
FROM sys.dm_os_performance_counters 
WHERE counter_name = 'Log File(s) Used Size (KB)'

3. INDEX Related DMV

This section details the DMV’s associated with SQL Server Databases. This DMV’s will help to identify database space usages, Partition usages, Session information usages, etc...

a. sys.dm_db_index_usage_stats

This DMV is used to get useful information about the index usage for all objects in all databases. This also shows the amount of seeks and scan for each index.

SELECT object_id, index_id, user_seeks, user_scans, user_lookups 
FROM sys.dm_db_index_usage_stats 
ORDER BY object_id, index_id

All indexes which are not been used sofar in as database can be identified using below Query:-

SELECT object_name(i.object_id), 
i.name, 
s.user_updates, 
s.user_seeks, 
s.user_scans, 
s.user_lookups
from sys.indexes i 
left join sys.dm_db_index_usage_stats s 
on s.object_id = i.object_id and 
i.index_id = s.index_id and s.database_id = 5
where objectproperty(i.object_id, 'IsIndexable') = 1 and
s.index_id is null or
(s.user_updates > 0 and s.user_seeks = 0 
and s.user_scans = 0 and s.user_lookups = 0)
order by object_name(i.object_id)

Replace the Database_id with the database you are looking.

4. Execution Related DMV

Execution related DMV’s will provide information’s regarding sessions, connections, and various requests which are coming in to SQL Server.

a. sys.dm_exec_sessions

This DMV will give information on each session connected to SQL Server. This DMV is similar to running sp_who2 or querying Master..sysprocesses table.

SELECT
session_id,login_name,
last_request_end_time,cpu_time
FROM sys.dm_exec_sessions
WHERE session_id >= 51All user Sessions

b. sys.dm_exec_connections

This DMV shows all the connection to SQL Server. The below query uses sys.dm_exec_connections DMV to get connection information. This view returns one row for each user connection (Sessionid > =51).

SELECT
connection_id,
session_id,client_net_address,
auth_scheme
FROM sys.dm_exec_connections

c. sys.dm_exec_requests

This DMV will give details on what each connection is actually performing in SQL Server.

SELECT
session_id,status,
command,sql_handle,database_id
FROM sys.dm_exec_requests
WHERE session_id >= 51

d. sys.dm_exec_sql_text

This dynamic management function returns the text of a SQL statement given a SQL handle.

SELECT 
st.text
FROM
sys.dm_exec_requests r 
CROSS APPLY 
sys.dm_exec_sql_text(sql_handle) AS st
WHERE r.session_id = 51

Conclusion

Dynamic Management views (DMV) and Dynamic Management Functions (DMF) in SQL Server 2005 gives a transparent view of what is going on inside various areas of SQL Server. By using them we will be able to query the system for information about its current state in a much more effective and provide solutions much faster. DMV’s can be used to performance tune and troubleshooting server and queries. This article has shown an overview of what they are and how we can use them.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Hari Prasad K


SUMMARY

 Working as a Database Architect in US Technology [CMMI/PCMMI Level 5 Company] - WWW.USTRI.COM
 MVP in SQL Server (2 years continuously) with MCDBA Certificationin SQL 2000
 9 years of experience in MSSQL Server
 Extensive experience in SQL Server from version 6.0 to SQL 2000
 Active participation in Microsoft Public SQL Server groups and Trivandrum user group
 Contributing articles on SQL Server for MSDN India.
 Experience in Database design, Installation, Crash / Recovery, Replication, Logshipping, 24 X 7 production support, Migration, Upgrades, Performance Tuning, DTS

Occupation: Web Developer
Location: United States United States

Other popular Database articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralDMV ListmemberBeckyWalker18:16 2 Jan '07  
GeneralRe: DMV ListmemberHari Prasad K3:26 3 Jan '07  
QuestionTable, Stored Procedure, and User Function info?memberrobrich13:45 22 Dec '06  
AnswerRe: Table, Stored Procedure, and User Function info?memberHari Prasad K19:41 22 Dec '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 Dec 2006
Editor:
Copyright 2006 by Hari Prasad K
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project