DOWNLOAD THE CODE:
Download the Code 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.

End of Article




You must log on before posting a comment.

If you don't have a username & password, please register now.

Reader Comments

Not bad!

RSP_SQL

Article Rating 4 out of 5

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?

datagod

Article Rating 5 out of 5

doesn't work as advertised

wilking

Article Rating 1 out of 5

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.

rpanigas@hoopp.com

Article Rating 3 out of 5

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.

datagod

Article Rating 5 out of 5

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.

sendow

Article Rating 4 out of 5

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.

datagod

Article Rating 5 out of 5

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?

jaypat

Article Rating 3 out of 5

Sorry ignore my previous comment, I re-read the article.

jaypat

Article Rating 5 out of 5

 
 

ADS BY GOOGLE