Subscribe to Windows IT Pro
September 21, 2006 12:00 AM

Prove That the Database Isn't to Blame

SQL Server Pro
InstantDoc ID #93150
Rating: (9)
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.

Related Content:

ARTICLE TOOLS

Comments
  • Jay
    6 years ago
    Nov 29, 2006

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

  • Jay
    6 years ago
    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?

  • William
    6 years ago
    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.

  • Stephen
    6 years ago
    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.

  • William
    6 years ago
    Oct 06, 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.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.