一、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. geoserver与OpenLayers配置

          geoserver与OpenLayers配置         目录   1     准备工作.... 4 1.1      需要用到的程序和资料... 4 2     地图格式转换方式(一 ...

  2. 论ul、ol和dl的区别

    1.ul是无序列表,也就是说没有排列限制可以随意加li: <ul> <li>可以随意放置</li> <li>可以随意放置</li> < ...

  3. homebrew长时间停在Updating Homebrew 这个步骤

    在国内的网络环境下使用 Homebrew 安装软件的过程中可能会长时间卡在 Updating Homebrew 这个步骤. 例:执行 brew install composer 命令 ➜ ~ brew ...

  4. linux-jdk-mysql-tomcat安装

    1.JDK安装 注意:rpm与软件相关命令 相当于window下的软件助手 管理软件 步骤: 1)查看当前Linux系统是否已经安装java 输入 rpm -qa | grep java 1)卸载两个 ...

  5. db link的查看创建与删除 1

    1.查看dblink select owner,object_name from dba_objects where object_type='DATABASE LINK'; 或者 select * ...

  6. day38 08-Spring的id、name和scope顺序

    访问的路径的是/login.id不允许出现特殊的字符./是特殊的字符.Struts 2已经没有/,action的名字已经不带/了.现在的开发中一般使用id这个属性即可. 这个类在被Spring创建的时 ...

  7. 洛谷P1890 gcd区间 [2017年6月计划 数论09]

    P1890 gcd区间 题目描述 给定一行n个正整数a[1]..a[n]. m次询问,每次询问给定一个区间[L,R],输出a[L]..a[R]的最大公因数. 输入输出格式 输入格式: 第一行两个整数n ...

  8. oracle-DML-2

    1.update 语句 update  table set  [column,column......] where  column ='' 示例: update   customers set   ...

  9. FastAdmin 安装后点登录没有反应怎么办?

    FastAdmin 安装后点登录没有反应怎么办? 很多小伙伴安装后点"登录"没有反应. 这个 URL 是对的,但是页面就是不改变. 如果这时候点击 URL 变了,那没有到登陆界面, ...

  10. BZOJ2770: YY的Treap

    原本看标题还以为是treap的题,但是实际上是线段树. 求两点的LCA相当于求区间priority最小值的位置. 然后就可以离线先离散化然后建树做了. 记录的minpos是线段树上叶子结点的节点编号. ...