Microsoft.SQL.Server2012.Performance.Tuning.Cookbook学习笔记(一)
一、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学习笔记(一)的更多相关文章
- Microsoft.SQL.Server2012.Performance.Tuning.Cookbook学习笔记(二)
Creating trace with system stored procedures Following are the stored procedures which you should kn ...
- 《Microsoft Sql server 2008 Internals》读书笔记--第六章Indexes:Internals and Management(1)
<Microsoft Sql server 2008 Internals>索引文件夹: <Microsoft Sql server 2008 Internals>读书笔记--文 ...
- Spark SQL 之 Performance Tuning & Distributed SQL Engine
Spark SQL 之 Performance Tuning & Distributed SQL Engine 转载请注明出处:http://www.cnblogs.com/BYRans/ 缓 ...
- SQL Server2012 T-SQL基础教程--读书笔记(1-4章)
SQL Server2012 T-SQL基础教程--读书笔记(1-4章) SqlServer T-SQL 示例数据库:点我 Chapter 01 T-SQL 查询和编程背景 1.3 创建表和定义数据的 ...
- SQL Server2012 T-SQL基础教程--读书笔记(8 - 10章)
SQL Server2012 T-SQL基础教程--读书笔记(8 - 10章) 示例数据库:点我 CHAPTER 08 数据修改 8.1 插入数据 8.1.1 INSERT VALUES 语句 8.1 ...
- SQL Server2012 T-SQL基础教程--读书笔记(5-7章)
SQL Server2012 T-SQL基础教程--读书笔记(5-7章) SqlServer T-SQL 示例数据库:点我 Chapter 05 表表达式 5.1 派生表 5.1.1 分配列别名 5. ...
- SQL必知必会学习笔记
2.5 select SELECT 要返回的列或表达式 是FROM 从中检索数据的表 仅在从表选择数据时使用WHERE 行级过滤 ...
- Inside Microsoft SQL Server 2008: T-SQL Querying 读书笔记之查询优化
一. 自顶向下优化方法论 1. 分析实例级别的等待 在实例级找出什么类型的等待占用大部分的时间,通过sys.dm_os_wait_stats select wait_type, --等待类型 wait ...
- sql分类及基本sql操作,校对规则(mysql学习笔记二)
sql针对操作对象分为不同语言 数据操作(管理)语言 DML或者将其细分为 ( 查询 DQL 管理(增,删,改) DML) 数据定义语言(对保存数据的格式进行定义) DDL 数据库控制语言(针对数 ...
随机推荐
- POJ1151 离散化求矩形面积的并
/*第一道离散化的题目,虽然是水题,不过还是很高兴...*/ #include<cstdio> #include<algorithm> #include<cstring& ...
- LintCode_41 最大子数组
题目 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 注意事项 子数组最少包含一个数 样例 给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1] ...
- CI框架--URL路径跳转与传值
CI框架使用URL的前提是需要加载辅助函数$this->load->helper('url');当然我建议大家将所有需要加载的东西写在构造方法内,这样就不需每个控制器每个方法都去调用一次了 ...
- Javascript模块化编程(一)模块的写法最佳实践
Javascript模块化编程,已经成为一个迫切的需求.理想情况下,开发者只需要实现核心的业务逻辑,其他都可以加载别人已经写好的模块但是,Javascript不是一种模块化编程语言,它不支持类clas ...
- 洛谷P1312 [NOIP2011提高组Day1T3]Mayan游戏
Mayan游戏 题目描述 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个 7 行5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游 ...
- select2 如何自定义提示信息-布布扣-bubuko.com
标签:color dom layui href 默认事件 替换 each ase options 最近项目中使用到了select2来美化和丰富下拉框的功能,这款插件功能 ...
- web前端学习(二)html学习笔记部分(3)--range对象
1.2.8 html5编辑api之range对象(一) 1.2.8.1 Range 对象基本概念 Range 对象的基本概念,通过使用 Range 对象所提供的方法实现一个鼠标选取内容,通过点击按 ...
- 如何高效地在github上找开源项目学习?
1.高级条件组合(精确搜索) in:readme 微服务 stars:>1000 in:readme spring security stars:>3000 in:name python ...
- Android——app基础
Android Application基础 系统启动过程 APK文件介绍 APK是Android Package的缩写,即android安装包.APK 文件其实是zip 格式,但后缀名被修改为apk ...
- 【python小随笔】celery周期任务(简单原理)
1:目录结构 |--celery_task |--celery.py # 执行任务的main函数 |--task_one # 第一个任务 |--task_two # 第2个任务 . . . . |-- ...