DTA2:DTA Practice

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DTA2:DTA Practice相关的知识,希望对你有一定的参考价值。

DTA依赖于Query Optimizer,对Workload进行analyse, 提供Tuning Recommendation。DTA提高查询语句的性能的能力有限,这与其提供的Tuning Recommendation有关,DTA提供的Tuning Recommendation主要分为三部分:Index,Indexed View 和 Partition。但DTA提供的Tuning recommendation还是十分有用的,可以作为performance tuning 探索的方向。

Optimizing query performance can be difficult without a full understanding the database structure and the queries that are run against the database. The Database Engine Tuning Advisor can make this task easier by analyzing the current query plan cache or by analyzing a workload of Transact-SQL queries that you create and recommending an appropriate physical design. For more advanced database administrators, DTA exposes a powerful mechanism to perform exploratory what-if analysis of different physical design alternatives. The DTA can provide the following information.

  • Recommend the best mix of indexes for databases by using the query optimizer to analyze queries in a workload.

  • Recommend aligned or non-aligned partitions for databases referenced in a workload.

  • Recommend indexed views for databases referenced in a workload.

  • Analyze the effects of the proposed changes, including index usage, query distribution among tables, and query performance in the workload.

  • Recommend ways to tune the database for a small set of problem queries.

  • Allow you to customize the recommendation by specifying advanced options such as disk space constraints.

  • Provide reports that summarize the effects of implementing the recommendations for a given workload.

 

一,Open DTA GUI

从SSMS的Tools Menu中,打开 Database Engine Tuning Advisor。Illustration引用MSDN,DTA主要分为两个Pane。

1,Left pane 是一个Session Monitor,底部是处于选中状态的tuning session的detail info。

The left pane contains the Session Monitor, which lists all tuning sessions that have been performed on this Microsoft SQL Server instance. In the bottom section of this pane, details of the selected tuning session are displayed.

2,Right pane初始状态下,只有两个Tab,General 和 Tuning Options,设置Workload和对Tuning 进行微调。

The right pane contains the General and the Tuning Options tabs. This is where you can define your Database Engine Tuning session. In the General tab, you type the name for your tuning session, specify the workload file or table to use, and select the databases and tables you want to tune in this session. A workload is a set of Transact-SQL statements that execute against a database or databases that you want to tune. Database Engine Tuning Advisor uses trace files, trace tables, Transact-SQL scripts, or XML files as workload input when tuning databases. On the Tuning Options tab, you can select the physical database design structures (indexes or indexed views) and the partitioning strategy that you want Database Engine Tuning Advisor to consider during its analysis. On this tab you also can specify the maximum time that Database Engine Tuning Advisor takes to tune a workload. By default, Database Engine Tuning Advisor will tune a workload for one hour.

技术分享

 

Script1:创建实例数据

use DB_Study
go

Create Table dbo.dt_a
(id int identity not null,
CreatedDateKey int constraint DF_dt_a_CreatedDateKey default(cast(convert(char(8),getdate(),112) as int)),
CreatedDate datetime not null constraint DF_dt_a_CreatedDate default(getdate())
);
go

Create Table dbo.dt_b
(
id int identity not null,
CreatedDateKey int constraint DF_dt_b_CreatedDateKey default(cast(convert(char(8),getdate(),112) as int)),
CreatedDate datetime not null constraint DF_dt_b_CreatedDate default(getdate())
);
go

insert into dbo.dt_a
default values
go 100


insert into dbo.dt_b
default values
go 1000

Script2:在SSMS中编写以下TSQL script,并保存为.sql 文件,文件路径为:D:\\ dt_join.sql 。

在DTA分析之前,先分析一下,首先,两个表都没有聚集index,如果要创建index,那么创建clustered index是首选。其次,On子句是Index的筛选条件,必然作为Index Key。最后,Select 子句show出现的字段中有CreatedDatey,这个字段不作为筛选条件,只是作为查询结果返回,如果创建的Index是nonclustered index,那么CreatedDatey字段作为include column比较合适,能够减少non-leaf level的Index key的大小。

use DB_Study
go

select b.id as ID_A,a.ID as ID_A,b.CreatedDateKey
from dbo.dt_b b
left join dbo.dt_a a
    on b.id=a.id
go

二,在General Tab 中设置Workload

1,设置Session name 为 DTA_ 2016-01-24 2:42:04 PM

2,Workload的类型选择File,File Path是:D:\\ dt_join.sql
3,Database for workload analysis 是DTA 连接SQL Server的初始DB,workload 使用Use Database_name 切换DB,在dt_join.sql 已经使用Use 语句切换到DB_Study。

4,Select database and tables to tune

选择需要进行优化的DB 和Tables,这次的Task是对dt_join.sql 进行优化,所以,我选择DB_Study和该DB里的所有表作为优化对象。

5,Save Tuning Log

  勾选,DTA会将Tuning Log 存储在MSDB的dbo.DTA_tuninglog中。

技术分享

三,在Tuning Options Tab里设置tuning options

技术分享

1,Limit tuning time

