<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlserver.ro/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>How-To Articles</title><link>http://sqlserver.ro/forums/19/ShowForum.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Re: Dynamic Management Views and Functions – Pain-free Introduction</title><link>http://sqlserver.ro/forums/thread/201.aspx</link><pubDate>Sat, 30 Sep 2006 07:40:34 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:201</guid><dc:creator>xmldeveloper</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/201.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=19&amp;PostID=201</wfw:commentRss><description>Some feedback for this article: &lt;A href="http://drsql.spaces.live.com/blog/cns!80677FB08B3162E4!900.entry"&gt;http://drsql.spaces.live.com/blog/cns!80677FB08B3162E4!900.entry&lt;/A&gt;</description></item><item><title>Dynamic Management Views and Functions – Pain-free Introduction</title><link>http://sqlserver.ro/forums/thread/90.aspx</link><pubDate>Sat, 16 Sep 2006 10:42:59 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:90</guid><dc:creator>xmldeveloper</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/90.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=19&amp;PostID=90</wfw:commentRss><description>&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;FONT face=Arial size=2&gt;In 1993 the alternative rock group, 4 Non Blondes, were climbing the charts all over the world screaming at the top of their lungs “What’s going on?”. Almost 12 years later, the SQL Server team came with the answer – the Dynamic Management Views and Functions. If you care what’s going on with your server, read on and I’ll try in 10 minutes of your time to get you started. &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;FONT face=Arial size=2&gt;Dynamic Management Views and Functions expose internal state of SQL Server and also statistical data. Don’t be scared by this phrase. In plain English, you can see through DMVs current sessions, requests, locks, transactions, memory allocation, &lt;B&gt;tempdb&lt;/B&gt; usage, index usage and so on. This is so called dynamic metadata that is not persisted physically on disk and lives somewhere in your server memory. You can guess from that what happens if your server gets restarted.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;FONT face=Arial size=2&gt;Let’s see them at work by checking the list of the current session. You will find the output similar with the output of &lt;B&gt;sp_who&lt;/B&gt; or &lt;B&gt;sp_who2&lt;/B&gt; stored procedures (or &lt;B&gt;sysprocesses&lt;/B&gt; virtual table):&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;B&gt;&lt;FONT face=Arial size=2&gt;Example:&lt;BR&gt;SELECT &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s.session_id&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , DB_NAME(r.database_id) as DBName&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , s.login_name&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , r.blocking_session_id as BlkBy&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , r.open_transaction_count&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , r.status&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , s.host_name&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , s.program_name&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , r.command&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , (SELECT SUBSTRING(text, statement_start_offset/2, (CASE WHEN statement_end_offset = -1 THEN LEN(CONVERT(nvarchar(MAX), text)) * 2 ELSE statement_end_offset END -statement_start_offset)/2) FROM sys.dm_exec_sql_text(sql_handle)) AS QueryText &lt;BR&gt;FROM&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.dm_exec_sessions s JOIN &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.dm_exec_requests r ON s.session_id = r.session_id&lt;BR&gt;WHERE&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s.is_user_process = 1&lt;/FONT&gt;&lt;/B&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;FONT face=Arial size=2&gt;Let me explain this first example more detailed. You already met two DMVs (&lt;B&gt;sys.dm_exec_sessions&lt;/B&gt;, &lt;B&gt;sys.dm_exec_requests)&lt;/B&gt; and one DMF (&lt;B&gt;sys.dm_exec_sql_text&lt;/B&gt;). The first information you can get from this objects come from their name. They belong to the &lt;B&gt;sys&lt;/B&gt; schema (like other system objects), then the &lt;B&gt;dm&lt;/B&gt; prefix is common for all DMVs (and DMFs). The prefix is followed by a category that groups related DMVs – &lt;B&gt;exec&lt;/B&gt; in this particular case for execution related DMVs. The last part of the name will tell you what the DMV does, in this example you will get a list of current sessions and associated requests (and the Query Text respectively from the &lt;B&gt;sys.dm_exec_sql_text&lt;/B&gt; DMF).&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;FONT face=Arial size=2&gt;The rows returned by the query look almost identical with the output of &lt;B&gt;sp_who&lt;/B&gt; stored procedure so it won’t be a mystery if you are familiar with SQL Server 2000 but I do have some remarks here. The former &lt;B&gt;SPID&lt;/B&gt; (Server Process ID) became the &lt;B&gt;session id&lt;/B&gt; and to get only the user sessions you have to use the &lt;B&gt;is_user_process&lt;/B&gt; column (the old approach – testing for spids greater than 51 is no longer guaranteed). For more information about these DMVs and all the others that will follow please refer to Books Online as I am trying only to whet your appetite here.&amp;nbsp; &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;FONT face=Arial size=2&gt;Before I move on, I will specify one more use of the &lt;B&gt;sys.dm_exec_requests&lt;/B&gt; DMV – progress reporting for some DBCC commands such as &lt;B&gt;DBCC CHECKDB&lt;/B&gt;, &lt;B&gt;DBCC CHECKFILEGROUP&lt;/B&gt; and &lt;B&gt;DBCC CHECKTABLE&lt;/B&gt;. You have two columns for that - the &lt;B&gt;percent_complete&lt;/B&gt; column (for the percent of work completed) and the &lt;B&gt;command&lt;/B&gt; column (for reporting the current phase of the DBCC command). To test this feature, use the following example:&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;FONT size=2&gt;&lt;FONT face=Arial&gt;&lt;B&gt;Example:&lt;BR&gt;&lt;/B&gt;-- run this in one window&lt;BR&gt;-- of SQL Server Management Studio&lt;BR&gt;-- or Query Analyzer&lt;/FONT&gt;&lt;/FONT&gt;&lt;B&gt;&lt;BR&gt;&lt;FONT face=Arial size=2&gt;DBCC CHECKDB ('AdventureWorks')&lt;BR&gt;GO&lt;BR&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;FONT face=Arial size=2&gt;-- run this in another window&lt;/FONT&gt;&lt;B&gt;&lt;BR&gt;&lt;FONT face=Arial size=2&gt;SELECT &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s.session_id&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,r.percent_complete &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,r.command&lt;BR&gt;FROM &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.dm_exec_sessions s join &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.dm_exec_requests r on s.session_id = r.session_idsession_id&lt;BR&gt;WHERE &lt;BR&gt;s.is_user_process = 1&lt;/FONT&gt;&lt;/B&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;FONT face=Arial size=2&gt;Optionally you can improve the example by including a specific session id in your WHERE clause.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;FONT face=Arial size=2&gt;Another DMV that I want to introduce to you is &lt;B&gt;sys.dm_os_memory_clerks&lt;/B&gt;. It displays the memory allocation for server components. &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;FONT face=Arial size=2&gt;Just run a &lt;B&gt;SELECT * FROM sys.dm_os_memory_clerks&lt;/B&gt; to get a feel of it. In time if you investigate further you will know that this DMV is just the top of the iceberg and if you need to you can get the memory allocation at the object level. More than that, you can peek at internal memory notifications and so on. Anyway, don’t worry about that for now.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;FONT face=Arial size=2&gt;How about indexes? The existing DMVs (and when I say DMVs I mean also DMFs) can tell you if your indexes are used and how, their fragmentation and even if your performance can benefit from additional indexes. The next example will display the fragmentation of your indexes from AdventureWorks database:&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;B&gt;&lt;FONT face=Arial size=2&gt;Example:&lt;BR&gt;USE AdventureWorks;&lt;BR&gt;GO&lt;BR&gt;SELECT &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OBJECT_NAME(s.object_id) Table_Name&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,i.[name] Index_Name&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,avg_fragmentation_in_percent&lt;BR&gt;FROM &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.dm_db_index_physical_stats(DB_ID(N'AdventureWorks'),NULL,NULL,NULL,'SAMPLED') s&lt;BR&gt;JOIN &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.indexes i ON i.[object_id]=s.[object_id] &lt;BR&gt;AND &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i.index_id=s.index_id&lt;BR&gt;WHERE &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OBJECTPROPERTY(s.[object_id],'IsUserTable')=1&lt;/FONT&gt;&lt;/B&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;FONT face=Arial size=2&gt;My last example will concentrate on performance troubleshooting. I found out in my work that the Pareto's Principle (the 80-20 Rule) really works. In the performance area if I will tune 20 percent of the queries that run on my server I will gain the maximum effect (that 80 percent improvement). But how to find that 20 percent of queries? With a DMV of course:&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;B&gt;&lt;FONT face=Arial size=2&gt;Example:&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SQ.*&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,(SELECT SUBSTRING(text, statement_start_offset/2, (CASE WHEN statement_end_offset = -1 THEN LEN(CONVERT(nvarchar(MAX), text)) * 2 ELSE statement_end_offset END -statement_start_offset)/2)) AS QueryText&lt;BR&gt;FROM&lt;BR&gt;( &lt;BR&gt;SELECT TOP 20 PERCENT&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sql_handle&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,SUM(total_worker_time) AS Total_CPU_Time&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,SUM(execution_count) AS Total_Execution_Count&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,COUNT(*) AS&amp;nbsp; Nmb_Statements&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,MIN(statement_start_offset) AS statement_start_offset&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,MIN(statement_end_offset) AS statement_end_offset&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.dm_exec_query_stats&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GROUP BY sql_handle&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ORDER BY Total_CPU_Time DESC&lt;BR&gt;) AS SQ&lt;BR&gt;CROSS APPLY sys.dm_exec_sql_text(SQ.sql_handle) AS T&lt;/FONT&gt;&lt;/B&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;FONT face=Arial size=2&gt;Don’t close this page yet – there’s an explanation for the above query. I find out first in a sub-query the most executed offending queries for me by means of the total CPU time used. &amp;nbsp;I also display the number of times they were executed because it does matter and also the query text. &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;FONT face=Arial size=2&gt;I will stop for now and I will continue in future articles (and hopefully in a book). If you are not convinced by now that you should at least consider using them, I’ll try one more approach. Think at your server as being a car. What do you do if something seems to be wrong? You open the hood, don’t you? For SQL Server the equivalent is the DMVs. You may find a work-around to avoid them in some cases but why would you do that? So pop-up that hood and open Books Online and see what those DMVs are capable of. &amp;nbsp;Thank you!&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN class=bodytext&gt;&lt;FONT face=Arial size=1&gt;Published in Culminis Compass&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;</description></item></channel></rss>