Sie sind auf Seite 1von 15

SQL Server Profiler

SQL Profiler is a tool that captures SQL Server events from the server and saves those
events in what's known as a trace file. You can then analyze or use the trace file to
troubleshoot logic or performance problems. You can use this utility to monitor several
areas of server activity, such as:

Analyzing and debugging SQL statements and stored procedures.
Monitoring slow performance.
Stress analysis.
General debugging and troubleshooting.
Fine-tuning indexes.
Auditing and reviewing security activity.
SQL Server Profiler Step by Step
By Abhijit Jana, 22 Feb 2008


3.32 (32 votes)



Rate this: vote 1vote 2vote 3vote 4vote 5

Introduction
Microsoft SQL Server Profiler is a graphical user interface to SQL Trace for monitoring T-SQL
Statements of Database Engine. We can save and reuse the state at a later point of time.
We can do the following using SQL Server Profiler
o Create a trace
o Watch the trace results as the trace runs
o Store the trace results in a table
o Start, stop, pause, and modify the trace results as necessary
o Replay the trace results
Use SQL Server Profiler to monitor only the events in which you are interested.
Menu Path: Start | All Programs | Microsoft SQL Server 2005 | Performance Tools | SQL Server
Profiler.
The following screen will come:

Figure 1.0
Click on <Connect> Button. New Properties Screen will come:

Figure 1.1
It has two selection tabs:
General: It is used for general setting for Trace Database Engine.
Event: It is used to add or remove some selected event for monitor.
In General Section (as given in Figure 1.1), it is divided into four sections.
Section 1: In this section, you have to just specify the name of your trace, Trace provider name and
server name are predefined and based upon your SQL Server.
And it is not editable.
Section 2: It is the template section. You can choose different type of Templates based upon your
requirements. It is the configuration for trace. By default, it is "Standard (Default)" templates. Others
templates are T-SQL, T-SQL Duration, T-SQL Reply, T-SQL SPs, etc. You can create your own custom
Templates by selecting different Events and Event Class. It is saved as ".tdf" Extension.
Section 3: This section is related to save your trace. Either as File (.trc) or in a database. as table.
While clicking on Save to file check box, File save dialog box should open and you can save that file
(with .trc extension).
If you check the "Save to Table", it will connect with your server and ask you to which database you
want to save that trace table information.

Figure 1.2
Section 4: You can stop your trace on a particular time. Check the "Enable trace stop time" checkbox
and give the time at which you want to stop track, SQL Server will automatically stop trace on that
time.
Now Move To "Event Section" Tab.
Now we need to know some definition with respect to SQL Server Profiler.
What is an Event?
An Event is an action or operation that is performed in your SQL Server 2005 Database Engine.
Some examples of Events are:
o Transact-SQL SELECT, INSERT, UPDATE, and DELETE statements.
o User login and logout
o Execution of Stored procedures
o Operation with cursor
SQL Server profiler is able to trace all of these events and all these events are categories on a
particular Event class.
What is an Event Class?
Event class is a type of event that can be traced.
Some examples are:
SQL: BatchCompleted
SQL: Batch Starting
Audit Login
Audit Logout
Lock: Acquired
Lock: Released
Now you can select events from this screen:

Figure 1.3
In section 1, we can select the proper check box based upon our requirement, section 2 will display
the details of Selected events and Events class. If you check in the check box of section 3, you will get
all the list of Events and Columns in Section 1.
Section 4 is something like customization. Just click on the "Column Filter Button". In this section,
you can specify some condition (like or Not like).

Figure 1.4
By clicking on "Organize Column" button, you can change the sequence of order of selected events.
Now Click on the "Run" Button, then Trace window will come:


Figure 1.5
Using these windows, you will get the detailed time duration of a query and all other events
information that you have selected.
You can save this result and use it in future. Or you can extract a particular query from the trace, just
right click and click on "Extract Event Data". And save this as a SQL Script.
Reply in SQL Server Profiler
SQL Server profiler has a Reply facility which has the ability to save a trace and replay it later.
Replay is useful to troubleshoot an application. Trace replay supports debugging by using Toggle
Breakpoint and the Run to Cursor options on the SQL Server Profiler Replay menu.
Anything changed in SQL Server Management Studio will be traced by the SQL Profiler. So it can
basically be used for database performance check. We also have "SQL Server Performance Monitor"
to monitor the System and Server performance too.
http://www.codeproject.com/Articles/21371/SQL-Server-Profiler-Step-by-Step






