一、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. css3动画性能优化

    css3的动画简单好用,但是性能方面存在一些问题,很多时候一不留神cpu就已经满了. 现在记下一些常用的技巧,去优化我们的css3的动画. 1. translate3d进行gpu加速 写动画的时候写个 ...

  2. python 获取天气信息

    [说明]接口为聚合数据接口.API使用说明: 实现代码: import requests,json def main(): #参数 farmat=1 cityname = input("请输 ...

  3. HDU1709

    /*  * 好奇怪的母函数  */ #include<cstdio> #include<cstring> #include<cmath> #include<a ...

  4. win7开机启动项设置

    Windows系统自身就有启动项命令可以进行设置: 要说到修改启动项,当然首推Windows系统自带的“MSCONFIG”命令,XP等其他Windows系统用户使用方法是一模一样的. 1.点击“开始” ...

  5. eclipse配置mybatis xml文件自动提示

    如果使用eclipse中,再写mybatis的xml文件的时候,没有提示,用“Alt+/”,不能把代码用快捷键敲出来,通过下面这个方法,可以解决. 1.下载一个文件,找一个专门的地方保存,配置自动提示 ...

  6. Leetcode71. Simplify Path简化路径

    给定一个文档 (Unix-style) 的完全路径,请进行路径简化. 例如, path = "/home/", => "/home" path = &qu ...

  7. 为什么我要使用Linux,使用Linux的十个理由。

    Linux一来都是做为服务器运行,这些年来,Linux的图形界面已经有了很大的改善,Linux已经成为一个完善的,用户友好的桌面操作系统了,有非多常多的人在使用Linux,下面是我们认为最必要的10个 ...

  8. 创建动态MSSQL数据库表的方法

    代码如下: ImportsSystem.Data ImportsSystem.Data.SqlClient PublicClassForm1 InheritsSystem.windows.Forms. ...

  9. js经典校验之注册与登录校验

    平时都专注于后台功能的实现和逻辑需求的分析及数据库方面的设计,很少关注前端的设计,而项目开发过程中专门负责后台是不太可能的事,所以前端我们也需要会用,除了漂亮的首页等其他的交给美工来做,一些功能性的东 ...

  10. LUOGU P1081 开车旅行 (noip 2012)

    传送门 解题思路 这道题刚了一下午,主要就刚在set那里了.先写了一个暴力70分..之后优化预处理,看着大佬神犇们都用的什么双向链表之类的东西,本蒟蒻不会,又懒得手写平衡树,就拿了个set搞了搞,感觉 ...