Monitoring and Tuning for Performance Overview

The goal of monitoring databases is to assess how a server is performing. Effective monitoring involves taking periodic snapshots of current performance to isolate processes that are causing problems, and gathering data continuously over time to track performance trends. Microsoft SQL Server and the Microsoft Windows operating system provide utilities that let you view the current condition of the database and to track performance as conditions change.

Monitoring SQL Server lets you do the following:

  • Determine whether you can improve performance. For example, by monitoring the response times for frequently used queries, you can determine whether changes to the query or indexes on the tables are required.

  • Evaluate user activity. For example, by monitoring users trying to connect to an instance of SQL Server, you can determine whether security is set up adequately and test applications or development systems. For example, by monitoring SQL queries as they are executed, you can determine whether they are written correctly and producing the expected results.

  • Troubleshoot any problems or debug application components, such as stored procedures.

Monitoring is important because SQL Server provides a service in a dynamic environment. The data in the application changes. The type of access that users require changes. The way that users connect changes. The types of applications accessing SQL Server may even change, but SQL Server automatically manages system-level resources such as memory and disk space so the need for extensive system-level manual tuning is minimized. But monitoring lets administrators identify performance trends to determine if changes are necessary.

To monitor any component of SQL Server effectively, follow these steps:

  1. Determine your monitoring goals.

  2. Select the appropriate tool.

  3. Identify components to monitor.

  4. Select metrics for those components.

  5. Monitor the server.

  6. Analyze the data.

These steps are discussed in turn below.

Determine Your Monitoring Goals

To monitor SQL Server effectively you should clearly identify your reason for monitoring. Reasons can include the following:

  • Establish a baseline for performance.

  • Identify performance changes over time.

  • Diagnose specific performance problems.

  • Identify components or processes to optimize.

  • Compare the effect of different client applications on performance.

  • Audit user activity.

  • Test a server under different loads.

  • Test database architecture.

  • Test maintenance schedules.

  • Test backup and restore plans.

  • Determining when to modify your hardware configuration.

Select the Appropriate Tool

After determining why you are monitoring, you should select the appropriate tools. The Windows operating system and SQL Server provide a complete set of tools to monitor servers in transaction-intensive environments. These tools clearly reveal the condition of an instance of the SQL Server Database Engine or an instance of SQL Server Analysis Services.

Windows provides the following tools for monitoring applications that are running on a server:

  • System Monitor, which lets you collect and view real-time data about activities such as memory, disk, and processor usage.

  • Performance logs and alerts.

  • Task Manager.

For more information about Windows Server or Windows tools, see the Windows documentation.

SQL Server provides the following tools for monitoring components of SQL Server:

  • SQL Trace

  • SQL Server Profiler¬†

  • SQL Server Management Studio Activity Monitor

  • SQL Server Management Studio Graphical Showplan

  • Stored procedures

  • Database Console Commands (DBCC)

  • Built-in functions

  • Trace flags

For more information about SQL Server monitoring tools, see Tools for Performance Monitoring and Tuning.

Identify the Components to Monitor

The third step to monitoring an instance of SQL Server is to identify the components that you monitor. For example, if you are using SQL Server Profiler to trace a server you can define the trace to collect data about specific events. You can also exclude events that do not apply to your situation.

Select Metrics for Monitored Components

After identifying the components to monitor, determine the metrics for components you monitor. For example, after selecting the events to include in a trace, you can choose to include only specific data about the events. Limiting the trace to data that is relevant to the trace minimizes the system resources required to perform the tracing.

Monitor the Server

To monitoring the server, run the monitoring tool that you have configured to gather data. For example, after a trace is defined, you can run the trace to gather data about events raised in the server.

Analyze the Data

After the trace has finished, analyze the data to see if you have achieved your monitoring goal. If you have not, modify the components or metrics that you used to monitor the server.

The following outlines the process for capturing event data and putting it to use.

  1. Apply filters to limit the event data collected.

    Limiting the event data allows for the system to focus on the events pertinent to the monitoring scenario. For example, if you want to monitor slow queries, you can use a filter to monitor only those queries issued by the application that take more than 30 seconds to run against a particular database. For more information, see How to: Set a Trace Filter (Transact-SQL) and How to: Filter Events in a Trace (SQL Server Profiler).

  2. Monitor (capture) events.

    As soon as it is enabled, active monitoring captures data from the specified application, instance of SQL Server, or operating system. For example, when disk activity is monitored using System Monitor, monitoring captures event data such as disk reads and writes and displays it to the screen. For more information, see Monitoring Resource Usage (System Monitor).

  3. Save captured event data.

    Saving captured event data lets you analyze it later or even replay it using SQL Server Profiler. Captured event data is saved to a file that can be loaded back into the tool that originally created it for analysis. SQL Server Profiler permits event data to be saved to a SQL Server table. Saving captured event data is important when you are creating a performance baseline. The performance baseline data is saved and used when comparing recently captured event data to determine whether performance is optimal. For more information, see Using SQL Server Profiler.

  4. Create trace templates that contain the settings specified to capture the events.

    Trace templates include specifications about the events themselves, event data, and filters that are used to capture data. These templates can be used to monitor a specific set of events later without redefining the events, event data, and filters. For example, if you want to frequently monitor the number of deadlocks and the users involved in those deadlocks, you can create a template defining those events, event data, and event filters; save the template; and reapply the filter the next time that you want to monitor deadlocks. SQL Server Profiler uses trace templates for this purpose. For more information, see How to: Set Trace Definition Defaults (SQL Server Profiler) and How to: Create a Trace Template (SQL Server Profiler).

  5. Analyze captured event data.

    To be analyzed, the captured, saved event data is loaded into the application that captured the data. For example, a captured trace from SQL Server Profiler can be reloaded into SQL Server Profiler for viewing and analysis. For more information, see Viewing and Analyzing Traces with SQL Server Profiler.

    Analyzing event data involves determining what is occurring and why. This information lets you make changes that can improve performance, such as adding more memory, changing indexes, correcting coding problems with Transact-SQL statements or stored procedures, and so on, depending on the type of analysis performed. For example, you can use the Database Engine Tuning Advisor to analyze a captured trace from SQL Server Profiler and make index recommendations based on the results. For more information, see Tuning the Physical Database Design.

  6. Replay captured event data.

    Event replay lets you establish a test copy of the database environment from which the data was captured and repeat the captured events as they occurred originally on the real system. This capability is only available in SQL Server Profiler. You can replay them at the same speed as they originally occurred, as fast as possible (to stress the system), or more likely, one step at a time (to analyze the system after each event has occurred). By analyzing the exact events in a test environment, you can prevent harm to the production system. For more information, see Replaying Traces.