Category Archives: DMVs

The SQL Server 2005/2008 Performance Statistics script

By David Pless – david.pless@microsoft.com

In this article we are going to build on what we discussed in the ‘Utilizing the SQLDiag Tool’ article and discuss an add-in which can provide some very useful information called the ‘SQL Server 2005/2008 Performance Statistics’ scripts. SQLTWIST simply refers to it as the ‘PerfStats scripts’.

The ‘PerfStats scripts’ collects data for SQL Nexus to analyze. It also produces a text output with some very useful information. We will discuss the text output in this article. First we must download the ‘PerfStats script’ and discuss how it works. We will cover SQLNexus in a future article.

Where to download the ‘SQL Server 2005/2008 Performance Statistics’ script
You can download the PerfStatsScript2005.zip package for SQL Server 2005, the PerfStats2008.zip for SQL Server 2008, or the PerfStatsScript2008R2.zip for SQL Server 2008 R2 respectively.

To download click on the link below:
http://sqlnexus.codeplex.com/Wiki/Info.aspx?title=Sql2005PerfStatsScript&action=Info

Once it is downloaded, all you need to do is unzip the download to a local disk on a SQL Server 2005, 2008 or 2008 R2 machine and run ‘StartSQLDiagTrace.cmd‘, ‘StartSQLDiagTrace2008.cmd‘, ‘StartSQLDiagTrace2008R2.cmd‘ or any other batch file depending on what what you need.

How does it Work?

The batch scripts call SQLDiag with a custom template. In the custom templates, there is a section called CustomDiagnostics  where some SQL scripts are called to produce the Perfstats script output. The contents of the ‘PerfStats Script’ for 2008 R2is shown below. The XML documents replace the SQLDiag templates found in the following directory: C:\Program Files\Microsoft SQL Server\100\Tools\Binn\ 

Below is the PerfStats download contents before executing one of the batch files

Just as we discussed in the previous SQLDiag article, the general XML file does not capture statement level data and the SHOWPLAN output that the detailed template does. The detailed template has slightly higher overhead than general profiler trace events. But it is useful if you want to identify statement level events. For example, if you have identified a stored procedure consuming high CPU you may want to focus on individual statements within that stored procedure.

The SQL Replay template is used to create a SQL trace output to replay it again.

“SQL Server Profiler features a multi-threaded playback engine that can simulate user connections and SQL Server Authentication. Replay is useful to troubleshoot an application or process problem. When you identify the problem and implement corrections, run the trace that found the potential problem against the corrected application or process. Then, replay the original trace and compare results.”

- BOL. 

Below is a comparison of the PerfStats detail and the replay templates.

Below is the CustomDiagnostics section which all of the templates have: 

              <CustomDiagnostics>
                <CustomGroup name=”MSInfo” enabled=”true” />
                <CustomTask enabled=”true” groupname=”MsInfo” taskname=”MSINFO32″ type=”Utility” point=”Startup” wait=”OnlyOnShutdown” cmd=”get_msinfo.cmd %server% &quot;%output_path%&quot;” />
                <CustomTask enabled=”true” groupname=”MsInfo” taskname=”FibreChannelInfo” type=”Utility” point=”Startup” wait=”No” cmd=”get_fcinfo.cmd &gt; &quot;%output_name%.txt&quot; 2&gt;&amp;1″ />
                <CustomTask enabled=”true” groupname=”MsInfo” taskname=”RegHardwareDesc” type=”Reg_Query” point=”Startup” wait=”OnlyOnShutdown” cmd=”HKLM\HARDWARE\DESCRIPTION” />
                <CustomGroup name=”SQL 2008 Perf Stats” enabled=”true” />
                <CustomTask enabled=”true” groupname=”SQL 2008 Perf Stats” taskname=”SQL 2008 Perf Stats Script” type=”TSQL_Script” point=”Startup” wait=”No” cmd=”SQL_2008_Perf_Stats.sql” pollinginterval=”0″ />
                <CustomTask enabled=”true” groupname=”SQL 2008 Perf Stats” taskname=”RDTSCTest” type=”Utility” point=”Startup” wait=”No” cmd=”RDTSCTestLoop.cmd &gt; &quot;%output_name%.OUT&quot; 2&gt;&amp;1″ pollinginterval=”0″ />
                <CustomTask enabled=”true” groupname=”SQL 2008 Perf Stats” taskname=”SQL 2008 Perf Stats Snapshot” type=”TSQL_Script” point=”Startup” wait=”No” cmd=”SQL_2008_Perf_Stats_Snapshot.sql” pollinginterval=”0″ />
                <CustomTask enabled=”true” groupname=”SQL 2008 Perf Stats” taskname=”SQL 2008 Perf Stats Snapshot” type=”TSQL_Script” point=”Shutdown” wait=”No” cmd=”SQL_2008_Perf_Stats_Snapshot.sql” pollinginterval=”0″ />
              </CustomDiagnostics>

