Database Administration and Tuning| Môn thiết kế và quản trị cơ sở dữ liệu| Trường Đại học Bách Khoa Hà Nội

Database Administration and Tuning| Môn thiết kế và quản trị cơ sở dữ liệu| Trường Đại học Bách Khoa Hà Nội. Tài liệu gồm 11 trang giúp bạn đọc ôn tập và đạt kết quả cao trong kỳ thi sắp tới. Mời bạn đọc đón xem.

2
Expriments in ICT : Database
Database creation
Instructor : Viet-Trung Tran
Promotion : VN K58
1. Database creation
Given the following relational database scheme
Account(ID, Name, Gender, DateOfBird, Address, DateOfIssue,
Balance)
Transaction(TranID, FromID, ToID, Date, Amount, Description)
Chú thích:
ID: Kiu chui c định 10 kí t
DateOfBird: Kiu Date
Address: Kiu chui tối đa 50 kí tự
Name: Kiu chui ti đa 30 kí tự
DateOfIssue: Ngày to tài khon, kiu date
Balance: giá tr tin hin có ca tài khon, kiu s nguyên
Amount: lượng tin chuyn t tài khon FromID ti ToID, kiu
s nguyên
Dùng ngôn ng SQL to 2 bng trên
a. To rng buộc cho trường ID vi yêu cu ID là chui 10 kí t
bắt đầu bng BKA. Ví d BKA2012232
b. Tạo index cho trường FromID, ToID (B-tree), Gender (Bitmap)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"#$%&'(#)'!*+!,*($-'#&!./0# ) /# !%) 1 !2 ) 30) # #&0) 3 !!!!! !!!!!!!456474568!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"#$#%#&'!()*+,+&$-#$+.,!#,)!/0,+,1!! !! !!! !! !!! !! !!!4)1!9#(#9'# &!
! !!!!!:%;!<!
!
!
=.>7"2=! ?@3A!6!1#!65! !
!
=)!'B09!C%;!/C%99!D#!D0CC!%$$&*%/B!'B#!+*CC*D0)3!'*$0/9E!
!
23 40'-5!/0,+,1!
23 607'&!.8!$90*%!8.-!:0'-5!$0,+,1!
;3 <,)'=!/0,+,1!
23 >&+,1!$9'!$9'!"#$#%#&'!?,1+,'!/0,+,1!()@+&.-!
A3 ?=B'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! 0'!
%3%0)9'!%!'%;C#!+0#C1!0+! $*9 90; C#A!=)9'#%1H!%$$CK!0'!*)!'B#! 9#%&/B!G%C-#H!+*&!#I%($C#E!SELECT&
*&FROM&customer&WHERE&zip&=&TO_NUMBER('94002')!
!
"#$#!$5B'!C.,@'-&+.,&!%&#!*+'#)!%!$&*;C#(!%)1!D0CC!C0L#CK!/*)+C0/'!D0'B!#I09'0)3!0)1#I#9!
[#A3AH!%)!0)1#I!09!/&#%'#1!*G#&!%!SOV,ROV!%'&0;-'#!)%(#1!dateStrH!;-'!%!J -#&K!%//#99#9!
'B#! %'&0;-'#! %9! 0+! 0'! D#&#! %! 1%'#H! +*&! 0)9'%)/#! 'B&*-3B! %! /*)G#&90*)! +-)/'0*)! 9-/B! %9!
CONVERT(DATETIME,&dateStr)\A! X)C#99! 0($C0/0'! 1%'%! 'K$#! /*)G#&90*)! *//-&9H! #A3A!
;#'D##)!)-(;#&!%)1!9'&0)3Q!+,)'='&!G+77!7+I'75!%'!+1,.-')A!
!
H#I'! C#-'807! 0&'! .8! $9'! R6"?6! ST! C7#0&'A! OG*01! 9*&'0)3! 'B#! &#9-C'9! DB#)! 'B%'! 09! )*'!
9'&0/'CK!)#/#99%&KA!
!
>&'!>L<RL!(PP!+,&$'#)!.8!>L<RLA!QB#)!K*-!-9#!'B#!XT=WT!/C%-9#!'*!/*)/%'#)%'#!'B#!
&#9-C'9! +&*(! 'D*! *&! (*&#! .2:2,>! 9'%'#(#)'9H! 1-$C0/%'#! &#/*&19! %&#! &#(*G#1A! >B09!
1-$C0/%'#! &#(*G%C! &#J-0&#9! %110'0*)%C! /*($-'0)3A! =+! K*-! %&#! )*'! /*)/#&)#1! 'B%'! K*-&!
&#9-C'9! (%K! 0)/C-1#! 1-$C0/%'#! &#/*&19H! -9#! 'B#! XT=WT! O::! /C%-9#H! DB0/B! /*)/%'#)%'#9!
'B#!+-CC!&#9-C'9!+&*(!'B#!.2:2,>!9'%'#(#)'9A!
!
(@.+)! #,$+EC.*B#-+&.,&A! OG*01! 0)9'&-/'0*)9! 9-/B! %9! ]^! *&! TW>H! %9! 'B#K! %&#! C**L0)3! +*&!
DB%'!09!)*'!0)!%!'%;C#!_!'B#!#)'0&#!'%;C#!(-9'!;#!&#%1!&#3%&1C#99A!
!
X9#!<L!$.!$'&$!#1#+,&$!7+$'-#7!@#70'&!%)1!?U<N/N!$.!C-'#$'!#!C.--'7#$+.,!%'$G'',!#!C#77+,1!
:0'-5! #,)! #! &0%:0'-5A! =T! D0 CC ! /%-9#! %! 9-;J-#&K! '*! ;#! #I#/-'#1! 0)! 0'9! #)'0&#'K! ;#+*&#!
$%990)3!'B#!&#9-C'!'*!'B#!/%CC0)3!J-#&KA!2`=.>.!D0CC!9'*$!*)/#!%!&#9-C'!09!+*-)1A!!
!
(@.+)! 0&+,1! $9'! R6! .-! $9'! <L! .B'-#$.-&A! T*'0/#H! +*&! 0)9'%)/#H! 'B%'! SELECT& *& FRO M&
employees& WHERE& state& IN& ('CA',& 'IL',& 'KS')! 09! 'B#! 9%(#! %9! SELECT& *& FROM& employees&
WHERE& state& =& 'CA'& OR& state& =& 'IL'& OR& state& =& 'KS',! %)1! ;*'B! %&#! /*9'CK! J-#&0#9! '*!
#I#/-'#A!>B#!J-#&K!*$'0(0N#&!D0CC!%CD%K9!$#&+*&(!%!'%;C #!9/%)![*&!%!/C-9'#&#1!0)1#I!9/%)!
*)!%)!0)1#I#1!'%;C#\!0 +!'B#!QR2V2!/C %-9#!0)!'B#!J-#&K!/*)'%0)9!%)!WV!*$#&%'*&H!%)1!0+!
%)K!*+!'B#!&#+#&#)/#1!/*C-()9!0)!'B#!WV!/C%-9#!1*#9!)*'!B%G#!%)!0)1#I!D0'B!'B#!/*C-()!
%9!'B#!9#%&/B!L#KA! =+! K*-!-9#!(%)K!J-#&0#9! /*)'%0)0) 3! WV!/C%-9#9!*&! =T! * $#&%'*&9H! K*-!
D0CC!D%)'!'*! #)9- &#!'B%'!#%/B!&#+#&#)/#1! /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H!'B#!TUNING!'#($C%'#!/%$'-&#9!*)CK!(0)0(%C!#G#)'9H!9*!'B#&#!9B*-C1!
)*'! ;#! %)K! 903)0+0/%)'! $#&+*&(%)/#! 0 ($%/'! *)! K*-&! 9#&G#&A! O! '#/B)0J-#! +*&! D* &LC*%1!
/*($&#990*)! 09! %C9*! #($C*K#1H! $%&'0'0*)0)3! D*&LC*%19! D0'B! ;%909! *)! %! 903)%'-&#! *+! #%/B!
J-#&K![0A#AH!'D*!J-#&0#9!B%G#!'B#!9%(#!903)%'-&#!0+!'B#K!%&#!01#)'0/%C!0)!%CC!%9$#/'9!#I/#$'!
+*&!'B#!/*)9'%)'9!&#+#&#)/#1!0)!'B#!J-#&K\A!
!
A3!?=B'-+*',$&!#,)!?='-C+&'&!
!
A323 (!D-#C$+C#7!?='-C+&'!>&+,1!$9'!"#$#%#&'!?,1+,'!/0,+,1!()@+&.-!
!
O9!(%'#&0%C9!+*&!'B09!/C%99H!D#!B%G#!$&*G01#1!%!D*&LC*%1![Queries4Workload.sql\!%3%0)9'!
'B#!AdventureWorks2012!databaseA!Q#!&#/*((#)1!'B%'!K*-!-9#!'B09!D*&LC*%1H!'*!3#'!%!
B%)19c*)! $#&9$#/'0G#! *+! 'B#! ">OA! OC'#&)%'0G#CK! '*! $&*G010)3! %)! .F:! 9/&0$'! D0'B! 'B#!
D*&LC*%1H! K*-! /%)! -9#! 'B#! .F:! ?&*+0C#&! >**C! '*! 3%'B#&! %! 9K9'#(! '&%/#H! %)1! $&*G01#! 'B09!
'&%/#!%9!0)$-'!'*!'B#!">OA!
!
O99-(0)3!'B%'!'B#!30G#)!D*&LC*%1!09!&#$&#9#)'%'0G#!*+!'B#!J-#&0#9!'B%'!D*-C1!;#!#I#/-'#1!
"%'%;%9#!O1(0)09'&%'0*)!%)1!>-)0)3!
=.>7"2=! ?@3A!k!1#!65! !
!
%3%0)9'! 'B#! 1%'%;%9#H! K*-! /%)! -9#! 0'! %9! %)! 0)$-'! '*! 'B#! " >OH! DB0/B! D0CC! 'B#)! 3#)#&%'#!
&#/*((#)1%'0*)9A!Y*-!/%)!$#&+*&(!*)#!*+!'B#!+*CC*D0)3!'D*!'K$#9!*+!%)%CK909A!
!
A.&Keep&my&existing&Physical&Design&Structures&and&tell&me&what&else&I&am&missing&
!
>B09!'K$#!*+!%)%CK909!09!/*((*)!%)1!09!-9#+-C!0+!K*-!B%G#!$&#G0*-9CK!#9'%;C09B#1!'B#!9#'!*+!
0)1#I#9!'B%'!K*-!1##(!'*!;#!(*9'!-9#+-C!+*&!K*-&!30G#)!D*&LC*%1H!%)1!%&#!9##L0)3!+-&'B#&!
&#/*((#)1%'0*)9A!>*!/*)1-/'!'B09!%)%CK909E!
!
23 <,+$+#$'!#!,'G!&'&&+.,!+,!$9'! "/(Q! %5! 7#0,C9+,1! $9'! C.--'&B.,)+,1! $..7! 8-.*! $9'!
Z+,).G&!*',0!G+$9!$9'!N4P!N'-@'-!D'-8.-*#,C'!/..7&Q!.-!%5!&'7'C$+,1!$9+&!
$..7!8-.*!$9'!$..7&!*',0!G+$9+,!$9'!N4P!N'-@'-!H#,#1'*',$!N$0)+.3!
;3 O9..&'!$9'!B-.@+)')!G.-I7.#)!#&!$9'!+,B0$!$.!$9+&!&'&&+.,3!
A3 <,!$9'![Select&databases&and&tables&to&tune\!&'C$+.,Q!&'7'C$!()@',$0-'Z.-I&;]2;3!
^3 <,!$9'! [Database& for& workload&analysis\! )-.B).G,Q! 0&'! ()@',$0-'Z.-I&;]2;!.-!
/'*B)%3!!
_3 ($!$9'![Tuning&Options\!$#%Q!&'7'C$!$9'!8.77.G+,1!.B$+.,&`!
W2X D95&+C#7!"'&+1,!N$-0C$0-'&!$.!0&'!+,!)#$#%#&'!Ea!<,)'='&!#,)!<,)'=')!@+'G&!
W;X D95&+C#7!"'&+1,!N$-0C$0-'&!$.!I''B!+,!)#$#%#&'!Ea!b''B!#77!'=+&$+,1!D"N!
c3 >,C9'CI!$9'!C9'CI%.=!8.-!7+*+$!$0,+,1!$+*'3!
d3 J+$!N/(6/!(L(PTN<N!#,)!"/(!G+77!&$#-$!C.,&0*+,1!5.0-!G.-I7.#)3!
!
W)/#! ">O! +0)09B#9! /*)9-(0)3! 'B#! D*&LC*%1H! 0'! D0CC! C09'! 0'9! &#/*((#)1%'0*)9! -)1#&! 'B#!
&#/*((#)1%'0*)9!'%;A!
!
B.&Ignore&my&existing&Physical&Design&Structures&and&tell&me&what&query&optimizer&needs&
!
=)!'B#!$&#G0*-9!9/#)%&0*H!">O!(%L#9!&#/*((#)1%'0*)9!+*&!%)K!(0990)3!0)1#I#9A!R*D#G#&H!
'B09!1*#9!)*'!)#/#99%&0CK!(#%)!K*-&!#I09'0) 3!0)1#I#9!%&#!*$'0(%C!+*&!'B#!J-#&K!*$'0(0N#&A!
Y*-! (%K! %C9*! /*)901#&! /*)1-/'0)3! %)! %)%CK909! DB#&#;K! ">O! 03)*&#9! %CC! #I09'0)3! $BK90/%C!
1#903)! 9'&-/'-&#9! %)1! &#/*((#)19! DB%'! 0'! 1##(9! 'B#! ;#9'! $*990;C#! 9#'! +*&! 'B#! 30G#)!
D*&LC*%1A!>B09!D%KH!K*-!/%)!G%C01%'#!K*-&!%99-($'0*)9!%;*-'!DB%'!0)1#I#9!%&#!&#J-0&#1A!
!
>*!/*)1-/'!'B09!%)%CK909H!+*CC*D!9'#$9!6!'*!j!*+!'B#!$&#G0*-9!9/#)%&0*H!#I/#$'!'B%'!%'!9'#$!
h[4\H!/B**9#!Do&not&keep&any&existing&PDSA!
!
,*)'&%&K! '*! B*D! 'B09! (03B'! 9*-)1H! 'B#! ">O! D0CC! )*'! %/'-%CCK! 1&*$! * &! 1#C#'#! %)K! #I09'0)3!
$BK90/%C!1#903)!9'&-/'-&#9A!>B09 !09!'B#!; 033#9'!%1G%)'%3#!*+!-90 )3!">OH!%9!0'!(#%)9!K*-!/%)!
-9#!'B#!'**C!'*! $#&+*&(!what7if!%)%CK909!D0'B*-'!%/'-%CCK!0)'&*1-/0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!
!
/\!QB%'!09!'B#!'*'%C!%(*-)'!*+!9%C%&0#9!$%01!;K!#%/B!1#$%&'(#)'g!
1\!R*D!(%)K!#($C*K##9!D#&#!B0&#1!0)!'B#!/-&&#)'!K#%&g!
!
QB0/B!0)10/#9!D*-C1!K*-!/&#%'#!*G#&!'B#!&#C%'0*)g!a*&!#%/B!0)1#IH!0)10/%'#!'B #!'K$#![B%9B!
*&!Mp'&##\!%)1!0)10/%'#!0+!'B#!0)1#I!09!/C-9'#&#1!*&!)*)c/C-9'#&#1A!q-9'0+KA!
!
| 1/11

Preview text:

2 Expriments in ICT : Database Database creation Instructor : Viet-Trung Tran Promotion : VN – K58 1. Database creation
Given the following relational database scheme
Account(ID, Name, Gender, DateOfBird, Address, DateOfIssue, Balance)
Transaction(TranID, FromID, ToID, Date, Amount, Description) Chú thích:
ID: Kiểu chuỗi cố định 10 kí tự DateOfBird: Kiểu Date
Address: Kiểu chuỗi tối đa 50 kí tự
Name: Kiểu chuỗi tối đa 30 kí tự
DateOfIssue: Ngày tạo tài khoản, kiểu date
Balance: giá trị tiền hiện có của tài khoản, kiểu số nguyên
Amount: lượng tiền chuyển từ tài khoản FromID tới ToID, kiểu số nguyên
Dùng ngôn ngữ SQL tạo 2 bảng trên a.
Tạo rằng buộc cho trường ID với yêu cầu ID là chuỗi 10 kí tự
bắt đầu bằng BKA. Ví dụ BKA2012232 b.
Tạo index cho trường FromID, ToID (B-tree), Gender (Bitmap)
Department of Computer Science and Engineering 2012/2013
Database Administration and Tuning 2nd semester Lab 8
In this lab class we wil approach the fol owing topics: 1. Query Tuning
1. Rules of thumb for query tuning 2. Index Tuning
1. Using the the Database Engine Tuning Advisor
3. Experiments and Exercises
1. A Practical Exercise Using the Database Engine Tuning Advisor
2. Implementing the Database Engine Tuning Advisor recommendations 3. Exercise 1. Query Tuning
SQL Server uses cost-­‐based optimization, i.e. it tries to find the execution plan with the
lowest possible cost, where cost means both the time the query wil take to execute and
the hardware resources that wil be used. Basically, the query optimizer is looking to
minimize the number of logical reads required to fetch the required data.
The bad news is that it is not magic, and the optimizer does not always come up with the
best solution. A database administrator should be aware of the factors that govern query
optimization, what pitfalls there are, and how the query optimizer can be assisted in its
job. Database administrators who know well their data can often influence the optimizer
to choose certain indexes, in order to come up with the most efficient solution.
1.1. Rules of Thumb for Query Tuning
There are some very basic guidelines for writing efficient SQL code. These guidelines
largely constitute nothing more than writing queries in the proper way. In fact, it might
be quite surprising to learn that as you work with a relational database, one of the most
common causes of performance problems can usually be tracked down to poorly coded
queries. We will now discuss in general terms what, in SQL statements, is good for performance, and what is not.
Make careful use of the HAVING clause. HAVING is intended to filter records from the
result of a GROUP BY, and a common mistake is using it to filter records that can be
more efficiently filtered using a WHERE clause. IST/DEI Pág. 1 de 10
Database Administration and Tuning
Make careful use of the DISTINCT clause. DISTINCT can cause an additional sort
operation, and it should be avoided except when strictly necessary.
• Make careful use of functions. They simply should not be used where you expect an
SQL statement to use an index. When using a function in a query, do not execute it
against a table field if possible. Instead, apply it on the search value, for example: SELECT
* FROM customer WHERE zip = TO_NUMBER('94002')
Data type conversions are often a problem and wil likely conflict with existing indexes
(e.g., an index is created over a VARCHAR atribute named dateStr, but a query accesses
the atribute as if it were a date, for instance through a conversion function such as
CONVERT(DATETIME, dateStr)). Unless implicit data type conversion occurs, e.g.
between number and string, indexes will likely be ignored.
Make careful use of the ORDER BY clause. Avoid sorting the results when that is not strictly necessary.
Use UNION ALL instead of UNION. When you use the UNION clause to concatenate the
results from two or more SELECT statements, duplicate records are removed. This
duplicate removal requires additional computing. If you are not concerned that your
results may include duplicate records, use the UNION ALL clause, which concatenates
the ful results from the SELECT statements.
Avoid anti-­‐comparisons. Avoid instructions such as != or NOT, as they are looking for
what is not in a table — the entire table must be read regardless.
• Use IN to test against literal values and EXISTS to create a correlation between a calling
query and a subquery. IN wil cause a subquery to be executed in its entirety before
passing the result to the calling query. EXISTS wil stop once a result is found.
Avoid using the OR or the IN operators. Notice, for instance, that SELECT * FROM
employees WHERE state IN ('CA', 'IL', 'KS') is the same as SELECT * FROM employees
WHERE state = 'CA' OR state = 'IL' OR state = 'KS', and both are costly queries to
execute. The query optimizer wil always perform a table scan (or a clustered index scan
on an indexed table) if the WHERE clause in the query contains an OR operator, and if
any of the referenced columns in the OR clause does not have an index with the column
as the search key. If you use many queries containing OR clauses or IN operators, you
wil want to ensure that each referenced column has an index. A query with one or more
OR clauses, or using the IN operator, can sometimes be rewritten as a series of queries
that are combined with a UNION statement, in order to boost the performance.
• For optimizing joins, the fol owing rules of thumb apply: IST/DEI Pág. 2 de 10
Database Administration and Tuning
o Use equality first, and only use range operators where equality does not apply.
o Avoid the use of negatives in the form of != or NOT.
o Avoid LIKE pattern matching.
o In the relations being joined, try to retrieve specific rows, and in small
numbers, so that only a small number of rows is actually involved in the join operation(s).
o Filter large tables before applying a join operation, to reduce the number of
rows that is joined. Also access tables from the most highly filtered,
preferably the largest, downward. Notice that this is importante to reduce
the number of rows that is involved in the join operation(s).
o Use indexes wherever possible except for very small tables.
Regarding joins, nested sub-­‐queries can be difficult to tune but can often be a viable tool,
and sometimes highly effective, for tuning mutable complex joins, with three and
sometimes many more tables in a single query. The term mutable complex joins refers to
join queries involving more than two tables, that are mutable in the sense that different
join orders can be considered, and that are complex in the sense that they involve other
selections on the tables that are being joined. Using nested sub-­‐queries, it might be easier
to tune such queries, because one can tune each sub-­‐query independently. 2. Index Tuning
In terms of tuning, the option that produces maximum gains with least impact on existing
systems and processes is to examine your indexing strategy. However, the task of
identifying the right indexes is not necessarily straightforward. It requires a sound
knowledge of the sort of queries that wil be run against the data, the distribution of that
data, and the volume of data, as wel as an understanding of what type of index wil best
suit your needs. Consider the fol owing query:
Select A, COUNT(*) FROM T WHERE X < 10 GROUP BY A;
The following different physical design structures can reduce the execution cost of this query: (i) A clustered index on X;
(i ) Table range partitioned on X;
(iii) A non-­‐clustered index with key X)and including the additional atribute A;
(iv) A materialized view that matches the query, and so on.
These alternatives can have widely varying storage and update characteristics. Thus, in the IST/DEI Pág. 3 de 10
Database Administration and Tuning
presence of storage constraints, or for a workload containing updates, making a global
choice for a workload is difficult. For example, a clustered index on a table and horizontal
partitioning of a table are both non-­‐redundant structures (i.e., they incur negligible
additional storage overhead) whereas non-­‐clustered indexes and materialized views can
be potentially storage intensive and involve higher update costs. However, non-­‐clustered
indexes and materialized views can often be much more beneficial than a clustered index
or a horizontally partitioned table. Clearly, a physical design tool that can give an
integrated physical design recommendation can greatly reduce/eliminate the need for a
DBA to make ad-­‐hoc decisions.
While understanding the basics is stil essential, SQL Server does offer a helping hand in
the form of some tools – in particular, the Database Engine Tuning Advisor – that can
help to determine, tune and monitor your indexes. It can be used to get answers to the fol owing questions:
• Which indexes are needed for specific queries?
• How to monitor index usage and its effectiveness?
• How to identify redundant indexes that could negatively impact performance?
• As the workload changes, how to identify missing indexes that could enhance
performance for the new queries?
2.1. Using the Database Engine Tuning Advisor
Determining exactly the right indexes for your system can be quite a taxing process. For
example, you have to consider:
• Which columns should be indexed, based on the knowledge on how the data is queried.
• Whether to choose a single-­‐column index or a multiple column index.
• Whether to use a clustered index or a non-­‐clustered index.
• Whether one could benefit from an index with included columns.
• How to utilize indexed (i.e., materialized) views.
Moreover, once you have determined the perfect set of indexes, your job is not finished.
Your workload wil change over time (i.e., new queries wil be added, and older ones
removed) and this might warrant revisiting existing indexes, analyzing their usage and
making adjustments (i.e., modifying or dropping existing indexes and creating new ones).
Maintenance of indexes is critical to ensure optimal performance in the long run.
The Database Engine Tuning Advisor (DTA) is a physical design tool providing an
integrated console where DBAs can tune all physical design features supported by the
server. The DTA takes into account all aspects of performance that the query optimizer IST/DEI Pág. 4 de 10
Database Administration and Tuning
can model, including the impact of multiple processors, amount of memory on the server,
and so on. It is important to note, however, that query optimizers typically do not model
all the aspects of query execution (e.g., impact of indexes on locking behavior, impact of
data layout etc.). Thus, DTA’s estimated improvement can be different from the actual
improvement in execution time.
Taking as input a workload to fine-­‐tune, i.e., a set of SQL statements that execute against
the database server, the DTA produces a set of physical design recommendations,
consisting of indexes, materialized views, and strategies for horizontal range partitioning
of tables, indexes and views. The basis of DTA’s recommendations is a what-­‐if analysis
provided by the SQL Server query optimizer, which allows the computation of an
estimated cost as if a given configuration (e.g., the existence of some indexes) was
materialized in the database. Similarly to the actual evaluation of a given query plan, the
query optimizer component can do an evaluation considering the what-­‐if existence of a
given physical design structure.
You can tune a single query or the entire workload to which your server is subjected. A
workload can be obtained, for instance, by using SQL Server Profiler, i.e., a tool for logging
events (e.g., queries) that execute on a server. In this case, the workload would be given
to the DTA in the form of a trace file, obtained with the SQL Server Profiler. The Profiler
tool is just used to col ect the workload, whereas the DTA performs the actual analysis and the tuning suggestions.
Alternatively, a workload can be specified as an SQL file containing an organization or
industry benchmark. In this case, a text file with the SQL for each query in the workload would be given to the DTA.
The DTA can also take as input workloads referring to either a single or to a set of
databases, as many applications use more than one database simultaneously.
Based on the options that you select, you can use the DTA to make recommendations for
several Physical Design Structures (PDS), including: • Clustered indexes
• Non-­‐clustered indexes
• Indexes with included columns (to avoid bookmark lookups) • Indexed views • Partitions
The first step is to col ect a workload for DTA to analyze. You can do this in one of two ways: IST/DEI Pág. 5 de 10
Database Administration and Tuning •
Using the Management Studio – If you need to optimize the performance of a
single query, you can use Management Studio to provide directly an input to
DTA. Type the query in Management Studio, highlight it and then right click on
it to choose Analyze in Database Engine Tuning Advisor. •
Using the Profiler – If you want to determine the optimum index set for the
entire workload, corresponding to the actual queries that are being executed
against an SQL Server instance, you should col ect a profiler trace with the
TUNING template (i.e., one of the possible options for the trace file that is
generated by the SQL Server Profiler, and that contains all the information that
is required by the Database Engine Tuning Advisor).
To ful y exploit the effectiveness of DTA, you should always use a representative profiler
trace. For instance, the indexes and partitioning considered by the DTA are limited only to
interesting column groups (i.e., those columns that appear in a large fraction of the
queries in the workload that have the highest cost), in order to improve scalability with
little impact on quality. If the profiler trace is not representative of a true workload,
important queries wil likely be missing.
You should make sure that you subject your server to all the queries that wil typically be
run against the data, while you are col ecting the trace. This could lead to a huge trace file,
but that is normal. If you simply col ect a profiler trace over a 5-­‐10 minute period, you can
be pretty sure it wil not be truly representative of all the queries executed against your database.
In the SQL Profiler, the TUNING template captures only minimal events, so there should
not be any significant performance impact on your server. A technique for workload
compression is also employed, partitioning workloads with basis on a signature of each
query (i.e., two queries have the same signature if they are identical in all aspects except
for the constants referenced in the query).
3. Experiments and Exercises
3.1. A Practical Exercise Using the Database Engine Tuning Advisor
As materials for this class, we have provided a workload (Queries4Workload.sql) against
the AdventureWorks2012 database. We recommend that you use this workload, to get a
hands-­‐on perspective of the DTA. Alternatively to providing an SQL script with the
workload, you can use the SQL Profiler Tool to gather a system trace, and provide this trace as input to the DTA.
Assuming that the given workload is representative of the queries that would be executed IST/DEI Pág. 6 de 10
Database Administration and Tuning
against the database, you can use it as an input to the DTA, which wil then generate
recommendations. You can perform one of the fol owing two types of analysis.
A. Keep my existing Physical Design Structures and tell me what else I am missing
This type of analysis is common and is useful if you have previously established the set of
indexes that you deem to be most useful for your given workload, and are seeking further
recommendations. To conduct this analysis:
1. Initiate a new session in the DTA, by launching the corresponding tool from the
Windows menu with the SQL Server Performance Tools, or by selecting this
tool from the tools menu within the SQL Server Management Studio.

2. Choose the provided workload as the input to this session.
3. In the “Select databases and tables to tune
” section, select AdventureWorks2012.
4. In the “Database for workload analysis
” dropdown, use AdventureWorks2012 or Tempdb.
5. At the “Tuning Options” tab, select the following options: (1)
Physical Design Structures to use in database -­‐> Indexes and Indexed views (2)
Physical Design Structures to keep in database -­‐> Keep all existing PDS
6. Uncheck the checkbox for limit tuning time.
7. Hit START ANALYSIS and DTA will start consuming your workload.

Once DTA finishes consuming the workload, it wil list its recommendations under the recommendations tab.
B. Ignore my existing Physical Design Structures and tell me what query optimizer needs
In the previous scenario, DTA makes recommendations for any missing indexes. However,
this does not necessarily mean your existing indexes are optimal for the query optimizer.
You may also consider conducting an analysis whereby DTA ignores all existing physical
design structures and recommends what it deems the best possible set for the given
workload. This way, you can validate your assumptions about what indexes are required.
To conduct this analysis, fol ow steps 1 to 6 of the previous scenario, except that at step
5(2), choose Do not keep any existing PDS.
Contrary to how this might sound, the DTA wil not actually drop or delete any existing
physical design structures. This is the biggest advantage of using DTA, as it means you can
use the tool to perform what-­‐if analysis without actually introducing any changes to the underlying schema. IST/DEI Pág. 7 de 10
Database Administration and Tuning
After consuming the workload, DTA presents, under the recommendations tab, a set of
tuning recommendations. A good idea is to focus on the fol owing sections:
Recommendation – this is the action that you need to take. Possible values include Create or Drop.
Target of Recommendation – this is the proposed name of the physical design
structure to be created. The naming convention is typical of DTA and generally starts
with _dta*. However, it is recommended that you change this name based on the
naming convention in your database.
Definition – this is the list of columns that this new physical design structure wil
include. If you click on the hyperlink, it wil open up a new window with the T-­‐SQL script
to implement this recommendation.
Estimated Improvements – this is the estimated percentage improvement that you can
expect in your workload performance, if you implement all the recommendations made by DTA.
Space used by recommendation (MB) – under the Tuning Summary section of the
Reports tab, you can find out the extra space in MB that you would need, if you decide
to implement these recommendations.
The reports tab features several in-­‐built analysis reports. There are 15 built-­‐in reports,
but the fol owing three are the most important.
Current Index Usage Report -­‐ Start with this report to see how your existing indexes
are being used by the queries running against your server. Each index that has been
used by a query is listed here. Each referenced index has a Percent Usage value which
indicates the percentage of statements in your workload that referenced this index. If
an index is not listed here, it means that it has not been used by any query in your
workload. If you are certain that all the queries that run against your server have been
captured by your profiler trace, then you can use this report to identify indexes that are
not required and possibly delete them.
Recommended Index Usage Report -­‐ Look at this report to identify how index usage
wil change if the recommended indexes are implemented. If you compare these two
reports, you wil see that the index usage of some of the current indexes has fallen
while some new indexes have been included with a higher usage percentage, indicating
a different execution plan for your workload and improved performance.
Statement Cost Report -­‐ This report lists individual statements in your workload and
the estimated performance improvement for each one. Using this report, you can IST/DEI Pág. 8 de 10
Database Administration and Tuning
identify your poorly performing queries and see the sort of improvement you can
expect if you implement the recommendations made by DTA. You wil find that some
statements don't have any improvements (Percent improvement = 0). This is because
either the statement was not tuned for some reason or it already has all the indexes
that it needs to perform optimally.
3.2 Implementing the Database Engine Tuning Advisor Recommendations
By now, we have col ected a workload using Profiler, consumed it using the DTA, and got a
set of recommendations to improve performance. You then have the choice to either:
Save recommendations – you can save the recommendations in an SQL script by
navigating to ACTIONS -­‐> SAVE RECOMMENDATIONS. You can then manually run the
script in Management Studio to create all the recommended physical design structures.
Apply recommendations using the DTA – if you are happy with the set of
recommendations, then simply navigate to ACTIONS -­‐> APPLY RECOMMENDATIONS.
You can also schedule a later time to apply these recommendations, for instance during
off-­‐peak hours so that interference with other operations is minimal.
Performing what-­‐if analysis is a very useful feature of the DTA. You may not want to apply
all the recommendations that the DTA provided. However, since the Estimated
Improvement value can only be achieved if you apply all of these recommendations
together, you are not really sure what kind of impact it wil have if you only choose to
apply a sub-­‐set of these recommendations.
To do a what-­‐if analysis, deselect the recommendations that you do not want to apply.
Now, go to ACTIONS -­‐> EVALUATE RECOMMENDATIONS. This wil launch another session
with the same options as the earlier one. However, when you click on START ANALYSIS,
the DTA wil provide data on estimated performance improvements, based on just this
sub-­‐set of the recommendations. Again, the key thing to remember is that the DTA
performs this what-­‐if analysis without actually implementing anything in the database. 3.3. Exercise
Consider the fol owing normalized relation where the primary key is ID:
Employees(ID, name, salary, department, contract_year)
Consider as wel the fol owing four queries equally important and frequent:
a) What is the average number of employees per department?
b) Which are the IDs of the employees with the highest salary? IST/DEI Pág. 9 de 10
Database Administration and Tuning
c) What is the total amount of salaries paid by each department?
d) How many employees were hired in the current year?
Which indices would you create over the relation? For each index, indicate the type (hash
or B+tree) and indicate if the index is clustered or non-­‐clustered. Justify. IST/DEI Pág. 10 de 10