This month the SQLskills team is presenting a series of blog posts aimed at helping Accidental/Junior DBAs ‘keep the SQL Server lights on’. It’s a little taster to let you know what we cover in ourÂ Immersion Event for The Accidental/Junior DBA, which we presentÂ several times each year. You can find all the other posts in this series atÂ http://www.SQLskills.com/help/AccidentalDBA. Enjoy!
For the last set of posts in our Accidental DBA series weâ€™re going to focus on troubleshooting, and I want to start with Wait Statistics.Â When SQL Server executes a task, if it has to wait for anything â€“ a lock to be released from a page, a page to be read from disk into memory, a write to the transaction log to complete â€“ then SQL Server records that wait and the time it had to wait.Â This information accumulates, and can be queried using the sys.dm_os_wait_stats DMV, which was first available in SQL Server 2005.Â Since then, the waits and queues troubleshooting methodology has been a technique DBAs can use to identify problems, and areas for optimizations, within an environment.
If you havenâ€™t worked with wait statistics, I recommend starting with Paulâ€™s wait stats post, and then working through the SQL Server Performance Tuning Using Wait Statistics: A Beginners GuideÂ whitepaper.
Viewing Wait Statistics
If you run the following query:
SELECT * FROM sys.dm_os_wait_stats ORDER BY wait_time_ms DESC;
You will get back output that isnâ€™t that helpful, as you can see below:
It looks like FT_IFTS_SCHEDULER_IDLE_WAIT is the biggest wait, and SQL Serverâ€™s waited for 1930299679 ms total.Â This is kind of interesting, but not what I really need to know.Â How I do really use this data?Â It needs some filtering and aggregation.Â There are some waits that arenâ€™t going to be of interest because they occur all the time and are irrelevant for our purposes; we can filter out those wait types.Â To make the most of our wait stats output, I really want to know the highest wait based on the percentage of time spent waiting overall, and the average wait time for that wait.Â The query that I use to get this information is the one from Paulâ€™s post (mentioned above).Â I wonâ€™t paste it here (you can get it from his post) but if I run that query against my instance, now I get only three rows in my output:
If we reference the various wait types listed in the MSDN entry for sys.dm_os_wait_stats, we see that the SQLTRACE_WAIT_ENTRIES wait type, â€śOccurs while a SQL Trace event queue waits for packets to arrive on the queue.â€ť
Well, this instance is on my local machine and isnâ€™t very active, so that wait is likely due to the default trace thatâ€™s always running.Â In a production environment, I probably wouldnâ€™t see that wait, and if I did, Iâ€™d check to see how many SQL Traces were running.Â But for our purposes, Iâ€™m going to add that as a wait type to filter out, and then re-run the query.Â Now there are more rows in my output, and the percentage for the PAGEIOLATCH_SH and LCK_M_X waits has changed:
If you review the original query, you will see that the percentage calculation for each wait type uses the wait_time_ms for the wait divided by the SUM of wait_time_ms for all waits.Â But â€śall waitsâ€ť are those wait types not filtered by the query.Â Therefore, as you change what wait types you do not consider, the calculations will change.Â Keep this in mind when you compare data over time or with other DBAs in your company – it’s a good idea to make sure you’re always running the same query that filters out the same wait types.
Capturing Wait Statistics
So far Iâ€™ve talked about looking at wait statistics at a point in time.Â As a DBA, you want to know what waits are normal for each instance.Â And there will be waits for every instance; even if itâ€™s highly tuned or incredibly low volume, there will be waits.Â You need to know whatâ€™s normal, and then use those values when the system is not performing well.
The easiest way to capture wait statistics is to snapshot the data to a table on a regular basis, and you can find queries for this process in my Capturing Baselines for SQL Server: Wait Statistics article on SQLServerCentral.com.Â Once you have your methodology in place to capture the data, review it on a regular basis to understand your typical waits, and identify potential issues before they escalate.Â When you do discover a problem, then you can use wait statistics to aid in your troubleshooting.
Using the Data
At the time that you identify a problem in your environment, a good first step is to run your wait statistics query and compare the output to your baseline numbers.Â If you see something out of the ordinary, you have an idea where to begin your investigation.Â But thatâ€™s it; wait statistics simply tell you where to start searching for your answer.Â Do not assume that your highest wait is the problem, or even that itâ€™s a problem at all.Â For example, a common top wait is CXPACKET, and CXPACKET waits indicate that parallelism is used, which is expected in a SQL Server environment.Â If thatâ€™s your top wait, does that mean you should immediately change the MAXDOP setting for the instance?Â No.Â You may end up changing it down the road, but a better direction is to understand why thatâ€™s the highest wait.Â You may have CXPACKET waits because youâ€™re missing some indexes and there are tons of table scans occurring.Â You donâ€™t need to change MAXDOP, you need to start tuning.
Another good example is the WRITELOG wait type.Â WRITELOG waits occur when SQL Server is waiting for a log flush to complete.Â A log flush occurs when information needs to be written to the databaseâ€™s transaction log.Â A log flush should complete quickly, because when there is a delay in a log write, then the task that initiated the modification has to wait, and tasks may be waiting behind that.Â But a log flush doesnâ€™t happen instantaneously every single time, so you will have WRITELOG waits.Â If you see WRITELOG as your top wait, donâ€™t immediately assume you need new storage.Â You should only assume that you need to investigate further.Â A good place to start would be looking at read and write latencies, and since Iâ€™ll be discussing monitoring IO more in tomorrowâ€™s post weâ€™ll shelve that discussion until then.
As you can see from these two examples, wait statistics are a starting point.Â They are very valuable â€“ itâ€™s easy to think of them as â€śthe answerâ€ť, but theyâ€™re not.Â Wait statistics do not tell you the entire story about a SQL Server implementation.Â There is no one â€śthingâ€ť that tells you the entire story, which is why troubleshooting can be incredibly frustrating, yet wonderfully satisfying when you find the root of a problem.Â Successfully troubleshooting performance issues in SQL Server requires an understanding of all the data available to aid in your discovery and investigation, understanding where to start, and what information to capture to correlate with other findings.
Our online training (Pluralsight) courses that can help you with this topic: