一、Creating a trace or workload

注意点:

In the Trace Properties dialog box, there is a checkbox option in the General tab with the caption Server processes trace data, to specify whether
trace data should be processed on the server. If not checked, trace data is processed at the client side.
When trace data is processed at the client side, it is possible for some events to be missed if the server load is high. If this option is checked, then trace
data is processed on the server and all the events included in trace definition are guaranteed to be captured without miss. However, this guarantee comes
with performance penalty, because processing trace data on server has an impact on the performance of SQL Server, and hence enabling this option is
not recommended on production server.
Also, running SQL Server Profiler on production server itself should be avoided as running SQL Server Profiler is resource consuming. Instead, you should run
SQL Server Profiler from a client computer and connect it to your SQL Server from there.

When large amount of event data is captured, the trace file can grow very quickly and become very large. Enabling the Enable file rollover option can prevent a trace file from becoming very large by limiting a file to the maximum file size specified. When the file size is reached to the maximum file size specified, SQL Server creates a new roll-over file with the same name appended with a suffix of an incremental number for the same trace. Thus, when we have this option enabled and the size of trace data is greater than maximum file, we have multiple trace files for the same trace.

It's best to store the trace file on a separate disk other than the one which is used to store data files and log files of SQL server databases. Storing the trace file on the same physical disk where database files are stored can degrade the performance of normal I/O operations of other databases.

提示

Configuring a trace by enabling the Save to table checkbox in the Trace Properties dialog box and saving trace data directly to trace table is less
efficient. If you want your trace data to be saved in a trace table then consider saving the trace data first in a trace file; then export your trace data from
trace file to trace table by opening the trace file in SQL Server Profiler and selecting the Save As command from the File menu with the Trace Table… 
option. When you want to save your trace in a trace table, always consider to save your trace in a separate database.

 

SQL Profiler

SQL Server Profiler is a graphical user interface tool for working with SQL Trace. Behind the scene, it uses the same SQL Trace engine, but additionally provides graphical user interface to the user for working with traces. SQL Server Profiler provides functionalities, such as displaying collected event data on its graphical interface, saving traces either in a file or in an SQL Server table, opening previously saved traces, extracting T-SQL statements from a trace,and many more. Finding and analyzing long running or costly queries, finding deadlocks and their related information, looking for which indexes are scanned, and looking for database connection requests are some of the practical applications of SQL Server Profiler.

To get list of all available event classes, you can query sys.trace_events catalog view.You can join sys.trace_events and sys.trace_categories

catalog views on category_id column to make correlation between the two views.

select e.trace_event_id, c.name from  sys.trace_events e
join sys.trace_categories c on e.category_id = c.category_id

Commonly-used event classes
The following list gives brief descriptions of commonly used event classes:

  • Audit Login: This event occurs when a user connects and logs in to SQL Server
  • Audit Logout: This event occurs when a users disconnects and logs out from
  • SQL Server
  • RPC:Starting: This event occurs when a Remote Procedure Call (RPC) starts executing
  • RPC:Completed: This event occurs when a Remote Procedure Call (RPC) completes its execution
  • SQL:BatchStarting: This event occurs when a T-SQL batch starts executing
  • SQL:StmtStarting: This event occurs when a statement inside a T-SQL batch starts executing
  • SQL:StmtCompleted: This event occurs when a statement inside a T-SQL batch completes its execution
  • SQL:BatchCompleted: This event occurs when a T-SQL batch completesits execution
  • SP:Starting: This event occurs when a stored procedure starts executing
  • SP:StmtStarting: This event occurs when a statement inside a stored procedure starts executing
  • SP:StmtCompleted: This event occurs when a statement inside a stored procedure completes its execution
  • SP:Completed: This event occurs when a stored procedure completes its execution

Commonly-used data columns
The following list gives brief descriptions of commonly used event classes:

  • ApplicationName: This data column represents the name of the client application causing a trace event to occur
  • DatabaseID: This data column represents the internal system assigned ID of the database for which a trace event occurs
  • DatabaseName: This data column represents the name of the database for which a trace event occurs
  • HostName: This data column represents the name of the host or computer where the client component connecting to SQL Server causes a trace event to occur
  • LoginName: This data column represents the name of the login under whose security context, particular T-SQL statement(s) executes that causes trace event to occur
  • ObjectID: This data column represents the internal system assigned ID of an object for which a trace event occurs
  • ObjectName: This data column represents the name of an object for which a trace event occurs
  • SessionLoginName: This data column represents the name of the login who initiated the connection and under whose security context a trace event occurs
  • SPID: This data column represents the Server Process ID or Session ID of the connection which causes a trace event to occur

Filter example:

