23
SQL Server Wait Types Everyone Should Know Dean Richards

SQL Server Wait Types Everyone Should Know

Embed Size (px)

DESCRIPTION

Many people use wait types for performance tuning, but do not know what some of the most common ones indicate. This presentation will go into details about the top 8 wait types I see at the customers I work with. It will provide wait descriptions as well as solutions.

Citation preview

Page 1: SQL Server Wait Types Everyone Should Know

SQL Server Wait TypesEveryone Should Know

Dean Richards

Page 2: SQL Server Wait Types Everyone Should Know

Who Am I• 20+ years of experience in SQL Server and Oracle• Speak at many user groups throughout US• Owner of DRConsulting• [email protected]• @ConfioDean• Focus on application and database performance

• Review performance for hundreds of customers per year• Common thread – how do I do performance tuning?

Page 3: SQL Server Wait Types Everyone Should Know

What Are Wait Types• SQL Server has been instrumented to give clues about what it is

doing when processing SQL statements• Wait Types identify a step being taken by SQL statement and its

latencies• These clues help immensely when doing SQL analysis/tuning• Knowing a SQL waits on locking issues will lead to a different

solution than if it were waiting on disk reads• SQL Server 2012 – 649 Wait Types• SQL Server 2014 – 800+ Waits• For a more complete description (for SQL 2005 but still relevant)• Microsoft Waits and Queue Document

Page 4: SQL Server Wait Types Everyone Should Know

Useful DMVs for Wait Types• sys.dm_os_wait_stats• Cumulative since instance startup• select * from sys.dm_os_wait_stats order by wait_time_ms desc

• Exclude idle wait types in slide notes• Provides a view into what your instance is waiting for

• sys.dm_exec_requests• Real-time view into what each session/SQL is waiting for• See slide notes for example query• Suspended state means the session is waiting for the wait_type• Running means the session is on the CPU• Sleeping means the session is idle

Page 5: SQL Server Wait Types Everyone Should Know

Wait Type DMV Problem• No Time Based View• What happened between 3am-5am this morning is not possible

to get from the DMV objects• Need to use other tools• Extended Events Session to gather waits and query results• System_health default session *does not* gather waits• Solarwinds Database Performance Analyzer (DPA formerly Confio

Ignite) does a great job of this• DPA provides great views into which SQLs are problematic and

what they are waiting for. Also includes a knowledge base for many wait types.

Different DMV Problem

Page 6: SQL Server Wait Types Everyone Should Know

Top 8 List of Wait Types• From my experience, there is a small list of wait types

you need to know well• The other 500+ you can Google or ask Microsoft• Need to know:• What causes these waits• How to reduce / fix these waits

• This presentation discusses common wait types I have personally encountered the most from over 400 customers

Page 7: SQL Server Wait Types Everyone Should Know

1. PAGEIOLATCH_*• Disk read when a page required by a SQL is not in the

buffer cache• Where * in:• SH – shared: session reads the data• EX – exclusive: session needs exclusive access to page• UP – update: session needs to update data• DT – destroy: session needs to remove the page• KP – keep: temporary while SQL Server decides• NL – undocumented

• The SH, EX and UP latches are by far the most common

Page 8: SQL Server Wait Types Everyone Should Know

1. PAGEIOLATCH_* Solutions• Do fewer disk reads• Tune the SQL statement to do less I/O• Cache more data, i.e. bigger buffer cache so disk reads no needed

• Many SQLs waiting – bigger cache may help• A few SQLs waiting – probably means SQL tuning

• Use query in notes to check MB/sec – are you trying to read/write way too much data and overloading disks – tune SQL.

• Make disk reads faster• Check file/disk latency with sys.dm_io_virtual_file_stats DMO• Use query in notes• Anything higher than ~ 15 ms would be considered slow on a

production class server• Talk to storage team but remember there are many layers between

the database and storage, i.e. O/S, virtualization, network, etc

Page 9: SQL Server Wait Types Everyone Should Know