The batch files execute the SQL statements and SQLDiag with the new templates. Below is an example of the StartSQLDiagDetailed_Trace2008R2 batch file.

@REM  To register the collector as a service, open a command prompt, change to this
@REM  directory, and run:
@REM
@REM     SQLDIAG /R /I “%cd%\SQLDiagPerfStats_Trace.XML” /O “%cd%\SQLDiagOutput” /P
@REM
@REM  You can then start collection by running “SQLDIAG START” from Start->Run, and
@REM  stop collection by running “SQLDIAG STOP”.

@rem the command below sets sqldiag.exe path.  if your installation is different, adjust accordingly
@rem sql 2008 sqldiag.exe will be able to capture multiple platforms.
@rem any sqldiag will be able to detect 32 bit or 64 bit instances
set SQLDIAGCMD=C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLdiag.exe
“%SQLDIAGCMD%” /I “%cd%\SQLDiagPerfStats_Detailed_Trace2008.XML” /O “%cd%\SQLDiagOutput” /P

All we need to do to begin a capture is to execute one of the PerfStats batch scripts.

PerfStats Collection

Similar to SQLDiag, the PerfStats script will collect blocking information, profiler trace data (each batch file will capture different events and StartSQLDiagNoTrace.cmd doesn’t capture any trace data), Perfmon, msinfo32, Windows event logs (system, security and application logs), and more. It is important to remember that the different batch files capture Profiler trace events differently, though everything else mentioned above will be captured exactly the same.

What makes the PerfStats scripts unique is the performance DMV data collected via the SQL2005PerfStats.sql and SQL2008PerfStats.sqlscripts. These scripts harvest DMV data startup, since SQL Server has been running, and shutdown which will show performance data during the capture. The 2008 version also captures procedure and trigger data with query_hash and query_plan_hash grouping.

When you start the batch file to collect the PerfStats data you will notice some new files created and a new directory called SQLDiagOutput. In this directory the usual SQLDiag data will be captured along with the PerfStats output.

In my example, I kicked off the StartSQLDiagDetailed_Trace2008R2.cmdbatch file. You will know that the PerfStats script is working when you see the following listed in the SQLDiag collection process:

Once you have reproduced your issue if working on a reactive problem or captured enough data for a proactive baseline click CTRL + Cto shutdown SQLDiag gracefully. If you close the command prompt window any other way, you will lose valuable data.

The Output

In this output directory look for the __SQL_2008_Perf_Stats_Snapshot_Startup.OUT and __SQL_2008_Perf_Stats_Snapshot_Shutdown.OUT files. In these files there is the Top N Query Plan Statisticssection which gives a ranking of the most expensive SQL Server statements by CpuRank, PhysicalReadsRank, and DurationRank. There are also columns for  usecounts, size_in_kb, tot_cpu_ms, tot_duration_ms, total_physical_reads, total_logical_writes, and total_logical_reads.  

There is a section for Missing Indexes which lists the Missing Indexes based on the improvement measurement. In these files there is the the current database options conviently documented. There is also statistics date and rowmodctr for indexes in all databases. This statistics information can give you some idea of which indexes have a high amount data change which might require special statistics maintenance.