Because two SQL Server logins named James and Peter with permissions on AdventureWorks2012 database are required, create them by performing the following steps:
1. Open SQL Server Management Studio.
2. Connect to the instance of SQL Server with login account having sysadmin rights.
3. Execute the following T-SQL script to create the logins and their corresponding users

in the AdventureWorks2012 database for James and Peter:
--Creating Login and User in
--AdventureWorks2012 database for James
USE [master]
GO
CREATE LOGIN [James] WITH PASSWORD=N'JamesPass123'
,DEFAULT_DATABASE=[AdventureWorks2012]
,CHECK_EXPIRATION=OFF
,CHECK_POLICY=OFF
GO
USE [AdventureWorks2012]
GO
CREATE USER [James] FOR LOGIN [James]
GO
ALTER ROLE [db_owner] ADD MEMBER [James]
GO
--Creating Login and User in AdventureWorks2012 database for Peter
USE [master]
GO
CREATE LOGIN [Peter] WITH PASSWORD=N'PeterPass123'
,DEFAULT_DATABASE=[AdventureWorks2012]
,CHECK_EXPIRATION=OFF
,CHECK_POLICY=OFF
GO
USE [AdventureWorks2012]
GO
CREATE USER [Peter] FOR LOGIN [Peter]
GO
ALTER ROLE [db_owner] ADD MEMBER [Peter]
GO

Now, we will create a trace and capture only events that occur for AdventureWorks2012 database from James' session only. To do this, follow these steps:

1. Start SQL Server Profiler.
2. Select New Trace… from the File menu. In the Connect to Server dialog box, provide connection details of SQL Server hosting the AdventureWorks2012 database and click on Connect.
3. In the General tab of Trace Properties, enter FilteringEvents as the Trace name and select Blank template for the Use the template: drop-down menu as shown
in following:

4. In Events Selection tab, check the checkbox for event class SQL:BatchCompleted under the TSQL event category as shown in following screenshot:

5. Click on Column Filters… button.

6. In the Edit Filter dialog box, select DatabaseName from the list of available data columns on the left. Expand the Like option and enter string value
AdventureWorks2012; then press the OK button as shown in the following screenshot:

7. In the Edit Filter dialog box, select SessionLoginName from the list of available data columns on the left. Expand the Like option and enter string value James; then press the OK button as shown in following screenshot:

8. Click on the Organize Columns… button in Events Selection tab of Trace Properties dialog box. Select TextData data column and then keep clicking on Up button repeatedly to move the column up the order in the list, until the column appears as the second item, at the top of the list underneath EventClass data column. Do this same exercise also for the data columns DatabaseName and SessionLoginName so that the final order of the data columns should look like as shown in following screenshot. Press OK in the Organize Columns dialog box:

9. Click on the Run button to run the trace in the Trace Properties dialog box.

Now, we will open two instances of SQL Server Management Studio one by one that connect to SQL Server with the logins James and Peter respectively and run a few queries.
1. Open the first instance of SSMS and connect to SQL Server with the login credentials of James. In the query window, type and execute the T-SQL statements as shown in following script:

USE [AdventureWorks2012]
GO
SELECT * FROM [Sales].[Customer]
GO
USE [master]
GO
SELECT * FROM sys.databases
GO

2. Open a second instance of SSMS and connect to SQL Server with the login credentials of Peter. In the query window, type and execute the same T-SQL
queries as shown in previous step.
3. Switch to SQL Server Profiler window that is running the trace. Examine the trace data as shown in following screenshot:

Tips:Use of DatabaseID
We can alternatively use DatabaseID data column instead of DatabaseName to specify a filter on a particular database. For this, we must know system
assigned ID value for a specific database. This value can be retrieved by either calling DB_ID('AdventureWorks2012') metadata function or querying sys.databases catalog view.

The following section lists some of data columns that are commonly used in trace filters:

  • ApplicationName: A filter can be specified on this data column so that only trace events raised by a particular client application are captured
  • DatabaseID: A filter can be specified on this data column so that only trace eventsraised for a specific database are captured
  • DatabaseName: A filter can be specified on this data column so that only trace events raised for a specific database are captured
  • HostName: A filter can be specified on this data column so that only trace events raised from a specific host or client machine are captured
  • LoginName: A filter can be specified on this data column so that only trace events raised by a specific login are captured
  • ObjectID: A filter can be specified on this data column so that only trace events raised for a specific object are captured
  • ObjectName: A filter can be specified on this data column so that only trace events raised for a specific object are captured
  • SessionLoginName: A filter can be specified on this data column so that only trace events raised by a specific login are captured
  • SPID: A filter can be specified on this data column so that only trace events raised from a specific session connection are captured