2. WRITELOG• Waiting for a log flush to complete• Log flush commonly occurs because of checkpoint or commit• Commits can be explicit (commit) or implicit (auto-commit)

Page 10: SQL Server Wait Types Everyone Should Know

2. WRITELOG Solutions• Do less work• Develop code to do more batch processing• Single row processing inside loop rather than set based

processing?• Make disk writes faster• Avoid RAID5/6 – write I/O penalty• Check file/disk latency with sys.dm_io_virtual_file_stats DMO• Review the write latencies for the transaction logs• Reduce I/O contention on disks containing logs• Solid State? – many questions about this but several test cases

have seen good results• Size the transaction logs properly – see notes for a good

references on this subject

Page 11: SQL Server Wait Types Everyone Should Know

3. ASYNC_NETWORK_IO • Query produces result set and sends back to client. While

client processes data SQL Server waits on this• Often caused by large result sets being returned• Application that queries every row from large tables• See MS Access joining SQL Server data to Access data. Access

must get all data in SQL table, bring back to Access to join it. Will see “select * from <large table>” queries

• Can also apply to linked server queries• Slow client processing• Client machine is very busy and not processing results quickly• Client is reading data and doing processing on it that is slow

• Could be a slow network connection from client to server

Page 12: SQL Server Wait Types Everyone Should Know

3. ASYNC_NETWORK_IO Solutions• Limit the result sets• Some poorly written applications read data from entire table and

then filter at client. Filter from database first or write rows to temp table and have client read temp data

• Avoid joins across Access to SQL Server data. This also applies to Linked Server and other distributed queries

• Check performance of client machine. If it is resource constrained, it may not process results quickly• Check logic of client application and avoid retrieving large

result sets if possible. Do more result set processing in database• Check the speed and stability of the network between

client and server.

Page 13: SQL Server Wait Types Everyone Should Know

4. CXPACKET • Session is running a SQL in parallel• More of a status and not necessarily a problem. May be

very normal for data warehouse but less so for OLTP• Master process will farm work out to slave processes and

then wait on CXPACKET until all have completed• SQL Server will try to parallel-ize big queries up to

MAXDOP – can be set instance wide down to this query• MAXDOP = 0 by default meaning unlimited• http://support.microsoft.com/kb/2806535 - recommendations• MAXDOP should not be set higher than 8 in most cases

Page 14: SQL Server Wait Types Everyone Should Know

4. CXPACKET More Information• Need to understand the slave processes and what they

are doing / waiting for• Use sys.dm_os_waiting_tasksselect session_id, exec_context_id, wait_type, wait_duration_ms, resource_descriptionfrom sys.dm_os_waiting_tasks where session_id in ( select session_id from sys.dm_exec_requests where wait_type='CXPACKET')order by session_id, exec_context_id

• Example Outputsession_id exec_context_id wait_type wait_duration_ms resource_description64 0 CXPACKET 41792064 1 PAGEIOLATCH_SH 149 5:1:135883064 2 PAGEIOLATCH_SH 368 5:1:351463964 3 PAGEIOLATCH_SH 84 5:1:348408964 4 PAGEIOLATCH_SH 156 5:1:1348098

• In this case, tune PAGEIOLATCH_SH waits

Page 15: SQL Server Wait Types Everyone Should Know

4. CXPACKET Solutions• Tune MAXDOP according to KB article provided• Not all queries do well in parallel, so experiment with

MAXDOP settings at SQL level• Much bad advice about reducing MAXDOP server wide

but avoid doing this blanket change• Tune the parallel slave process waits (previous slide)• Inefficient queries reading a lot of data will often be

parallel-ized by SQL Server. Tune the queries• Review data skew and bad statistics which can cause one

or few slave processes to do a bulk of the work

Page 16: SQL Server Wait Types Everyone Should Know

5. CPU• Not a wait type - identifies time on CPU• Query Response Time = Wait Time (wait types) + Service Time (CPU)

