|
|
This is a two part excerpt from Joseph Sack's book, SQL Server 2005 T-SQL Recipes (Apress). This book is considered to be a "no-fluff" desk reference that covers basic T-SQL data manipulation, the use of stored procedures, triggers and UDFs, and advanced T-SQL techniques for database security and maintenance. It also provides "how-to" answers to common SQL Server T-SQL questions, conceptual overviews, and highlights of new features introduced in SQL Server 2005.
There are usually two branches of query performance tuning: proactive and reactive. Proactive query tuning usually occurs during development. You design the database, populate it with data, and then start building queries. You build the queries based on application and end-user requirements. For those queries that don’t perform well, you can tune them before deploying out in the stored procedure or to the application developer.
Reactive performance involves capturing poor performance after the code has already been deployed to production. Data changes over time, and so does the effectiveness of indexes and the queries that use them. Queries that once performed well may execute longer than they did originally. You may hear complaints from end-users, or you might actually seek out poorly performing queries yourself.
One of the most valuable graphical interface tools in the SQL Server 2005 toolset is SQL Server Profiler. With SQL Server Profiler, you can monitor query activity as it occurs against your SQL Server instance. There are many potential uses for this tool, but this recipe specifically demonstrates how to use SQL Server Profiler to capture high duration queries.
In this recipe, SQL Server Profiler is launched and configured to capture high duration queries. Then, a query is executed in SQL Server Management Studio that will be captured in SQL Server Profiler:

Figure 28-1. The Connect to Server dialog box

Figure 28-2. The Trace Properties dialog box

Figure 28-3. Events Selection

Figure 28-4. Expanded events and columns

Figure 28-5. Edit Filter dialog box
SELECT CustomerID, ContactID, Demographics, ModifiedDate
FROM Sales.Individual

Figure 28-6. SQL Server Profiler results
How It Works
In this recipe, I demonstrated using SQL Server Profiler to identify a long running query. I filtered the list based on the query’s duration in this example, but you can also add additional filters based on your needs. For example, you can add an additional filter to only include activity for the AdventureWorks database. Or, you could add filters to only include queries that reference a specific database object or column name. Once you’ve captured the activity you are looking for, you can save the trace output to a file or table. You can also launch the Database Engine Tuning Advisor to evaluate the trace data for potential index improvements.
Joe Sack is a senior consultant with Microsoft's Consulting for IT
Operations team. He is the author of SQL Server 2000 Fast Answers (Apress),
and the recently published
SQL Server 2005 T-SQL Recipes (Apress).