There is the Resource Governor output new to SQL Server 2008. There is also sys.dm_database_encryption_keys, sys.dm_os_loaded_modules, and sys.dm_server_audit_status output.

At the end of the file is more SQL Server performance information:

  • Top 10 CPU consuming procedures
  • Top 10 CPU consuming triggers
  • Top 10 CPU by query_hash
  • Top 10 logical reads by query_hash
  • Top 10 elapsed time by query_hash
  • Top 10 CPU by query_plan_hash and query_hash
  • Top 10 logical reads by query_plan_hash and query_hash
  • Top 10 elapsed time  by query_plan_hash and query_hash

Again, the query_hash and query_hash_plan information is only for SQL Server 2008 and above. The PerfStats script is a very beneficial addition to SQLDiag, it does not replace using DMVs, but can make ease of SQLDiag via templates even more powerful for reactive efforts as well as capturing a baseline of your SQL Server environment.

References:

SQL Nexus
http://sqlnexus.codeplex.com

SQL Server 2005/2008 Performance Statistics collection scripts
http://sqlnexus.codeplex.com/wikipage?title=Sql2005PerfStatsScript&referringTitle=Home

Utilizing the SQLDiag Tool
http://sqltwist.com/archives/date/2011/01

UPDATE:The SQLDiag Configuration Tool
http://sqltwist.com/archives/98

SQL Server 2008 R2 SP1 – Improvements Part 1

By David Pless – dpless@microsoft.com

Changes to sys.dm_exec_query_stats

In a previous article we discussed the ‘Mother of all DMV Queries’, aptly named because of the ability to
get so much information by simply changing the order by columns and mapping to two Dynamic Management Functions
sys.dm_exec_sql_text and sys.dm_exec_query_plan.

In this article we are going to look at a variation of this query again to look at the improvements introduced
in ‘SQL Server 2008 R2 SP1′

Summary
The sys.dm_exec_query_stats is critical because it allows us to see the following information for each statement
within a query plan. Below is a summary of the columns available in the sys.dm_exec_query_stats output.

sys.dm_exec_query_stats columns SQL Server 2008 R2 Pre-SP1

  • sql_handle- Identifies a SQL statement used to get the statement from sys.dm_exec_sql_text
  • plan_handle- Identifies a SQL plan used to get the statement from sys.dm_exec_query_plan     
  • statement_start_offset – Used to identify a statement within a batch
  • statement_end_offset – Used to identify a statement within a batch         
  • creation_time     
  • last_execution_time        

 SQL Server Resources:

  • plan_generation_num – The number of times a statement was recompiled
  • execution_count – The number of times a statement was executed

 There are total, min, max, and last columns for the following resources:

  • worker_time (CPU)
  • physical reads
  • logical writes
  • logical reads
  • clr time
  • elapsed_time (Duration)

 The following can be used to uniquely identify SQL Server text and plans which was introduced in SQL Server 2008.

  • query_hash
  • query_plan_hash              

For more information on sys.dm_exec_query_stats see the following:
http://msdn.microsoft.com/en-us/library/ms189741.aspx

 New in Service Pack 1 is the following columns:

  • total_rows
  • last_rows
  • min_rows
  • max_rows

This information can be very useful when trying to track volume between executions including the average number
of records. A common need for this is in a reporting / data processing environment where the volume of records
can easily slow down reporting. When you are trying to discern what is slowing down a report, it is very useful to be
able to track the number of records over time.

Below is an example of how these new columns added to SQL Server 2008 SP1 (CTP) can be leveraged:

SELECT
TOP 10
qs.*, (total_rows/execution_count) AS AVG_ROWS, qp.query_plan, st.text
FROM
sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) as qp
–SPECIFY A TIMEFRAME TO GATHER CACHED DATA
–WHERE DATEDIFF(hour, last_execution_time, getdate()) < 1 — change hour time frame
–CHANGE THE ORDER BY TO GET THE TOP 10 for DIFFERENT RESOURCES
ORDER BY (total_rows/execution_count) DESC;

