Thursday 2 June 2011

Basic Troubleshooting of a Slow Database - SQL Server 2008

Troubleshoot a Slow Database: SQL Server 2008
  • Troubleshooting database slowness can be difficult.  Most problems will present themselves to you right away. 
  • Sometimes the exact issue may not appear obvious. Furthermore once you find the issue, there isn't a satisfactory solution.
The guide below is a way of finding a place to start and will help with the majority of 'slowness' issues.

Once you have determined that it is SQL Server that is causing the performance issues, we need to find the query which is causing the problems.

Your primary performance investigations may have told you that it was one of three issues:
  • A CPU issue
  • A disk issue
  • Memory Issue
Use the following code depending upon the issue. n.b. they are all based upon (Select * from sys.sysprocesses ) except for the Order By clause.

In SSMS, open a new query and type the following line of code:

CPU issue: "Select * from sys.sysprocesses order by cpu desc"
Disk Issue: "Select * from sys.sysprocesses order by physical_io desc"
Memory Issue: "Select * from sys.sysprocesses order by memusage desc"

Each of these order clauses tells you what the most expensive queries are in terms of what you’re ordering by.  So the “order by memusage desc” clause puts the highest memory usage queries to the top of the list. 



Further Investigations:

If the queries above do not identify any issues you’ll need to check some other things in the 'sys.sysprocesses' table.

Blocking:

In SSMS, execute the following code: "Select * from sys.sysprocesses order by blocked"

Look at the blocked column.  A small amount of blocking in your database is ok provided that these are intermittent. However, if you’ve got a single process that’s blocking everything else then this is a problem. This block will subsequently block another process, sparking a chain reaction of blocked processes. 

We need to identify the first process that is causing the chain reaction of blocked processes. It can be identified as the process that isn't blocked by anything else.

spidblocked
101121
1140
121114
150136
136101

The numbers in the blocked column relate to the numbers in the spid column. Thus we can see that the spid; 114 is not being blocked by anything.

You can now hand this over to your DBA. If you happen to be the DBA you can find out what that spid is doing by executing the DBCC InputBuffer(spid) in SSMS query window:

DBCC InputBuffer(spid)

To look at spid 114 type: DBCC InputBuffer(114)


"lastwaittype":
Another more complex column to look at within 'sys.sysprocesses' is 'lastwaittype'.  This can tell you which query the initial blocked spid is waiting to complete.  More can be written about this but we will keep it simple for now and just take a look at the following values.
  • Cxpacket
  • IO_Completion
  • Network_io
  • SOS_SCHEDULER_YIELD
When looking at the 'lastwaittype', these are some explanations as to some of the values:

Cxpacket – This usually means that your process is waiting on other parallel processes to complete.  So what’s probably happening is you have a process running on more than one CPU and one thread finished before the rest.  To fix this: limit your parallelism either at the query or server level.  It could also be a query tuning issue.
IO_Completion – Long wait times indicate disk issues. You could either not have enough disks for the operation or you could be running an array in degraded mode.
Network_io – If you see this for an extended amount of time, you’re waiting on the network, and this is an indication that there is too much traffic.  Solution: Call your network team.
SOS_SCHEDULER_YIELD – Long wait times indicate CPU issues.  You may not have enough CPU or you may have some queries that need tuning.

Useful Links:

http://www.simple-talk.com/sql/performance/finding-the-causes-of-poor-performance-in-sql-server,-part-1/
http://www.simple-talk.com/sql/performance/finding-the-causes-of-poor-performance-in-sql-server,-part-2/

No comments:

Post a Comment