• Waiting on CPU when sys.dm_exec_request has:• status = running• wait_type is often null• wait_time = 0

• CPU time is often spent doing logical I/O• Could also come from compiles/recompiles and other

CPU intensive operations

Page 17: SQL Server Wait Types Everyone Should Know

5. CPU Solutions• Tune queries with high amount of CPU time and often

high logical I/O (see notes)• Reduce high execution count queries – do more batch

processing• Check O/S for other activity putting pressure on CPU –

may see high CPU queue lengths• Is the hardware undersized – may need to purchase

larger/faster servers

Page 18: SQL Server Wait Types Everyone Should Know

6. LCK_M_*• Classic locking/blocking scenario• Where * in 21 different possibilities. Most common are:• U – trying to update the same resource• S – trying to modify data while it is being read• X – trying to lock a resource exclusively• IU, IS, IX – indicates intent to lock• SCH – schema locks – object is changing underneath

• A session waiting on LCK_M_* wait is the victim. Need to use blocking_session_id in dm_exec_request to see the root cause (see query in slide notes)• Not to be confused with deadlocks – special locking case

Page 19: SQL Server Wait Types Everyone Should Know

6. LCK_M_* Solutions• Review the wait_description data to understand the locked resource. See

slide notes for information.• Review the blocking session and understand the relationship with the

blockee. Does the application need to be redesigned?• Blocking issues are often associated with a session holding locks for

longer than necessary• Does the blocking session go on to do a lot of other SQLs? Can the transactions be

committed sooner?• Does the blocking session execute inefficient SQLs while holding locks? Tuning the

poor SQL could reduce the blocking time.• Has the client process waited and finally terminated due to timeouts? The SQL

Server session could be left behind (orphaned) and never go away. Terminating the session should release the locks.

• Is the client not fetching the whole result set quickly enough? See the ASYNC_NETWORK_IO wait description.

• Is the session rolling back data? If so, that process must complete before locks are released

Page 20: SQL Server Wait Types Everyone Should Know

7. PREEMPTIVE_*• Often associated with external calls to O/S etc• Code being executed is outside SQL Server• OS_GETPROCADDRESS – seen with xp_fixeddrives,

xp_readerrorlog, etc. Session is going to the O/S to get information• OLEDBOPS – seen with bulk loads from file and other

external database operations• OS_AUTHENTICATIONOPS – seen when using

xp_fixeddrives• OS_PIPEOPS – seen when using xp_cmdshell

Page 21: SQL Server Wait Types Everyone Should Know

8. PAGELATCH_*• Not as common, but 8 is my lucky number and I needed

one more wait type • Latches synchronize access to internal SQL Server

memory structures• Many different classes of latches, but BUFFER is most

common, i.e. control access to buffer cache• Where * is the same set of values shown in

PAGEIOLATCH waits slide• Not a disk read – do not confuse with PAGEIOLATCH

Page 22: SQL Server Wait Types Everyone Should Know

PAGELATCH_* Solutions• Use wait_resource to determine the page waited for. See

slide notes for LCK_M_* for decoding this data• Could be TEMPDB contention. Check the wait_resource

column from dm_exec_requests to see if the dbid (first part) is 2 to verify. • If so, good document from Microsoft -

http://www.microsoft.com/en-gb/download/details.aspx?id=26665• Can be caused by index page splitting• Specify fill factors properly

• Tune SQL statements waiting for this. Inefficient SQL read more data from memory than needed and increase the likelihood of this wat

Page 23: SQL Server Wait Types Everyone Should Know

Summary• Wait Types provide the best information about why a SQL

statement is running slow• SQL Server includes several DMVs showing Wait Types but

lacks the point in time view which allows you to go back to 3-5am when something was slow• Use Extended Events to collect the data• Use tools like Solarwinds Database Performance Analyzer (DPA)

• Memorize these 8 wait types, what causes them, how to fix them and you will be good to go

• Contact me at:• [email protected]• @ConfioDean• www.linkedin.com/in/deanrichardsconsulting/