![]() |
|

SQL Server backup compression with network fault tolerance and zero impact encryption
|
|
By: Tim Ford | Read Comments (8) | Related Tips: More > Dynamic Management Views and Functions |
Problem
I get so frustrated when trying to determine where to start when a user comes to me and tells me that "the database is slow". Launching Performance Monitor and running a trace in Profiler is inefficient. Unless I know what I'm looking for to begin with it's hard to narrow things down using those tools! Do you have any suggestions for quickly determining if I should be looking at CPU, Memory, or I/O issues without the needle v. haystack dilemma that comes with diving right in to Perfmon and Profiler?
Solution
There is definitely a better option to quickly determining where you should spend your performance tuning resources on when encountering a "slow database". It's also associated with my favorite SQL Server subject: Dynamic Management Views. There is a specific DMV that you can quickly query to determine if you're dealing with CPU, Memory, or Disk (I/O) issues: sys.dm_os_wait_stats. Every time SQL Server is forced to wait for a resource it records the wait. Yes, just like all your exes, SQL Server holds a grudge as well! You can query this listing of wait instance via the sys.dm_os_wait_stats DMV. The column list for that DMV is shown below:
I made mention above that the information is cumulative for this DMV. This information is held in cache. It will be lost/reset upon services restart. If you want to maintain history for this metadata you may want to consider persisting the information in a physical table. More about that later in this tip though.
So, what does the data from this DMV look like? Let's see some of the results based upon a simple SELECT *... query:
SELECT * FROM sys.dm_os_wait_stats; |
|
|
Looking at the sample output you'll see that results are returned for each wait_type, regardless of whether or not there were any cumulative tasks that waited on resources for that specific type. You can also see that lock types are included in the output of sys.dm_os_wait_stats. A general query of this type doesn't really tell us much though. It's the other things we can do with this DMV that are telling.
Glenn Berry, in his chapter on DMVs in the recent MVP Deep Dive Book, had a great query that utilizes sys.dm_os_wait_stats. I’ve taken that query and altered it slightly for my use to get a snapshot of waits in percentage form at the current point in time. By creating a common table expression to build a Waits table you can filter out irrelevant wait types and then look at a listing of just those waits that comprise the top N% (in this case 95%) of the waits on the SQL Server instance:
WITH Waits AS |
|
|
In this case you'll see that since the last SQL Server services restart, I'm primarily dealing with ASYNC_NETWORK_IO and OLEDB waits on my SQL Server instance. I know there to be issues with a specific application on my server that causes these waits due to performing row-by-row processing of SQL batch results sets returned to the application. The SOS_SCHEDULER_YIELD wait occurs whenever a task offers up its place in queue to allow another process to run in its stead. It is indicative of CPU issues that may need to be addressed. Specific waits will point you in the direction of where to focus your tuning resources because the cause for certain waits are attributed directly to CPU, memory, or I/O. The PAGEIOLATCH_... waits are indicative of I/O issues, as is WRITELOG. CXPACKET is an indicator of waits due to queries going parallel and running across a number of CPU cores. I strongly suggest following the Microsoft CSS Team's blog and reading through the number of entries associated with wait types for the continual evolution of precision tuning using sys.dm_os_wait_stats.
The important thing to remember is that the metrics for the columns returned when querying sys.dm_os_wait_stats is cumulative. Like all Dynamic Management Object information these values accumulate over time and are wiped-clean at a SQL Server service restart. This is why I mentioned persisting the information in a static table on a dedicated administrative database located on each managed SQL Server instance. You can then query the information just for the period between the last two stats collections. I use a SQL Server Agent job that runs hourly to persist the information to the static table. You could either run the code manually when desired or schedule as works best in your organization. The query is lightweight, not really too noticeable on a decently-sized server. The template script below will allow you to do just that. Note that the remaining scripts in this post use template notation. CTRL+SHIFT+M in SQL Server Management Studio will provide a form to enter parameter values that are correct for your environment.
|
|
The following information would be persisted to the physical table of your choosing. If using the defaults in the template it would be Foo.dbo.dm_os_wait_stats. Note how it creates and increment_id and datestamp for the data collected:

Then you can run a query similar to this and only view counts resulting from recent activity – not activity over days, months, weeks, etc (trailing columns removed from screen shot for space concerns):
|
|
|
|
Finally, you could re-tool the earlier top N% waits query to examine just the most recent wait stats activity from the persisted table as demonstrated below. You can utilize this process (building a CTE comparing deltas for just the activity collected since the last two polling periods) to retrofit any query you find online for analyzing results from sys.dm_os_wait_stats.
--wait_stats as percentage just for current collection period: |
|
|
Next Steps
| Wednesday, February 24, 2010 - 8:29:57 AM - unclebiguns | Read The Tip |
| Nice post. Since the stats are reset at a restart wouldn't you need to modify the queries that report on your persisted data to NOT subtract the previous entries if there was a server restart between the times you collected the data? You could use the start time for the default trace in sys.traces or the creation time for tempdb from sys.databases. | |
| Wednesday, February 24, 2010 - 4:57:45 PM - gmartinez | Read The Tip |
|
Have you tried this command: DBCC SQLPERF ('sys.dm_os_wait_stats', CLEAR);?
|
|
| Thursday, February 25, 2010 - 8:49:34 AM - timmer26 | Read The Tip |
|
Doh Jack! I do that with the other persisted metadata I've written about here. I'll change the script this weekend and repost. Thanks! |
|
| Thursday, February 25, 2010 - 8:53:12 AM - timmer26 | Read The Tip |
|
[quote user="gmartinez"] Have you tried this command: DBCC SQLPERF ('sys.dm_os_wait_stats', CLEAR);? [/quote] Yes, that is how your clear the wait stats DMV, however, typically you want to retain this info. I do use it though after making changes/tuning and cutting a final, persisted store of the metadata from the DMV to the physical table. Then, when reviewing the results from this DMV I am only looking at results since the tuning changes that have been made. |
|
| Thursday, February 25, 2010 - 4:04:44 PM - unclebiguns | Read The Tip |
| Not a big deal. I think I saw that in one of your earlier posts. Still a great post and something I need to start moving toward persisting this data and analyzing it. Need to become more proactive. Enjoying the Tips. | |
| Tuesday, March 02, 2010 - 5:09:30 PM - Ranga | Read The Tip |
|
Does adding this to the insert script ignore wait_types that do not have any impact ? where wait_time_ms > 0 |
|
| Wednesday, October 12, 2011 - 5:31:01 PM - Bennett | Read The Tip |
|
Awsome post!!! I love the idea of implementing a permanent table to capture and persist the wait statistics. I'm implementing it right now. One thing has me scratching my head. Is there a reason that increment_id is *not* an identity column? |
|
| Tuesday, May 14, 2013 - 11:38:06 AM - Laura | Read The Tip |
|
I have ran this WITH Waits AS
The output looks like this BROKER_TASK_STOP 439656.09 59.11 59.11
I do not know how to interpret this. Can you help me?
|
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |