Prove That the Database Isn't to Blame

Downloads
93150.zip

If you ever had users call to tell you that a SQL Server database is slow, you'll likely find sp_Now a handy tool. This stored procedure determines what processes are currently executing and consuming resources on a database server. This information is helpful when troubleshooting sporadic performance problems, especially in an environment in which applications span multiple servers.

For example, if a user calls and complains that the database is slow, you can run sp_Now to quickly determine what the database server is actually doing. If the database server is under a heavy load due to SQL activity, you'll be able to see the exact SQL code that's causing the load. Perhaps a user submitted a poorly coded query (e.g., the user created a Cartesian product of the three biggest tables). Or perhaps an administrator is running a job that's producing a detailed report, but that job really should be run only during off-hours. If sp_Now shows that not much SQL activity is occurring, you can be pretty sure that the database server isn't to blame. You can then work with the user to determine whether the problem is occurring at the application or network level.

Listing 1 shows an excerpt from sp_Now Now, which I wrote for use on SQL Server 2000. As callout B in Listing 1 shows, sp_Now uses the fn_get_sql system function, which Microsoft introduced in SQL Server 2000 Service Pack 3 (SP3). This function returns the SQL statements that a particular process is currently executing. The sp_Now stored procedure uses fn_get_sql to build a cursor of all currently active processes.

As callout A shows, sp_Now produces a report that shows summary information for each active process, including the process's cumulative disk reads and writes (phys_io), the process's CPU usage (cpu), and the application's name (program_name) from the sysprocesses table. The report also includes the exact SQL statements that the process is executing.

The report is easy to read. At the top, you'll find the total number of active system process IDs (SPIDs) that have open SQL connections and that are currently processing data.The report uses two lines of x's to separate the details of what each SPID is doing.

For each SPID, there are three sections. The first section provides a summary from the sysprocesses table that shows the login name, host name, and other details of the associated SQL connection. This section includes a summary of the connection's CPU usage, the total amount of disk I/O, and the SPID of any other process that's being blocked by this process. (Multiple entries for the same SPID in this section indicates that SQL Server has divided up the query among multiple CPUs in an attempt increase the performance through parallel processing.)

The next section contains the output of DBCC INPUTBUFFER (which displays the last statement sent from a client) for the SPID being examined. The first 255 characters of the query are displayed.

The last section contains the output from the call to the fn_get_sql system function. A much larger portion of the SQL code being executed is displayed here. In the case of a stored procedure, usually all the code is displayed.

When reviewing the sp_Now output, pay attention to the amount of physical I/O and CPU time the processes are using. Also take note of any blocking. Remember that if processes are consuming resources, they'll show up in the results. If nothing shows up in the results, you can be pretty sure that the database server isn't experiencing performance problems.

Discuss this Article 9

datagod
on Sep 29, 2006
I just used this proc to show how every time a user was hitting a particular button on the app, the server start to grind. Pushing the button caused a script to run that updated every ID in a table to itself. Yes, you read that right. I don't know who wrote that code, but it is not only useless it brings the server to its knees. Did I mention the ID had a clustered index on it?
jaypat
on Nov 29, 2006
Sorry ignore my previous comment, I re-read the article.
jaypat
on Nov 29, 2006
Nice tool but it only returns the results for my spid (using sql server 2000) how can it be changed to show the activity for all current logged on users?
datagod
on Oct 16, 2006
The script was indeed written for SQL2000, service pack 3 (and up). Make sure you are running the latest service pack. As for your suggestions, they sound great. The source is open, knock yourself out.
rpanigas@hoopp.com
on Oct 5, 2006
Why is this code showing my own process? Does it work on SQL 2005? I think that all articles should specify SQL200, SQL 2005 or both.
sendow
on Oct 14, 2006
Despite the author's note that he wrote this for SQL 2000 (perhaps an editing error), based on my tests, the script does NOT work on SQL 2000, and was written for SQL 2005. (there is no sql_handle field in sysprocesses on my SQL 2000, but there is in 2005) Very interesting utility--excellent idea, solving a problem that I have had several times trying to diagnose a slow running SQL Server. It is a nice utility in that it displays only the activity that is occurring, vs. running sp_who and sysprocesses and having to scroll through the results. Having just tested it for the first time, I'm finding the output very difficult to review and scan, since each SPID has 3 different result sets. Perhaps it could be tweaked to insert the data into a temp table that can accomodate all of the fields and produce a more readable output.
wilking
on Oct 4, 2006
doesn't work as advertised
RSP_SQL
on Sep 27, 2006
Not bad!
datagod
on Oct 6, 2006
First line of third paragraph states this code was written for SQL2000. You can block your own spid, but you won't be able to see if you are blocking some other process.

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottOur Experts will show you:
• Common SQL Server
Problems
• Best Practices for T-SQL
• SQL Server Integration
Services
• Database Development

Come See Michael Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 21, 2013
blog

A Common Misconception about MAXDOP

Out of the box, SQL Server is (and has been) able to take advantage of multiple processors/cores without any effort on behalf of administrators....More
May 9, 2013
blog

My ISO 8601-Compliant Signature 2

My family recently just "officially" announced that we're in the process of adopting a child from South Africa. We're quite excited, of course, but there's a ton of paperwork to do—along with the need for gobs of signatures....More
May 8, 2013
blog

Use SSIS for ETL from Hadoop

In this blog post, Mark Kromer walks you through using SSIS as a way to use ETL techniques using Microsoft's Hadoop on Windows (HDInsight) as a source using Hive connectors...More
SQL Server Pro Forums

Get answers to questions, share tips, and engage with the SQL Server community in our Forums.