Download - Themis Top 25 Db2 Tuning Presentation
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
1/58
Top 25+ DB2 SQLTuning Tips for
Developers
Presented by Tony Andrews, Theis !n"#t$ndrews%theisin"#"o
mailto:[email protected]:[email protected] -
7/25/2019 Themis Top 25 Db2 Tuning Presentation
2/58
&b'e"tives
By the end of this present$tion, developers should be $ble to(
) *nderst$nd wh$t SQL &ptii$tion is
) *nderst$nd St$ge versus St$ge 2 predi"$tes) *nderst$nd wh$t the DB2 -.pl$in Tool does
) *nderst$nd wh$t the /isu$l -.pl$in "$n tell us
) *nderst$nd wh$t $0es 1ueries run too long) Le$rn wh$t to do when they see $ t$ble s"$n t$0ing pl$"e#
) Le$rn steps to tuning $ 1uery
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
3/58
3y -.perien"e Shows
) Th$t the $'ority of perfor$n"e probles $ong$ppli"$tions $re "$used by poorly "oded progr$s oriproperly "oded SQL#
) Th$t poor perforing SQL is responsible for $s u"h $s4 per"ent of responsetie issues#
) Th$t ost developers need edu"$ted in how to re$d $nd$n$lye the DB2 -.pl$in output#
) There $re $ny SQL developers in the !T industry, but !h$ve found th$t less th$n 6 of the re$lly 0now the
perfor$n"e issues involved with SQL progr$ing orhow to fi. the#
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
4/58
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
5/58
Developers should $lw$ys progr$ with
perfor$n"e in ind#
Progr$ers should $lw$ys h$ve two go$ls in ind when developing
progr$s $nd $ppli"$tions for user groups#
) To get the "orre"t results for re1uested d$t$
) To get the results b$"0 $s 1ui"0ly $s possible
So $ny ties progr$ers tod$y lose sight of the se"ond go$l# They
either(
) Do not 0now wh$t to do to get progr$s to run f$ster
) Bl$e other pie"es of their environent 7d$t$b$se, networ0, T8P9!P,wor0st$tion, oper$ting syste, et"#:, or
) Thin0 th$t the tie spent pro"essing the d$t$ w$s pretty good b$sed on
the $ount of d$t$#
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
6/58
The DB2 &ptiier
Catalog StatisticsObject Definitions
Access Path
Access Path Hint
RID Pool Sorts
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
7/58
St$ge versus St$ge 2 Predi"$tes
) St$ge 7DB2 D$t$ 3$n$ger: is responsible for tr$nsl$ting the d$t$
stored on p$ges into $ result set of rows $nd "oluns# Predi"$tes
th$t $re written in $ f$irly str$ightforw$rd w$y "$n usu$lly be
ev$lu$ted by the D$t$ 3$n$ger with rel$tively little e.pense#
) St$ge 2 7;el$tion$l D$t$ Servi"es: h$ndle ore "ople. predi"$tes,
d$t$ tr$nsfor$tions, $nd "oput$tions# These St$ge 2 predi"$tes
$re u"h ore e.pensive for DB2 to resolve th$n St$ge due to
$ddition$l pro"essing $nd $ddition$l "ode p$th# Addition$lly, ;DS
"$nnot $0e effe"tive use of inde.es#
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
8/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
9/58
Top 2+ Tuning Tips ;&3 -3PL&@-- -;- @-A;7!;-DAT-: C 25
Should be "oded $s(
S-L-8T -3P?&, LAST?A3- >;&3 -3PL&@--
-;- !;-DAT- B-T--? 25E $nd 252FE
V&' Can no( create indexes on S)* expressions#
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
10/58
Top 25+ Tuning Tips ;&3 -3PL&@-- >;&3 -3PL&@--
-;- SALA;@ H 5# 9 # -;- !;-DAT- H DAT-7K:
+ J DA@S
V&' Can no( create indexes on S)* expressions
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
11/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
12/58
Top 25+ Tuning Tips irst 9 &rder By within sub1ueries#
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
13/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
14/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
15/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
16/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
17/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
18/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
19/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
20/58
Top 25+ Tuning Tips ". Only code the columns needed in the Select portion o the S)* statement.
$ving e.tr$ "oluns "$n h$ve $n $ffe"t on(
The optiier "hoosing !nde. &nlyE -.pensiveness of $ny sorts
&ptiierEs "hoi"e of 'oin ethods
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
21/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
22/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
23/58
Top 25+ Tuning Tips or -.$ple(
Non Correlated Su3uery Can also e coded as'
S-L-8T -#-3P?&, -#LAST?A3- S-L-8T -#-3P?&, -#LAST?A3-
>;&3 -3PL&@-- - >;&3 -3PL&@-- - -;- -#-3P?& !? -;- -U!STS
7S-L-8T D#3;?& 7S-L-8T
>;&3 D-PA;T3-?T D >;&3 D-PA;T3-?T D
-;- D#D-PT?& L!- D6V: -;- D#3;?& C -#-3P?&
A?D D#D-PT?& L!- D6E:
V&' 8loal )uery Optimization. Optimizer no( tries to determine ho( an access path o one
3uery loc$ may aect the others. #his can e seen at times y D12 re(riting an ,Exists-
su3uery into a @oin0 or an ,?n- su3uery into an ,Exists- su3uery .
V& optimization calls this ,Correlating- and ,DeAcorrelating-.
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
24/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
25/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
26/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
27/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
28/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
29/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
30/58
Top 25+ Tuning Tips re1uen"y /$lue st$tisti"s on $ny "olun7s: with uneven distribution roup st$tisti"s 7distin"t v$lues over $ group of "oluns: for $ny set of "oluns th$t
$re "orrel$ted
V&' Qu$ntile St$tisti"s 7further bre$0down of st$tisti"s:# elps with r$nge predi"$tes,
between predi"$tes, $nd the li0e predi"$te# -spe"i$lly where there e.ists ot SpotsEof d$t$ distribution#
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
31/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
32/58
Top 25+ Tuning Tips or e.$ple( There e.ists $ t$ble with illion rows# !n this t$ble e.ists $n inde. on
the "olun St$tus8ode# After $ typi"$l ;unst$ts utility is e.e"uted $g$inst this t$ble,
the optiier will 0now th$t there $re F different v$lues for the st$tus "ode# After $
spe"i$l ;unst$ts th$t spe"ifies fre1uen"y v$lue st$tisti"s for th$t "olun, DB2 will 0now
the following d$t$ distributions(
St$tus 8ode v$lue AE "ont$ins 56 of the d$t$
St$tus 8ode v$lue BE "ont$ins M56 of the d$t$
St$tus 8ode v$lue 8E "ont$ins 56 of the d$t$#
A progr$ "ont$ins the following SQL# The v$lue is $lw$ys 8E th$t "oes into
the progr$(
S-L-8T 8&L, 8&L2, 8&LF
>;&3 TABL-
-;- STAT*S8D C (&ST/A;!ABL-
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
33/58
Top 25+ Tuning Tips ;&3 -3PL&@-- -
-;- -U!STS
7S-L-8T
>;&3 D-PA;T3-?T D
-;- D#3;?& C -#-3P?&
A?D D#D-PT?& L!- D6E:
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
34/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
35/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
36/58
Top 25+ Tuning Tips or e.$ple(
S-L-8T -3P?&, LAST?A3-, D-PT?&, SALA;@
>;&3 -3PL&@--
-;- D-PT?& H K
&PT!3!W- >&; M ;&S
!f you 0now for sure th$t your s"reen will only pro"ess M rows no $tter how
$ny get returned, then the SQL st$teent should spe"ify >et"h >irst M ;ows
&nlyE#
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
37/58
Top 25+ Tuning Tips . ?nsert (ith Select V6".
DB2 /4 introdu"ed the $bility to now sele"t wh$t w$s 'ust inserted with the s$est$teent s$ving ultiple "$lls to DB2# This $g$in we "$ll ;el$tion$lE progr$ing
inste$d of Pro"edur$lE progr$ing# The st$teent "$n retrieve the following
infor$tion#
!dentity "oluns or se1uen"e v$lues th$t get $uto$ti"$lly $ssigned by DB2
*serdefined def$ults $nd e.pressions th$t $re not 0nown to the developer 8oluns odified by triggers th$t "$n v$ry fro insert to insert depending on v$lues
;&!Ds, 8*;;-?T T!3-STA3P th$t $re $ssigned $uto$ti"$lly
>or e.$ple(
S-L-8T 8, 82, 85, 8O, 84, 8, 82 >;&3 >!?AL TABL-
7!?S-;T 782, 8F, 8M, 85, 8O, 84, 8:
/AL*-S 7AB8E, 2F#2, AE, D->E,
5#, !E, 24E:
:
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
38/58
Top 25+ Tuning Tips ;&3 -3PL&@-- -
-;- ?&T -U!STS
7S-L-8T
>;&3 D-PA;T3-?T D
-;- D#3;?& C -#-3P?&:
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
39/58
Top 25+ Tuning Tips !;ST?A3-, LAST?A3-,
D-PA;T3-?T:
&? -#-3P?& C ?-!T-3#-3P?&
-? 3AT8-D T-?
*PDAT- S-T >!;ST?A3- C ?-!T-3#>!;ST?A3-,
LAST?A3- C ?-!T-3#LAST?A3-
-? ?&T 3AT8-D T-?
!?S-;T 7-3P?&, >!;ST?A3-, LAST?A3-, D-PA;T3-?T:
/AL*-S 7?-!T-3#-3P?&, ?-!T-3#>!;ST?A3-,
?-!T-3#LAST?A3-, ?-!T-3#D-PA;T3-?T:
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
40/58
Top 25+ Tuning Tips et"h the ;&!D 7;!D $s of /N: for e$"h row being pro"essed in the
;e$d &nlyE "ursor, $nd e.e"ute $ll upd$te or delete st$teents using the;&!D9;!D v$lue in pl$"e of the 0ey fields for better perfor$n"e#
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
41/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
42/58
Top 25+ Tuning Tips
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
43/58
Top 25+ Tuning Tips $"ility
F: -.tern$l SPs needs $ l$ngu$ge environent in whi"h to e.e"ute# This is
provided by $ L3$n$ged $ddress sp$"e# Thre$ds get suspended $s
T8Bs get $ssigned#
M: hen $ n$tive SP is "$lled, DB2 swit"hes fro the "$llers p$"0$ge to the
stored pro"edure p$"0$ge# ?o thre$d suspension $nd t$s0 swit"hing, $nd
no del$y in stored pro"edure e.e"ution
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
44/58
Top 25+ Tuning Tips un"tion 3ode
J: SQL "ode runs $s $ interpretive l$ngu$ge $nd is in"luded $s p$rt of the
p$"0$ge# $t"h out for SPs th$t "ont$in $ lot of "ode#
4: -.tern$l 7to DB2: resour"es $re not re1uired to prep$re or to e.e"ute $
n$tive SQL pro"edure# >or e.$ple "opiles $nd lo$d libr$ries#
N: ?o L3 $ppli"$tion e.e"ution environent( when $ n$tive SQL pro"edure
is "$lled it e.e"utes in the DB2 d$t$b$se servi"es $ddress sp$"e 7DB3:#
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
45/58
?$tive SP -.$ple
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
46/58
?$tive SP -.$ple
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
47/58
?$tive SP -.$ple A*LT X
D-8LA;- " 8*;S&; >&;
S-L-8T s$l$ry >;&3 st$ff &;D-; B@ s$l$ryX
D-8LA;- "2 8*;S&; !T ;-T*;? >&;
S-L-8T n$e, 'ob, s$l$ry
>;&3 st$ff-;- s$l$ry H edi$nS$l$ry
&;D-; B@ s$l$ryX
D-8LA;- -U!T A?DL-; >&; ?&T >&*?D
S-T edi$nS$l$ry C X
S-L-8T 8&*?T7G: !?T& vnu;e"ords >;&3 STA>>X
&P-? "X
!L- v"ounter R 7vnu;e"ords 9 2 + : D&>-T8 " !?T& edi$nS$l$ryX
S-T v"ounter C v"ounter + X
-?D !L-X
8L&S- "X
&P-? "2X
-?D
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
48/58
Ten Steps to Tuning $ Query
: 8he"0 every predi"$te# Are they inde.$ble $nd St$ge K2: !s there $ Distin"tEK !s it neededK 8$n it be rewrittenK Try roup B@E#
F: Are there sub1ueriesK ;ewrite !nE $s -.istsE $nd vi"e vers$#
M: 8he"0 DB2 st$tisti"s on every t$ble $nd every "olun involved#
5: 8he"0 the nuber of ties every SQL st$teent is getting e.e"uted# 8$n the
logi" be "h$nged to "ut down the nuber of ties re1uests $re sentKO: 8he"0 the DB2 -.pl$in# Are there $ny t$ble s"$nsK Any sortsK
J: 8he"0 the DB2 /-# Are there $ny predi"$tes with "r$y >ilter >$"torsK
4: 8he"0 the DB2 -.pl$in# !f there is $ 'oin, wh$t is the order of t$bles "hosenK
N: Are there $ny "orrel$ted sub1ueriesK 8$n they be !nde.&nlyK
: Are there $ny "oluns in predi"$tes with uneven distribution st$tisti"sK Shouldthe v$lue be h$rd "odedK
: Are there $ny r$nge predi"$tes# 8ould histogr$ st$tisti"s helpK
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
49/58
?ew DB2 /4 SQL >e$tures
9ollo(ing are some o the ne( S)* eatures in D12 V6'
: 3ore St$ge predi"$tes
2: 3ulti ;ow >et"h, *pd$te, $nd !nsert
F: 3ultiple Distin"ts
M: -.pressions in the roup ByE
5: 8oon T$ble -.pression
O: Dyn$i" S"roll$ble 8ursors
J: Se1uen"es versus !dentity 8oluns
4: 3$teri$lied Query T$bles 73QTs:
N: ;e"ursive SQL
: 3ore effi"ient use of inde.es# >orw$rd $nd B$"0w$rd s"$ns: ?ew U3L fun"tions $nd d$t$types
2: ?ew et Di$gnosti"sE for w$rning $nd error infor$tion
F: Sele"t fro $n !nsert st$teent
M: S"$l$r >ullsele"t within $ Sele"tE, 8$seE, *pd$te, et"#
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
50/58
?ew DB2 /N SQL >e$tures
9ollo(ing are some o the ne( S)* eatures in D12 V&'
: Set oper$tions !nterse"tE $nd -."eptE
2: 3erge st$teent for *psertE pro"essing# !nsert or *pd$te
F: &LAP fe$tures for ;$n0ing $nd ?ubering of d$t$
M: ?$tive SQL Stored Pro"edures
5: !nste$d ofE Triggers
O: ?ew support $nd SQL for U3L d$t$
J: &ptii$tion Servi"e 8enter 7&S8:
4: Distin"t sort $void$n"e with non uni1ue inde.es
N: !nde.ing on -.pressions
: St$tisti"s on /iews: S0ipped lo"0ed d$t$
2: Trun"$te st$teent
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
51/58
?ew DB2 /N SQL >e$tures
9ollo(ing are some o the ne( S)* eatures in D12 V&'
F: Arr$y host v$ri$bles now supported for stored pro"edures
M: Tiest$p $uto upd$te for inserts $nd *pd$tes
5: &ptiisti" lo"0ing
O: ?ew D-8>L&AT d$t$type
J: Sele"t fro *pd$te or Delete getting old or new v$lues
4: >et"h >irst, &rder B@ within sub1ueries
N: ;-&PT A*T& 7Dyn$i" SQL:
2: D$t$ Studio for ?$tive Stored Pro"edures
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
52/58
-.perien"e Shows
Th$t $ little 0nowledge goes $ long w$y in !T developent shops
When you know something, say what you know. When you don't know something, say that you
don't know. That is knowedge.!
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
53/58
-.perien"e Shows
Th$t good tr$ining goes $ long w$y in !t developent shops#
The mediocre teacher tes.
The good teacher e"pains.The superior teacher demonstrates.
The great teacher inspires.!
i h
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
54/58
-.perien"e Shows
Th$t h$ndson "onsulting $longside developers goes $ long w$yin !T developent shops
#onsuting engagements ne$er seem compete
uness there is a comfortabeness that the cientstaff fuy understands a issues, and has theabiity to mo$e forward on their own.!
i Sh
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
55/58
-.perien"e Shows
Th$t st$nd$rds, guidelines, $nd w$l0throughs s$ves 8P* "osts
$nd in"ident reporting in !T shops#
There is aways time for a %& minute wakthrough!
T i i d 8 lti 8h 0 & t
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
56/58
Tr$ining $nd 8onsulting# 8he"0 &ut
www#theisin"#"o
) &nsite $nd Publi"
) !nstru"tor led
)$ndson) 8ustoi$tion
) -.perien"e
) &ver 25 DB2 "ourses
) &ver M !T "oursesUS !A6>>A;7:A+>>>
?ntl. !A&>6A2++A6&>>
-du"$tion 8he"0 out
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
57/58
9inallyG oo$ o D12 S)* tuning tips or
developers0 speciically designed to improveperormance.
D12 S)* developers no( have a handy
reerence guide (ith tuning tips to improve
perormance in 3ueries0 programs and
applications.
-du"$tion# 8he"0 out
www#db2s1ltuningtips#"o
-
7/25/2019 Themis Top 25 Db2 Tuning Presentation
58/58
Th$n0 @ou for $llowing us $t Theis to
sh$re soe of our e.perien"e $nd
0nowledgeY
) e hoped th$t you le$rned soething new tod$y) e hope th$t you $re $ little ore inspired to "ode with
perfor$n"e in ind
ha$e noticed that when the de$eopers get
educated, good ()* programming standards are in
pace, and program wakthroughs are e"ecutedcorrecty, incident reporting stays ow, #+ costs do not get
out of contro, and most performance issues are foundbefore promoting code to production.!