The Mother of All DMV Queries

By David Pless – dpless@microsoft.com

I call the included query below the ‘Mother of all DMV Queries’ because it is very powerful as it can grab the most expensive statements and their execution plans for several different resources such as CPU, duration, execution count, and logical reads, and more.

The included query is a building block query that can be found in many different articles, but is a critical tool for addressing the top consumers of any SQL Server environment post SQL Server 2005. Our included query uses the sys.dm_exec_query_stats DMV (Dynamic Management View) to get the bulk of the information. Other functions are used to grab the text and the execution plan.

From Books Online the sys.dm_exec_query_stats DMV, ‘returns aggregate performance statistics for cached query plans. The view contains one row per query statement within the cached plan, and the lifetime of the rows are tied to the plan itself. When a plan is removed from the cache, the corresponding rows are eliminated from this view.’ This means that once the SQL Server is recycled or the plans are removed from cache, then the references and hence the information is also not accessible.

Note: Though there is not an unlimited amount of time available to access this information, there are several methods available that can be used to harvest this data such as the DMVStats tool (http://sqldmvstats.codeplex.com/) from Codeplex.com or by using the Management Data Warehouse (MDW) that was introduced in SQL Server 2008. We will cover both of these technologies in a future set of articles.

Reference: sys.dm_exec_query_stats (BOL)
http://msdn.microsoft.com/en-us/library/ms189741.aspx

By using the sql_handle you can access the actual text of the SQL Server statement and by using the plan_handle you can access the Showplan in XML format for the batch specified by the plan handle. The plan specified by the plan handle can either be cached or currently executing. In order to get this information you have to utilize two Dynamic Management Functions and utilize the ‘CROSS APPLY’ method to get the text and the plan for each sys.dm_exec_sql_text reference.

References: sys.dm_exec_sql_text (BOL)
http://msdn.microsoft.com/en-us/library/ms181929.aspx

sys.dm_exec_text_query_plan (BOL)
http://msdn.microsoft.com/en-us/library/bb326654.aspx

Though we are only showing one example of obtaining the sql_handle, you can also obtain the sql_handle from the following dynamic management objects:

  • sys.dm_exec_query_stats
  • sys.dm_exec_requests
  • sys.dm_exec_cursors
  • sys.dm_exec_xml_handles
  • sys.dm_exec_query_memory_grants
  • sys.dm_exec_connections

We will cover using some of these Dynamic Management Views in a future article.

Note: For batches, the SQL handles are hash values based on the SQL text. For database objects such as stored procedures, triggers or functions, the SQL handles are derived from the database ID, object ID, and object number. plan_handle is a hash value derived from the compiled plan of the entire batch.

There are some situations where the query plan cannot be obtained by leveraging the sys.dm_exec_query_plan Dynamic Management Function, for example consider the points below.

Some SQL statements are not cached, such as bulk operation statements or statements containing string literals larger than 8 KB in size. XML Showplans for such statements cannot be retrieved by using sys.dm_exec_query_plan unless the batch is currently executing because they do not exist in the cache.

If a Transact-SQL batch or stored procedure contains a call to a user-defined function or a call to dynamic SQL, for example using EXEC (string), the compiled XML Showplan for the user-defined function is not included in the table returned by sys.dm_exec_query_plan for the batch or stored procedure. Instead, you must make a separate call to sys.dm_exec_query_plan for the plan handle that corresponds to the user-defined function.

When an ad hoc query uses simple or forced parameterization, the query_plan column will contain only the statement text and not the actual query plan. To return the query plan, call sys.dm_exec_query_plan for the plan handle of the prepared parameterized query. You can determine whether the query was parameterized by referencing the sql column of the sys.syscacheobjects view or the text column of the sys.dm_exec_sql_text dynamic management view.

Additionally, the number of due to a limitation in the number of nested levels allowed in the xml data type, sys.dm_exec_query_plan cannot return query plans that meet or exceed 128 levels of nested elements. In earlier versions of SQL Server, this condition prevented the query plan from returning and generates error 6335.

In SQL Server 2005 Service Pack 2 and later versions, the query_plan column returns NULL. You can use the sys.dm_exec_text_query_plan dynamic management function to return the output of the query plan in text format.

The sys.dm_exec_text_query_plan dynamic management function was introduced in SQL Server 2005 Service Pack 2. This DMF was is also leveraged in the Performance Dashboard reports that can be found here: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=1d3a4a0d-7e0c-4730-8204-e419218c1efc&DisplayLang=en

We will discuss the SQL Server 2005 Performance Dashboard in a future article.

For more information on leveraging the sys.dm_exec_sql_text, see the following article:
Reference: sys.dm_exec_sql_text (BOL)
http://msdn.microsoft.com/en-us/library/ms181929.aspx

Example:

From this single DMV below we can get the statement and plan for the top consumers driven simply by the ORDER BY clause. Averages can be obtained in the SELECT and ORDER BY by dividing by the execution count.

SELECT TOP 10
    (total_logical_reads/execution_count) AS avg_logical_reads,
    (total_logical_writes/execution_count) AS avg_logical_writes,
    (total_physical_reads/execution_count) AS avg_phys_reads,
    (total_worker_time/execution_count) AS avg_cpu_over_head,
total_logical_reads, total_logical_writes, total_physical_reads,
total_worker_time, execution_count, total_elapsed_time AS Duration, plan_generation_num AS num_recompiles,
statement_start_offset AS stmt_start_offset,
    (SELECT SUBSTRING(text, statement_start_offset/2 + 1,
        (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 query_text,
      (SELECT query_plan FROM sys.dm_exec_query_plan(plan_handle)) AS query_plan
FROM sys.dm_exec_query_stats a
–JUST CHANGE THE ORDER BY TO GET THE OTHER RESOURCES
ORDER BY (total_logical_reads + total_logical_writes)/execution_count DESC

Leveraging sys.dm_io_virtual_file_stats

By David Pless - dpless@microsoft.com

One of the DMVs I try to utilize on any engagement where customers are complaining about disk issues is the sys.dm_io_virtual_file_stats DMV where you can look at the IO stalls for both reads and writes. The sys.dm_io_virtual_file_stats DMV will show an IO Stall when any wait occurs to access a physical data file. IO Stalls are recorded at the file level and you can also obtain the IO Stalls at the database level directly out of the DMV.

By getting this information it is very easy to ORDER BY io_stall_read_ms, io_stall_write_ms, or by io_stall which is an accumulation of reads and writes.

One addition step I have made in the script below is mapping to the sys.master_files catalog view and using the substring function to get the physical disk drive letter. You will now be able to see IO Stall activity at the file, database, and the drive letter. You can then use Reporting Services or simply use Excel to get a quick view of which of these is absorbing most of the IO Stall impact.

If you use Excel 2007, one of the interesting strategies is to use the Chart Advisor from Live Labs.
http://www.officelabs.com/projects/chartadvisor/Pages/default.aspx

This analysis can help make decisions around table partitioning and potentially file and index placement. Of course, this will all depend on the customer’s SAN and other constraints.

Note: Mount points will make getting the drive letter less effective. If you are using mount points then just ignore the drive letter column.

SELECT a.io_stall, a.io_stall_read_ms, a.io_stall_write_ms, a.num_of_reads,
a.num_of_writes,
–a.sample_ms, a.num_of_bytes_read, a.num_of_bytes_written, a.io_stall_write_ms,
( ( a.size_on_disk_bytes / 1024 ) / 1024.0 ) AS size_on_disk_mb,
db_name(a.database_id) AS dbname,
b.name, a.file_id,
db_file_type = CASE
                   WHEN a.file_id = 2 THEN ‘Log’
                   ELSE ‘Data’
                   END,
UPPER(SUBSTRING(b.physical_name, 1, 2)) AS disk_location
FROM sys.dm_io_virtual_file_stats (NULL, NULL) a
JOIN sys.master_files b ON a.file_id = b.file_id
AND a.database_id = b.database_id
ORDER BY a.io_stall DESC

For those looking at disk issues, I have pasted the general guidance on the avg. reads/sec and avg. writes/sec values for perfmon. By using the script above and the guidance here on perfmon, you should be able to take the next steps in addressing disk performance issues with your customers.

I/O Bottlenecks

SQL Server performance depends heavily on the I/O subsystem. Unless your database fits into physical memory, SQL Server constantly brings database pages in and out of the buffer pool. This generates substantial I/O traffic. Similarly, the log records need to be flushed to the disk before a transaction can be declared committed. And finally, SQL Server uses TempDB for various purposes such as to store intermediate results, to sort, to keep row versions and so on. So a good I/O subsystem is critical to the performance of SQL Server.

Access to log files is sequential except when a transaction needs to be rolled back while access to data files, including TempDB, is randomly accessed. So as a general rule, you should have log files on a separate physical disk than data files for better performance. The focus of this paper is not how to configure your I/O devices but to describe ways to identify if you have I/O bottleneck. Once an I/O bottleneck is identified, you may need to reconfigure your I/O subsystem.
If you have a slow I/O subsystem, your users may experience performance problems such as slow response times, and tasks that abort due to timeouts.
You can use the following performance counters to identify I/O bottlenecks. Note, these AVG values tend to be skewed (to the low side) if you have an infrequent collection interval. For example, it is hard to tell the nature of an I/O spike with 60-second snapshots. Also, you should not rely on one counter to determine a bottleneck; look for multiple counters to cross check the validity of your findings.

  • Physical Disk Object: Avg. Disk Queue Length represents the average number of physical read and write requests that were queued on the selected physical disk during the sampling period. If your I/O system is overloaded, more read/write operations will be waiting. If your disk queue length frequently exceeds a value of 2 during peak usage of SQL Server, then you might have an I/O bottleneck.
  • Avg. Disk Sec/Read is the average time, in seconds, of a read of data from the disk. Any number:
    • Less than 10 ms – very good
    • Between 10 – 20 ms – okay
    • Between 20 – 50 ms – slow, needs attention
    • Greater than 50 ms – Serious I/O bottleneck
  • Avg. Disk Sec/Write is the average time, in seconds, of a write of data to the disk. Please refer to the guideline in the previous bullet.
  • Physical Disk: %Disk Time is the percentage of elapsed time that the selected disk drive was busy servicing read or write requests. A general guideline is that if this value is greater than 50 percent, it represents an I/O bottleneck.
  • Avg. Disk Reads/Sec is the rate of read operations on the disk. You need to make sure that this number is less than 85 percent of the disk capacity. The disk access time increases exponentially beyond 85 percent capacity.
  • Avg. Disk Writes/Sec is the rate of write operations on the disk. Make sure that this number is less than 85 percent of the disk capacity. The disk access time increases exponentially beyond 85 percent capacity.

When using above counters, you may need to adjust the values for RAID configurations using the following formulas.

  • Raid 0 — I/Os per disk = (reads + writes) / number of disks
  • Raid 1 — I/Os per disk = [reads + (2 * writes)] / 2
  • Raid 5 — I/Os per disk = [reads + (4 * writes)] / number of disks
  • Raid 10 — I/Os per disk = [reads + (2 * writes)] / number of disks

For example, you have a RAID-1 system with two physical disks with the following values of the counters.

  • Disk Reads/sec -  80
  • Disk Writes/sec – 70
  • Avg. Disk Queue Length – 5

In that case, you are encountering (80 + (2 * 70))/2 = 110 I/Os per disk and your disk queue length = 5/2 = 2.5 which indicates a border line I/O bottleneck.”

Reference(s):

Troubleshooting Performance Problems in SQL Server 2008
http://msdn.microsoft.com/en-us/library/dd672789.aspx

(BOL) – sys.dm_io_virtual_file_stats (Transact-SQL)
http://msdn.microsoft.com/en-us/library/ms190326.aspx

David Pless, Senior Premier Field Engineer
http://blogs.msdn.com/dpless/