SQL SERVER Introduction to SQL
Server 2008 Profiler
Introduction
SQL Server Profiler is a powerful tool that is available with SQL Server since a long time; however, it has
mostly been underutilized by DBAs. SQL Server Profiler can perform various significant functions such as
tracing what is running under the SQL Server Engines hood, and finding out how queries are resolved
internally and what scripts are running to accomplish any T-SQL command. The major functions this tool can
perform have been listed below:
Creating trace
Watching trace
Storing trace
Replaying trace
Trace includes all the T-SQL scripts that run simultaneously on SQL Server. As trace contains all the T-SQL
scripts running on SQL Server, it often tends to become considerably huge. Hence, it is always a good practice
to capture only those data that are actually required for analysis.
Dilemma to use on Production Server
SQL Server Profiler has a lot of potential as a tool and offers several advantages. Unfortunately, developers
have been keeping their hands off from this tool and usually give two major excuses.
Reason 1: Profiler adds too much overhead to production server.
Reason 2: Profiler accumulates plethora of data making it extremely intricate to analyze them
afterwards.
It cannot be denied that both the above excuses are valid reasons and play a significant role in discouraging
developers from using the profiler. When a profiler is run over a production server it adds additional load on
the CPU. However, the real question is How much load does the profiler add to CPU? It may be possible that
the production servers CPU is not used more than 50% at all, and running the profiler over that database will
not degrade the performance.
If the profiler is configured appropriately to trace only relevant data for analysis, it will diminish the additional
load that is introduced while it is running. If the profiler is run for a longer duration, then to capture the event
of longer duration it is very important to collect selective data, otherwise the collected data can outgrow the
space on the server.
Profiler Terminology
It is important to understand profiler terminology before digging into its details and finding how it works.
Event - An event is an action within an instance of SQL Server Database Engine. Some good examples of
event are running any T-SQL or performing any operation in the SSMS that is related to database; running
stored procedures, and creating jobs.
Event Class - An event class can be defined as a type of event that can be traced. Examples of event classes
are SP:Starting and RPC:Completed.
Event Category Group of events are called an event category. Examples of event category are Stored
Procedure and Locks. In fact, there can be multiple event categories that can be selected for single trace.
Data Column - Data column is an attribute of an event class captured in the trace. Data column contains value
of an event class.
Filter Filters are used to create selectiveness in data that are collected in trace.
Trace - A trace captures data based on the selected event classes, data columns, and filters. A trace devoid of
filter can be very hefty. Columns that are indispensable for the analysis should be selected only in trace. Trace
can be saved in the database as well as in trace files.
Template - A template defines the default configuration for a trace. Templates can be saved, imported and
exported between SQL Server instances. Templates from one SQL Server version cannot be imported to a
different SQL Server version. SQL Server 2009 comes with 9 pre-created templates. The default template is a
standard template that captures all SP and T-SQL batches. It additionally logs general database server activity.
Some very basic examples are presented below where data has been captured.
Starting the Profiler
SQL Server profiler is a component of client tools and can be installed independently from SQL Server
Database Engine. It is mandatory for the user to have system admin rights to start the profiler. It can be
started using the following methods.
1. Going to Start >> All Programs >> Microsoft SQL Server 2008 >> Performance Tools >> SQL Server
Profiler

2. From SQL Server Management Studio >> Go to Tools >> SQL Server Profiler

3. For SQL Server 2008, type profiler in the command prompt.
4. For SQL Server 2005, type profiler90 in the command prompt.

Collecting Data
Once the profiler is started, connect it to any database. As discussed earlier, a profiler is not restricted to
connecting only to the local database. With appropriate authentication and system admin role, it can connect to
any database and capture the data.
Once the profiler is started click on Menu >> File >> New Trace.

This will display a login prompt very similar to SQL Server Database Engine. Connect with the appropriate
username and password.

It will display the Trace Properties screen. On this screen you can enter the trace name. You will notice that
trace provider name, type and versions are pre-populated and cannot be altered. These are set based on which
SQL Server instance you are connected with.

The Trace Properties screen contains a drop-down for template selection, which is labeled with Use the
template. As mentioned earlier, the default template selected is standard. We can see what events are included
in the Standard template in the next tab, Events Selection.

The Event selection screen contains a checkbox that can be configured as per the requirement of data to be
collected. The same screen contains a Column Filters button, which when clicked brings up another screen
that can be used to further filter trace data.

There is another button named Organize Columns on the same screen. This screen can be used to organize
and group columns. Please note that columns cannot be grouped or ordered once the process of trace collection
has begun.

The Trace Properties screen contains a Run button. When you click on this button, it runs the trace. The
trace can be paused or stopped as required using the play menu on top.

This data can be saved in database and trace file or saved as XML. Trace file can be imported back in profiler,
and profiler can re-run the whole scenario. Once the trace file is saved in database table, it can be queried by
using T-SQL.

Das könnte Ihnen auch gefallen