DTA 在分析workload的查询性能时,会消耗系统资源,应该避免在系统繁忙时进行performance tuning,勾选 Limit tuning time,并设置stop at,将DTA 设置在特定的时间停止运行,默认的设置是DTA在一个小时后stop

2,Physical Design Structures(PDS) to use in database

  允许DTA 评估创建PDS(index 或indexed view)对性能的提升程度,如果创建新的PDS能够提升性能,那么DTA将返回包含创建PDS的tuning recommendation。

3,Partitioning strategy to employ

  允许DTA 评估 创建partition对性能的提升程度,如果创建新的Partition能够提升性能,那么DTA将返回包含创建Partition的tuning recommendation。

4,Physical Design Structures(PDS) to keep in database

  允许DTA 评估删除PDS(index 或indexed view)对性能的提升程度,如果删除已经存在的PDS能够提升性能,那么DTA将返回包含删除PDS的tuning recommendation。

 

一般情况下,Advanced tuning options使用默认值即可。

技术分享

1,Define max. space for recommendations (MB)   

设置创建PDS消耗的最大space

2,Include plan cache events from all databases                                                   

Specify that plan cache events from all databases are analyzed.

3,Max. columns per index                                                    

Specify the maximum number of columns to include in any index. The default is 1023.

4,Online index recommendations

指定创建Index是否 online,分为三个等级:offline,尽可能online创建index,online。

四,Progress

1,在tool bar中,点击 Start Analysis,DTA开始分析Workload

技术分享

2,Progress Tab

当Start analysis后,多一个Progress Tab,能够查看DTA分析的progress

技术分享

 

分析结束后,可以看到tuning Log有一行记录,多了两个Tab:Recommendations 和Reports。

tuning Log的Reason字段:Statement references only small tables,语句引用的是小表,什么情况,难道DTA对小表查询不屑于调优?

技术分享

 

五,查看Recommendations和Reports Tab

Recommendations Tab 中显示Estimated Improvement 是0%,

Reports Tab中显示 Estimated percentage improvement 是0.00

DTA不会对小表进行优化,那么我就向表中插入更多的数据和数据列,使表变大

技术分享

技术分享

 

六,增加wide columns 和 插入更多数据行。

--enlare 
alter table dbo.dt_a
add col_1 char(1000) not null
constraint DF__dt_a_col_1 default(Naaaaaaaaaaaaaaaaaaaaaaaaaaaa);
go

alter table dbo.dt_a
add col_2 char(1000) not null
constraint DF__dt_a_col_2 default(Nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
go

insert into dbo.dt_a
default values
go 10000

--enlare 
alter table dbo.dt_b
add col_1 char(1000) not null
constraint DF__dt_b_col_1 default(Naaaaaaaaaaaaaaaaaaaaaaaaaaaa);
go

alter table dbo.dt_b
add col_2 char(1000) not null
constraint DF__dt_b_col_2 default(Nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
go

insert into dbo.dt_b
default values
go 10000


七,clone session

在Left pane,选中之前的Session,点击Clone Session,复制这个Session的配置,并rename

技术分享

八,Progress Tab

Tuning Log 中,没有任何记录

技术分享

 

九,Recommendations Tab

Estimated Improvement:96%  DTA预估的性能提升是96%,很不错。

DTA没有提供Partition Recommendations。

DTA提供了两个Index Recommendations。

技术分享

点击 Definition,弹出SQL Script preview,这是Index Recommendation的Definition。

技术分享

下面是创建Index的script,跟预估的index 结构差不多。DTA推荐创建nonclustered index,并且没有进行数据压缩(data_compression),使用定长column的大量空间被闲置,从性能优化的角度看,欠佳。

CREATE NONCLUSTERED INDEX [_dta_index_dt_a_7_1490104349__K1] 
ON [dbo].[dt_a]
(
    [id] ASC
)
WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF) 
ON [PRIMARY]


CREATE NONCLUSTERED INDEX [_dta_index_dt_b_7_1538104520__K1_2] 
ON [dbo].[dt_b]
(
    [id] ASC
)
INCLUDE (     [CreatedDateKey]) 
WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF) 
ON [PRIMARY]

十,Reports Tab
tuning summary 显示DTA 分析Workload的信息汇总

技术分享

tuning Reports 提供15个Reports,用于汇报DTA对Workload的performance tuning信息,还是非常有用的。

技术分享

 

参考文档:

https://msdn.microsoft.com/en-us/library/ms167345.aspx

https://msdn.microsoft.com/en-us/library/ms186232(v=sql.110).aspx

以上是关于DTA2:DTA Practice的主要内容,如果未能解决你的问题,请参考以下文章

不使用Stata软件将dta文件转换为csv

DTA3:Reduce the Production Server Tuning Load

markdown 数码Tanda Tangan Asli(DTA)

有啥方法可以尝试使用 JAVA 读取后缀为“.sas7bdat”、“.dta”、“.sav”和“.xpt”的文件?

GridControl 列合并(自定义分组条件)

GridControl 列合并(自定义分组条件)