Microsoft.SQL.Server2012.Performance.Tuning.Cookbook学习笔记(一)的更多相关文章

  1. Microsoft.SQL.Server2012.Performance.Tuning.Cookbook学习笔记(二)

    Creating trace with system stored procedures Following are the stored procedures which you should kn ...

  2. 《Microsoft Sql server 2008 Internals》读书笔记--第六章Indexes:Internals and Management(1)

    <Microsoft Sql server 2008 Internals>索引文件夹: <Microsoft Sql server 2008 Internals>读书笔记--文 ...

  3. Spark SQL 之 Performance Tuning & Distributed SQL Engine

    Spark SQL 之 Performance Tuning & Distributed SQL Engine 转载请注明出处:http://www.cnblogs.com/BYRans/ 缓 ...

  4. SQL Server2012 T-SQL基础教程--读书笔记(1-4章)

    SQL Server2012 T-SQL基础教程--读书笔记(1-4章) SqlServer T-SQL 示例数据库:点我 Chapter 01 T-SQL 查询和编程背景 1.3 创建表和定义数据的 ...

  5. SQL Server2012 T-SQL基础教程--读书笔记(8 - 10章)

    SQL Server2012 T-SQL基础教程--读书笔记(8 - 10章) 示例数据库:点我 CHAPTER 08 数据修改 8.1 插入数据 8.1.1 INSERT VALUES 语句 8.1 ...

  6. SQL Server2012 T-SQL基础教程--读书笔记(5-7章)

    SQL Server2012 T-SQL基础教程--读书笔记(5-7章) SqlServer T-SQL 示例数据库:点我 Chapter 05 表表达式 5.1 派生表 5.1.1 分配列别名 5. ...

  7. SQL必知必会学习笔记

    2.5  select SELECT       要返回的列或表达式     是FROM          从中检索数据的表        仅在从表选择数据时使用WHERE        行级过滤   ...

  8. Inside Microsoft SQL Server 2008: T-SQL Querying 读书笔记之查询优化

    一. 自顶向下优化方法论 1. 分析实例级别的等待 在实例级找出什么类型的等待占用大部分的时间,通过sys.dm_os_wait_stats select wait_type, --等待类型 wait ...

  9. sql分类及基本sql操作,校对规则(mysql学习笔记二)

    sql针对操作对象分为不同语言 数据操作(管理)语言 DML或者将其细分为 ( 查询  DQL 管理(增,删,改)  DML) 数据定义语言(对保存数据的格式进行定义) DDL 数据库控制语言(针对数 ...

随机推荐

  1. Iterm2 快捷键介绍

    Mac 原来自带的终端工具 Terminal 不好用是出了名的,虽然最近几个版本苹果稍微做了些优化,功能上,可用性方面增强不少,无奈有个更好用的 Iterm2 摆在那,基本上也就没有多少出场机会了 I ...

  2. Python 爬取高清桌面壁纸

    今天写了一个脚本用来爬取ZOL桌面壁纸网站的高清图片: 链接:http://desk.zol.com.cn/1920x1080/ 本程序只爬了美女板块的图片,若要下载其他板块,只需修改程序中的&quo ...

  3. QT生成GUID

    #include <QCoreApplication> #include <QUuid> #include <QDebug> int main(int argc, ...

  4. Node.js概述1

    为什么我们要学习Node.js? 认为: Node.js就学习一周,时间比较短,不重要 将来工作我后端又不用Node.js做,我们又java/python/php/c,为什么要在意它 Node.js接 ...

  5. 微信小程序之上拉加载更多

    loadmore 加载更多(分页加载) 当用户打开一个页面时,假设后台数据量庞大时,一次性地返回所有数据给客户端,页面的打开速度就会有所下降,而且用户只看上面的内容而不需要看后面的内容时,也浪费用户流 ...

  6. 拦截导弹 (最长上升子序列LIS)

    #include <iostream> #include <stdio.h> #include <algorithm> using namespace std; ] ...

  7. jsp之jstl(展示所有商品、重写登录案例)

    jsp之jstl jstl: jsp标准的标签库语言,apache的,是用来替代java脚本 使用步骤: 1.导入jar包 (jstl.jar和standard.jar) 2.在页面上导入标签库 &l ...

  8. node.js对象数据类型

    在这里复习下前端JS的数据类型:前端JS中的数据类型: 1.基本/原生/值类型 string.number.boolean.null.undefined 2.引用/对象类型    ES对象类型:Str ...

  9. python的数据类型和变量

    数据类型 计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同的数据,需要定 ...

  10. xml入门与解析

    xml入门与解析 1.xml基础知识 xml:可扩展的标签语言,标签自定义. 作用:存储数据.(配置文件) 书写规范: 1.区分大小写 2.应该有一个根标签 3.标签必须关闭 <xx>&l ...