The document provides guidelines for developing SQL code including:
1) SQL statements should be laid out clearly with indentation and each on a separate line for readability.
2) Keywords should be uppercase and certain keywords like SELECT should start on a new line.
3) Constants and variables should be on the right side of WHERE and HAVING clauses.
4) Table aliases should be used in queries with multiple tables to improve performance.
The document provides guidelines for developing SQL code including:
1) SQL statements should be laid out clearly with indentation and each on a separate line for readability.
2) Keywords should be uppercase and certain keywords like SELECT should start on a new line.
3) Constants and variables should be on the right side of WHERE and HAVING clauses.
4) Table aliases should be used in queries with multiple tables to improve performance.
The document provides guidelines for developing SQL code including:
1) SQL statements should be laid out clearly with indentation and each on a separate line for readability.
2) Keywords should be uppercase and certain keywords like SELECT should start on a new line.
3) Constants and variables should be on the right side of WHERE and HAVING clauses.
4) Table aliases should be used in queries with multiple tables to improve performance.
The document provides guidelines for developing SQL code including:
1) SQL statements should be laid out clearly with indentation and each on a separate line for readability.
2) Keywords should be uppercase and certain keywords like SELECT should start on a new line.
3) Constants and variables should be on the right side of WHERE and HAVING clauses.
4) Table aliases should be used in queries with multiple tables to improve performance.
Tim% ;",; '%'" O$ I h%at%d 'om%0hat th% data &o%' into th% ta3l% in nam% ord%r" Thu' 98r%d th% *9 &o%' in 8ir't data 3lo$ and 98r%d th% ++++9 &o%' in la't on%" D%2%lo#%r' 'hould 3% a0ar% that) th% %88ii%n. o8 EPISTS and IN i' d%#%ndant on th% amount o8 data in %ah ta3l%" A ?u%r. 0ith IN in it dri2%' 8rom th% 'u3?u%r. a%''in& th% main ?u%r. 8or %ah ro0 r%turn%d) 0h%n) a ?u%r. u'%' EPISTS it dri2%' 8rom th% main ?u%r. a%''in& th% 'u3?u%r. 8or %ah ro0 r%turn%d" So i8 th% 'u3?u%r. r%turn' 8%0 ro0') 3ut) th% main ?u%r. r%turn' a lot o8 ro0' 8or %ah ro0 8rom th% 'u3?u%r. u'% th% IN o#%rator" 1"7 on(t 'erfor) unnecessary *oins. In 'om% #art' o8 our '.'t%m) d%2%lo#%r' ar% :oinin& onto th% '.'K#aram ta3l%" Thi' i' 3ad #rati% and mu't 3% 'to##%d" Th% '.'K#aram' ta3l% 0a' d%'i&n%d to hold 'tati '.'t%m 0id% 2alu%'" Th% orr%t u'% o8 thi' ta3l%) i' 8or th% a##liation od% to &%t th% 2alu%' it r%?uir%' on% at th% 'tart o8 th% od%) or in th% a'% o8 r%&ular u#dat% .l%') at th% 'tart o8 %ah 'uh .l%" At th% lo&ial l%2%l) an. runtim% modi8iation o8 '.'K#aram') #ro3a3l. indiat%' mi''in& %ntiti%'" 1"( "esource intensive o'erations Qu%ri%' 0hih u'% DISTINCT) UNION) >INUS) INTERSECT) ORDER AE and GROUP AE all u#on th% $%rn%l to #%r8orm r%'our% int%n'i2% 'ort'" A DISTINCT r%?uir%' on% 'ort) th% oth%r '%t o#%rator' r%?uir% at l%a't t0o 'ort'" Oth%r 0a.' o8 0ritin& th%'% ?u%ri%' 'hould 3% 8ound" >o't ?u%ri%' that u'% th% '%t o#%rator') UNION)>INUS and INTERSECT) an 3% r%0ritt%n in oth%r 0a.'" 1"; $se realistic test data. R%ali'ti t%'t data) 0hih math%' 3oth in 2olum% and 2alu%') that 'ho0n in th% ER dia&ram' 0ill 3% u'%d" Th% 8ollo0in& i' 3a'%d on th% %!am#l% u'%d in 1"4 a3out r%0ordin& ?u%ri%'" Th%r% ar% no0 +++ #%o#l% in th% ta3l% and +++- :o3'" Thu' 0% ha2% inr%a'%d our #%o#l% 3. a 8ator o8 t%n" Noti% ho0 th% ?u%r. #%r8orm%d 0ith an out%r :oin i' muh ?ui$%r than that #%r8orm%d 3. th% 'u3?u%r." Thi' i' a om#l%t% r%2%r'al o8 th% r%'ult in 1"4" Thi' d%mon'trat%' ?uit% l%arl. that th% '?l d%2%lo#%r mu't ha2% a $no0l%d&% o8 3oth th% 'trutur% o8 th% data and th% t.#ial num3%r o8 ro0' in %ah ta3l%" SELECT #"nam% FRO> #%o#l% #) :o3 : @DERE #"nam% = :"nam%GIC AND :"nam% i' null
8r%d th% ,*; Tim% ,1",- '%'"
SELECT nam% FRO> #%o#l% @DERE not nam% in GSELECT nam% FRO> :o3C
8r%d th% ,*; Tim% *+1"47 '%'"
SELECT nam% FRO> #%o#l% # @DERE not nam% in GSELECT nam% FRO> :o3 : @DERE #"nam% = :"nam%C
8r%d th% ,*; Tim% ;"77 '%'" 1"+ $se of +, o'erator Unl%'' it i' a3'olut%l. n%%''ar. a2oid u'in& th% <= o#%rator" Th% u'% o8 thi' o#%rator di'a3l%' th% u'% o8 ind%!%') 3%au'% th% Oral% $%rn%l a''um%' that th% ?u%r. 0ill 3% r%tri%2in& mo't o8 th% ro0' in th% ta3l%" 1"*- $se Oracle(s trace facility. For %2%r. ?u%r.) %2%n th% 'im#l%'t) h%$ th% out#ut 8rom oral%9' tra% 8ailit." Th% 8ir't 't%# in o#timi'in& ?u%ri%' i' to %liminat% FTS9') thi' an 3% don% u'in& tra%" S%% APPENDIP F 8or d%tail' on th% tra% 8ailit." 1"** #anage)ent of oracle cursors. @h%r% %2%r #o''i3l%) oral% ur'or' 'hould 3% d%lar%d at th% 'tart o8 th% #ro&ram" It i' al'o &ood #rati% to t%ll th% '.'t%m th% ma!imum num3%r o8 ur'or' .ou 0ant) at th% 'tart o8 th% #ro&ram" Thi' an 3% don% in DA 3. o#%nin& th% ur'or' .ou r%?uir%) in ProJ '%t >APCURSORS" Th%r% ar% t0o 9%!#%n'i2%9 o#%ration' durin& th% %!%ution o8 a ?u%r.) a#art 8rom r%turnin& th% ro0'" Th%'% ar% a''oiat%d 0ith th% 9#ar'%9 and 93ind9 #ha'% o8 ?u%r. %!%ution" @hat i' m%ant 3. th% #ar'% #ha'%" Thi' i' th% tim% 0h%n th% '?l t%!t that th% #ro&ramm%r ha' 0ritt%n i' tran'lat%d into an int%rnal r%#r%'%ntation) that th% Oral% $%rn%l an und%r'tand" Th% 8ollo0in& ati2iti%' ha##%n durin& th% #ar'% #ha'%" aC data ditionar. loo$ u# and '%urit. h%$in&" Thi' ati2it. au'%' th% oral% $%rn%l to #rodu% int%rnal '?l ?u%ri%') 0hih ar% all%d r%ur'i2% ?u%ri%') to h%$ 'uh thin&' a' 0h%th%r thi' u'%r i' allo0%d a%'' to thi' olumn %t" 3C ?u%r. o#timi'ation" Part o8 th% ?u%r. o#timi'ation 0hih d%al' 0ith 2i%0' and 'u3 ?u%ri%' i' #%r8orm%d" P%r8ormin& all o8 th% a3o2% an ta$% a on'id%ra3l% tim%" Th%r%8or% th%. 'hould 3% don% a' in8r%?u%ntl. a' #o''i3l%" Th% 3indin& #ha'% ta$%' #la% a8t%r th% #ar'% #ha'%" It i' at thi' 'ta&% that th% u'%r 2aria3l%' ar% d%id%d" For in'tan% in th% ?u%r. '%l%t 9!9 8rom #%r'on 0h%r% nam% = :* th% ho't 2aria3l% * 0ould ha2% it' 2alu% 93ound9 into th% ?u%r. durin& 3indin& 3. ha2in& th% addr%'' o8 :* a''oiat%d 0ith th% ho't 2aria3l%" Und%r normal irum'tan%') 0m' #ro&ram' 'hould #%r8orm th% 8ollo0in& 't%#') 8or a ?u%r. that i' th% 3% %!%ut%d mor% than on%" St%# * Durin& #ro&ram 'tartLu# '%t u# and #ar'% th% ?u%ri%' 0ith all' to '?lK'%tLu#" St%# , E!%ut% th% ?u%r. 0ith a all to '?lK%!%ut% 'u##l.in& th% n%%''ar. 3ind 2aria3l%'" St%# 1 F%th and #ro%'' th% r%turn%d ro0' 3. u'in& '?lK8%th" To %!%ut% th% ?u%r. a&ain) r%turn to 't%# , and 'u##l. n%0 3ind 2aria3l%'" For 8urth%r in8ormation '%% th% doum%nt %ntitl%d DAKu'%r"do" 1"*, Th% *-)*6),- #%r%nt rul% Onl. u'% ind%!%' 8or r%tri%2in& data 8rom ta3l%') 0h%r% .our 0ant l%'' than *6 #%r%nt o8 th% ro0'" FTS9' ar% ?ui$%r i8 .ou 0ant mor% than *6 #%r%nt o8 a ta3l%' data" Th% rul% i' all%d th% *-)*6),- #%r%nt rul%) 3%au'% it d%#%nd' on 0hih oral% doum%nt .ou r%ad) a' to 0h%th%r it' *-)*6 or ,- #%r%nt" 4"- Insert state)ents. 4"* $se of - in insert state)ents Althou&h th% u'% o8 th% 'horthand harat%r 9J9 i' allo0%d 3. th% oral% $%rn%l) it i' on'id%r%d to 3% 3ad #ro&rammin& #rati%" It i' not allo0%d in th% @>S #ro:%t" Th% r%a'on 8or it 3%in& 3ad #rati%) i' that i8 a ta3l% i' modi8i%d durin& an u#&rad% to th% data3a'%) 'u3'%?u%nt in'%rt' ould 8ail out in th% 8i%ld" 6"- $'date State)ents. 6"* Correlated u'dates. On% o8 th% 'lo0%'t ommand' in SQL i' th% UPDATE" Thi' i' lar&%l. du% to th% 8at that mo't orr%lat%d u#dat%' r%?uir% a 8ull ta3l% 'an" Thi' r%'ult' in 2%r. 'lo0 #%r8orman% 0h%n th% ta3l% i' %!tr%m%l. lar&%" Th% 8ollo0in& u#dat% 'tat%m%nt i' t.#ial o8 orr%lat%d u#dat%': U#dat% Tar&%tKTa3l% S%t Tar&%tKFi%ld = GS%l%t Sour%KIn8ormation From Sour%KTa3l% @h%r% Sour%KTa3l%"/%. = Tar&%tKTa3l%"/%.C @h%r% %!i't' GS%l%t 9!9 From Sour%KTa3l% @h%r% Sour%KTa3l%"/%. = Tar&%tKTa3l%"/%.C P%r8orman% #ro3l%m' ari'% 3%au'% th%r% i' no m%thod o8 %liminatin& ro0' in th% Tar&%tKTa3l% 3a'%d on in8ormation in th% Sour%KTa3l%" I8 oth%r ondition' in th% U#dat%9' @h%r% lau'% do not di'?uali8. mo't o8 th% ro0' in th% Tar&%tKTa3l%) thi' u#dat% 0ill r%?uir% 'u3'tantial #ro%''in& tim%" Th% 8ollo0in& PLBSQL od% %88%ti2%l. u'%' an ind%! on th% /%. 8i%ld to a%'' onl. th% a##ro#riat% r%ord' in th% Tar&%tKTa3l%: D%lar% Cur'or Sour% i' S%l%t J From Sour%KTa3l%H A%&in For Ro0 in Sour% Loo# U#dat% Tar&%tKTa3l% S%t Tar&%tKFi%ld = Ro0"Sour%KIn8ormation @h%r% /%. = Ro0"/%.H End Loo#H E!%#tion @h%n OTDERS Th%n NullH EndH Thi' PLBSQL 'ri#t loo#' throu&h %ah o8 th% r%ord' in th% Sour%KTa3l% and u#dat%' th% a##ro#riat% ro0 in th% Tar&%tKTa3l%) i8 an." E''%ntiall.) thi' tran'8%r' th% 8ull ta3l% 'an to th% Sour%KTa3l% and allo0' th% ind%! on th% Tar&%tKTa3l% to 3% u'%d %88%ti2%l." Runnin& a t.#ial orr%lat%d u#dat% on an ;)--- ro0 ta3l% to u#dat% + r%ord' r%?uir%d *+"4 CPU '%ond'" Th% 'am% u#dat% u'in& th% PLBSQL 'ri#t %!%ut%d in *"*, CPU '%ond' LL a +4Q #%r8orman% im#ro2%m%nt" Th% PLBSQL 'ri#t 0ill out#%r8orm th% orr%lat%d u#dat% 0h%n%2%r th% Sour%KTa3l% i' 'mall%r than th% Tar&%tKTa3l%" Th% lar&%r th% Tar&%tKTa3l% om#ar%d to th% Sour%KTa3l%) th% mor% 'u3'tantial th% #%r8orman% &ain" @ith an %88%ti2% ind%!) th% 'iR% o8 th% Tar&%tKTa3l% i' no lon&%r a 8ator in th% tim% r%?uir%d to u#dat% th% ta3l%H th% num3%r o8 r%ord' 3%in& u#dat%d d%t%rmin%' th% #%r8orman%" R%#lain& th% EPISTS 'u3?u%r. 0ith IN 'u3?u%r. 0ill &i2% 'am% im#ro2%m%nt in mo't a'%'" 7"- O'ti)ising oracle .ueries. 7"* Table access. Th%r% ar% thr%% 0a.' that Oral% an 8ind a ro0 in a ta3l%) th%'% ar% : *C San %ah ro0 in th% ta3l% h%$in& 0h%th%r to '%l%t th% ro0 8or out#ut" Thi' i' ommonl. $no0n a' a 8ull ta3l% 'an GFTSC) it i' al'o th% l%a't %88ii%nt m%thod o8 loatin& a ro0" ,C A. u'in& an ind%! to loat% th% ro0" 1C A. u'in& th% ro0id to dir%tl. a%'' th% ro0 on di'" Thi' i' th% mo't %88ii%nt m%thod o8 a%''in& a ro0" Do not u'% thi' m%thod aro'' tran'ation' a' it i' #o''i3l% 8or a ro0id to han&%) u'% th% #rimar. $%. o8 th% ta3l% in th%'% irum'tan%'" Un8ortunat%l. 0% do not normall. $no0 th% ro0id) 'o th% 8a't%'t m%thod o8 a%'' i' 3. th% u'% o8 ind%!%'" Th% &%n%ral rul%' ar% a' 8ollo0': Ind%!%' an 3% u'%d in th% 8ollo0in& irum'tan%'" *C I8 th% ind%!%d olumn i' m%ntion%d in th% 0h%r% lau'%" ,C I8 th% ind%!%d olumn i' not modi8i%d 3. a 8untion or) an arithm%ti o#%ration) or 3oth o8 th%m" Th% 8ollo0in& %!%#tion i' a##lia3l%) a >INGolumnC) >INGolumnIon'tantC or >APGolumnC) >APGolumnIon'tantC" Ind%!%' annot 3% u'%d in th% 8ollo0in& irum'tan%'" *C th%r% i' no 90h%r%9 lau'%" ,C th% ind%!%d olumn i' modi8i%d in an. 0a."
1C th% '%arh i' 8or 9null9 or 9not null9 7", Inde%es and /ull. I8 a r%ord ha' no 2alu% Gi"%" NULLC in th% olumn '#%i8i%d a' th% ind%! $%.) th%n th%r% 0ill not 3% an ind%! %ntr. 8or th% r%ord" For thi' r%a'on th% 8ollo0in& ?u%r. 0ill not u'% an ind%! on olumn CO>> SELECT J FRO> %m# @DERE omm i' NULL Oral% a''um%' that th% ma:orit. o8 r%ord' in a ta3l% 0ill ontain 2alu%' 8or ind%!%d olumn') 3%au'% o8 thi' a FTS 0ill 3% %!%ut%d 8or th% 8ollo0in& ?u%r." SELECT J FRO> %m# @DERE omm i' not NULL A FTS ma. not 3% a##ro#riat% i8 th% olumn CO>> i' '#ar'%l. #o#ulat%d" Th%r% i' an %!am#l% o8 ho0 to alt%r th% ?u%r. to ma$% u'% o8 th% ind%!" SELECT J FRO> %m# @DERE omm F L-"-* 7"1 Inde%es and (/OT,( 'redicates. @h%n a #r%diat% ontain' a Snot %?ualS ondition) Oral% 0ill not on'id%r an ind%!) ho0%2%r) Oral% 0ill int%r#r%t oth%r 9not9 #r%diat%' 'o a' to u'% an. ind%!%' a2aila3l% %"&" 9not 'al F 6-9 =F 9'al M= 6-9 9not 'al M= 6-9 =F 9'al F 6-9 7"4 Grou' by and 'redicate clauses. Th% #%r8orman% o8 &rou# 3. ?u%ri%' an 3% im#ro2%d 3. %liminatin& un0ant%d ro0' %arl. in th% '%l%tion #ro%''" Th% 8ollo0in& t0o ?u%ri%' r%turn th% 'am% data) ho0%2%r) th% '%ond i' #ot%ntiall. ?ui$%r) 'in% ro0' 0ill 3% %liminat%d 3%8or% th% '%t o#%rator' ar% a##li%d" SELECT :o3) a2&G'alC FRO> %m# GROUP AE :o3 DAVING :o3 = 9#r%'id%nt9 OR :o3 = 9mana&%r9 SELECT :o3) a2&G'alC FRO> %m# @DERE :o3 = 9#r%'id%nt9 OR :o3 = 9mana&%r9 GROUP AE :o3 7"6 #ulti'le inde% .ueries. @h%n a ?u%r. ha' t0o or mor% %?ualit. #r%diat% lau'%') multi#l% ind%!%' ma. 3% u'%d" Oral% 0ill m%r&% th% ind%!%' at run tim%) r%turnin& ro0' that ar% in 3oth ind%!%') 8or %!am#l%" SELECT %nam% FRO> %m# @DERE d%#tno=,- AND :o3=9mana&%r9 @ill m%r&% th% 8ollo0in& ind%!%'" non uni?u% ind%! on :o3 non uni?u% ind%! on d%#tno 7"7 0hen inde%es cannot be )erged @h%r% a ?u%r. an u'% 3oth ran&% and %?ualit. #r%diat%') a' in th% 8ollo0in& %!am#l%) oral% annot m%r&% th% ind%!%'" Th% ind%! on :o3 0ill 3% u'%d to loat% th% 9mana&%r9 ro0') th%'% 0ill th%n 3% h%$%d to %n'ur% d%#tno i' &r%at%r than *-" SELECT J FRO> %m# @DERE :o3=9mana&%r9 AND d%#tno F *- ind%!%': non uni?u% ind%! on :o3 non uni?u% ind%! on d%#tno @h%n th%r% i' no l%ar #r%8%r%n% a' to 0hih ind%! to u'%) '%% th% 8ollo0in& ?u%r.) Oral% 0ill u'% onl. on% ind%!) 3%au'% it i' in%88ii%nt to m%r&% th%m" Not% that a 'an o8 th% '%ond ta3l%) 0ill ha2% to 3% don% 8or %ah ro0 r%turn%d in th% 8ir't ta3l%" Sin% 3oth ind%!%' ar% non uni?u%) Oral% 0ill hoo'% th% ind%! 0hih) it %nount%r' 8ir't in th% DCKINDEPES ah%" In #rati%) a' d%2%lo#%r' annot '%% th% ah%) th% hoi% i' ar3itrar." SELECT %nam% FRO> %m# @DERE 'al F * AND %m#no F * ind%!%' : non uni?u% ind%! on %m#no non uni?u% ind%! on 'al 7"( Su''ression of inde%es for 'erfor)ance @h%n th%r% i' a hoi% 3%t0%%n m%r&in& a uni?u% ind%! 0ith a non uni?u% on%) th% $%rn%l 0ill al0a.' u'% th% uni?u% ind%! and a2oid #%r8ormin& th% m%r&%" Th% r%a'onin& 3%hind thi' i' that th% uni?u% ind%! 0ill r%turn onl. on% ro0" S%% th% 8ollo0in& %!am#l%" SELECT %nam% FRO> %m# @DERE 'al = 1--- AND %m#no = (+-, ind%!%' :
uni?u% ind%! on %m#no non uni?u% ind%! on 'al Onl. th% %m#no ind%! 0ill 3% u'%d) i8 a r%ord i' 8ound th%n th% ro0 0ill 3% h%$%d to '%% i8 th% 'al = 1---" Oral% 0ill onl. u'% 6 ind%!%' in a 'in&l% '?l 'tat%m%nt" A8t%r 6 ind%!%' ha2% 3%%n m%r&%d) th% $%rn%l 0ill h%$ tho'% ro0' r%turn%d 8or th% 2alu%' in th% r%mainin& #r%diat%'" SELECT J FRO> %m# @DERE %m#no =(;44 AND :o3 = 9'al%'man9 AND d%#tno = 1- AND 'al = *6-- AND omm = - AND %nam% = 9turn%r9 ind%!%' : non uni?u% ind%! on %m#no non uni?u% ind%! on :o3 non uni?u% ind%! on d%#tno non uni?u% ind%! on 'al non uni?u% ind%! on omm non uni?u% ind%! on %nam% A%au'% all th% #r%diat%' 'or% %?uall.) aordin& to APPENDIP A) onl. 8i2% o8 th% a3o2% ind%!%' 0ill 3% u'%d" Ro0' that ar% r%turn%d 0ill 3% h%$%d 3. th% $%rn%l to '%% i8 th% la't 2alu% i' orr%t" 7"; Concatenated inde%es Conat%nat%d ind%!%' ar% ind%!%' that r%8%r%n% mor% than on% olumn" Th% ind%! 0ill 3% a2aila3l% 8or 'orin&) #ro2id%d that th% 8ir't #art o8 th% ind%! i' #r%'%nt and u'a3l%" In th% 8ollo0in& %!am#l%' a''um% a onat%nat%d ind%! on :o3 and d%#tno" SELECT J FRO> %m# @DERE :o3 = 9#r%'id%nt9 AND d%#tno = *- Th% 8ull ind%! 0ill 3% u'%d" SELECT J FRO> %m# @DERE d%#tno = *- AND :o3 = 9#r%'id%nt9 Th% 8ull ind%! 0ill 3% u'%d" SELECT J FRO> %m# @DERE d%#tno = *- Th% ind%! 0ill not 3% u'%d) th% l%adin& #art o8 th% ind%! i' mi''in& 8rom th% 'tat%m%nt" SELECT J FRO> %m# @DERE :o3 = 9anal.'t9 Th% l%adin& #art o8 th% ind%! 0ill 3% u'%d) d%2%lo#%r' 'hould h%$ th% '%l%ti2it. o8 thi' #art o8 th% ind%!) it ma. not 3% '%l%ti2%) and n%%d' to 3% 'u##r%''%d" SELECT J FRO> %m# @DERE :o3 <= 9l%r$9 AND d%#tno = *- Th% ind%! annot 3% u'%d 3%au'% th% <= #r%2%nt' th% u'% o8 th% l%adin& #art o8 th% ind%!" 7"+ Or o'ti)isation Unli$% th% 9AND9 o#%rator 0hih r%?uir%' that a ro0 #a''%' 3oth #r%diat%') th% 9OR9 o#%rator r%?uir%' a ro0 to #a'' %ith%r o8 th% #r%diat%'" S%% th% 8ollo0in& %!am#l% SELECT %nam%) 'al) :o3 FRO> %m# @DERE 'al = 1--- OR :o3 = 9l%r$9 ind%! : non uni?u% on :o3 I8 th% :o3 ind%! i' u'%d to id%nti8. th% ro0') th%n it 0ould onl. r%turn tho'% %m#lo.%%' 0ho ar% 9l%r$'9) it 0ould not r%turn tho'% %m#lo.%%' 0h%r% th% 'al i' 1--- and :o3 i' not 9l%r$9" A 'in&l% onat%nat%d ind%! on :o3) 'al or 'al):o3 annot 3% u'%d 3%au'% r%ord' 0ith 2alu%' 8or th% '%ond $%.) mu't 3% 8ound r%&ardl%'' o8 th% 2alu% o8 th% 8ir't $%." I8 0% ha2% an ind%! on 'al):o3) th%n 0ith th% ind%! on :o3 th% $%rn%l an o#timi'% th% ?u%r." A 'in&l% olumn ind%! on 'al ould al'o 3% u'%d" I8 th%r% ar% ind%!%' a2aila3l% 8or 3oth th% #r%diat%' that ar% or9d) th%n th% ?u%r. 0ill 3% #ro%''%d on%#tuall.) a' th% union o8 t0o '%l%t 'tat%m%nt'" Thi' i' 'ho0n in th% %!am#l% 3%lo0" SELECT %nam%) 'al) :o3 FRO> %m# @DERE 'al = 1--- OR :o3 = 9l%r$9 ind%!%' : non uni?u% on :o3 non uni?u% on 'al A%om%' :
SELECT %nam%) 'al) :o3 FRO> %m# @DERE :o3 = 9l%r$9 UNION SELECT %nam%) 'al) :o3 FRO> %m# @DERE 'al = 1--- AND :o3 <= 9l%r$9 Noti% that th% $%rn%l ha' u'%d th% la't #r%diat% in th% or lau'% 8or th% 'in&l% ondition ?u%r.) and it ha' u'%d th% 'am% #r%diat% to 8orm th% S<=S ondition in th% dou3l% ondition ?u%r." Noti% that i8 0% r%0rit% th% ?u%r. a' SELECT %nam%) 'al) :o3 FRO> %m# @DERE :o3 = 9l%r$9 OR 'al = 1--- ind%!%' : non uni?u% on :o3 non uni?u% on 'al A%om%' :
SELECT %nam%) 'al) :o3 FRO> %m# @DERE 'al = 1--- UNION SELECT %nam%) 'al) :o3 FRO> %m# @DERE :o3 = 9l%r$9 AND 'al <= 1--- From thi') 0% an '%% that it i' 3%'t to #la% th% #r%diat% a''oiat%d 0ith th% mo't '%l%ti2% ind%! 8ir't in th% 0h%r% lau'%) and th% l%a't '%l%ti2% la't" Thi' minimi'%' th% num3%r o8 h%$' 8or 9<=9" Th%'% 9or9 o#timi'ation' annot ta$% #la% 0h%n th% '?l ?u%r. ontain' a onn%t 3.) or an out%r :oin" 7"*- /on correlated sub .ueries Th%r% ar% t0o a'%' to on'id%r h%r%) 8ir'tl. ?u%ri%' 0hih u'% th% IN o#%rator) and '%ondl.) ?u%ri%' 0hih u'% th% NOT IN o#%rator" @% 0ill d%al 0ith th% IN o#%rator 8ir't" Th% 8ollo0in& rul%' a##l. to o#timi'in& th% ?u%r." *C Th% main and 'u3?u%ri%' ar% o#timi'%d '%#arat%l." ,C Th% 'am% rul%' 8or o#timi'ation a##l. to th% main and 'u3?u%ri%') 8or %!am#l%) in th% ?u%r. 3%lo0 th%r% i' no o#timi'ation o8 th% 'u3?u%r. 3%au'% th%r% i' no 0h%r% lau'%" 1C Th% dri2in& ta3l% i' th% ta3l% 8rom th% 'u3?u%r." In th% %!am#l% 3%lo0 thi' 0ill 3% :o3" 4C Th% 'u3?u%r. i' tran'8orm%d into a :oin 3. th% 8ollo0in& m%thod" Th% ro0' 8rturn%d 8rom th% 'u3?u%r. ar% 'ort%d and du#liat%' r%mo2%d" A 8ull ta3l% 'an o8 th%'% 'ort%d ro0' i' u'%d 8or a%'') 8inall. th% ta3l% in th% main ?u%r. i' :oin%d to th% 'ort%d ro0' 8rom th% 'u3?u%r. u'in& olumn in th% main ?u%r.9' 0h%r% lau'%" SELECT di'tint nam% FRO> m%n @DERE #in in GSELECT #in FRO> :o3C ind%!%' : &i2%' th% 8ollo0in& %!#lain #lan" SORTGUNIQUEC >ERGE 5OIN TAALE ACCESS GFULLC OF 9>EN9 SORTG5OINC TAALE ACCESS GFULLC OF 95OA9 E!%ut% tim% 4(6+ Not% that th% SORTGUNIQUEC i' au'%d 3. th% di'tint" I8 0% no0 #ut a dumm. 0h%r% lau'% on th% 'u3 ?u%r. 0% &%t
SELECT di'tint nam% FRO> m%n @DERE #in in GSELECT #in FRO> :o3 C ind%!%' : uni?u% on :o3G:o3noC non uni?u% on :o3G#inC &i2%' th% 8ollo0in& %!#lain #lan SORTGUNIQUEC NESTED LOOPS TAALE ACCESS GFULLC OF 9>EN9 INDEPGRANGE SCANC OF 95OAK*9 GNONKUNIQUEC Not% that an ind%! ha' 3%%n u'%d on :o3" Thi' i' th% on% a'% 0h%r% an ind%! an 3% u'%d %2%n thou&h th%r% i' no 0h%r% lau'% on th% 'u3?u%r." Th% '?l i' %!%ut%d on%#tuall. a') SELECT di'tint #in FRO> :o3 ORDER AE #in) th%'% 'ort%d ro0' ar% th%n :oin%d 0ith th% ro0' 8rom m%n u'in& th% ind%! :o3G#inC" Th% u'% o8 NOT IN i' not r%omm%nd%d) d%2%lo#%r' 'hould on'id%r r%0ordin& ?u%ri%' u'in& thi' on'trut to u'% out%r :oin') ho0%2%r) it 'hould 3% not%d that out%r :oin' ar% an oral% %!t%n'ion) and ar% not a2aila3l% on oth%r RDA>S9'" S%% 3%lo0 8or %!am#l%" SELECT J FRO> d%#t @DERE d%#tno not in GSELECT d%#tno FRO> %m#C Thi' an 3% r%0ritt%n a' th% 8ollo0in& out%r :oin" SELECT d"J FRO> d%#t d)%m# % @DERE d"d%#tno = %"d%#tnoGIC AND %"ro0id i' NULL Thi' r%li%' on th% 8at that %ah ro0 ha' a uni?u% ro0id) and that) ro0id i' n%2%r NULL" Not% al'o that an. NOT IN i' %?ui2%l%nt to a NOT EPISTS" Th%r%8o% an. NOT EPISTS an 3% tran'8orm%d into an out%r :oin" 7"** Correlated sub .ueries All Corr%lat%d 'u3?u%ri%' 8ollo0 th% 'am% %!%ution #ath" Th% main and th% 'u3?u%ri%' ar% '%#arat%l. o#timi'%d" Th% dri2in& ta3l% in '%l%t%d 8rom th% main ?u%r." For %ah ro0 r%turn%d in th% main ?u%r.) th% 'u3?u%r. i' %!%ut%d" Ind%!%' an 3% u'%d 8or th% 0h%r% lau'%' on 3oth th% main and 'u3?u%r." APPENDIP A Qu%r. Path' Ran$%d in Ord%r o8 S#%%d Th% lo0%r th% ran$) th% 8a't%r th% #ath" Ran$ Path ====== ================================== * RO@ID = on'tant , Uni?u% ind%!%d olumn = on'tant 1 %ntir% uni?u% onat%nat%d ind%! = on'tant 4 %ntir% lu't%r $%. = orr%'#ondin& lu't%r $%. in anoth%r ta3l% in th% 'am% lu't%r 6 %ntir% lu't%r $%. = on'tant 7 %ntir% nonLuni?u% onat%nat%d ind%! = on'tant ( nonLuni?u% 'in&l% olumn ind%! m%r&% ; mo't l%adin& onat%nat%d ind%! = on'tant + ind%!%d olumn AET@EEN lo0 2alu% AND hi&h 2alu%) or ind%!%d olumn LI/E 9CQ9 G3ound%d ran&%C *- 'ortBm%r&% G:oin' onl.C ** >AP or >IN o8 'in&l% ind%!%d olumn *, ORDER AE %ntir% ind%! *1 8ull ta3l% 'an' *4 unind%!%d olumn = on'tant) or olumn IS NULL) or olumn LI/E 9QCQ9 G8ull ta3l% 'anC APPENDIP A Ta3l% Strutur% 8or d%mo' ta3l% o% Gid num3%rG7)-C) o% num3%rG*)-C) tim%'tam#Gdat%CC
Th% 8i%ld id hold' th% uni?u% r%8%r%n% num3%r 'tartin& at * th% 8i%ld o% hold' a - i8 th% id i' %2%n and a * i8 it i' odd uni?u% ind%! on id nonuni?u% ind%! on o% th%r% ar% *--)--- ro0' in o% ta3l% o%Klin$ Go% num3%rG*C)nam% harG*-C 0ith , r%ord'
U'in& PLBSQL to Enhan% U#dat% P%r8orman% On% o8 th% 'lo0%'t ommand' in SQL i' th% UPDATE" >o't o8t%n) 0h%n a li%nt om#lain' a3out #%r8orman%) a lar&%) orr%lat%d u#dat% Gor)0or'%) an unorr%lat%d u#dat%C i' at th% root o8 th% #ro3l%m"O8t%n) %2%n a8t%r r%atin& th% o#timal ind%!%' and 8in% tunin& th% SQL 'tat%m%nt it'%l8) th% u#dat% i' 'till ho&&in& %normou' CPU r%'our%' and i' 'till th% 3ottl%n%$ in th% u'%r9' #rodution 'h%m%" Th% Corr%lat%d U#dat% Thi' i' lar&%l. du% to th% 8at that mo't orr%lat%d u#dat%' r%?uir% a 8ull ta3l% 'an" Thi' r%'ult' in 2%r. 'lo0 #%r8orman% 0h%n th% ta3l% i' %!tr%m%l. lar&%" Th% 8ollo0in& u#dat% 'tat%m%nt i' t.#ial o8 orr%lat%d u#dat%': U#dat% Tar&%tKTa3l% S%t Tar&%tKFi%ld = GS%l%t Sour%KIn8ormation From Sour%KTa3l% @h%r% Sour%KTa3l%"/%. = Tar&%tKTa3l%"/%.C @h%r% %!i't' GS%l%t 9!9 From Sour%KTa3l% @h%r% Sour%KTa3l%"/%. = Tar&%tKTa3l%"/%.C P%r8orman% #ro3l%m' ari'% 3%au'% th%r% i' no m%thod o8 %liminatin& ro0' in th% Tar&%tKTa3l% 3a'%d on in8ormation in th% Sour%KTa3l%" I8 oth%r ondition' in th% U#dat%9' @h%r% lau'% do not di'?uali8. mo't o8 th% ro0' in th% Tar&%tKTa3l%) thi' u#dat% 0ill r%?uir% 'u3'tantial #ro%''in& tim%" In addition) thi' t.#% o8 ?u%r. 'u88%r' 3%au'% an ind%! 0ould not h%l# th% Tar&%tKTa3l% a%'' tim%" In 8at) th% onl. ind%! 0hih 0ill im#ro2% th% #%r8orman% o8 th% #r%%din& u#dat% i' an ind%! on th% Sour%KTa3l% /%. 8i%ld" I8 th% Sour%KTa3l% i' lar&%) thi' i' 2%r. u'%8ul" N%2%rth%l%'') th% 3%'t m%thod o8 u#datin& th% ta3l% 0ould 3% a3l% to a%'' :u't th% orr%t ro0' in th% Tar&%tKTa3l% u'in& an %88%ti2% ind%!" Thi' m%thod i' no0 a2aila3l% u'in& PLBSQL and an ind%! on th% Tar&%tKTa3l% /%. 8i%ld" Th% PLBSQL U#dat% Th% 8ollo0in& PLBSQL od% %88%ti2%l. u'%' an ind%! on th% /%. 8i%ld to a%'' onl. th% a##ro#riat% r%ord' in th% Tar&%tKTa3l%: D%lar% Cur'or Sour% i' S%l%t J From Sour%KTa3l%H A%&in For Ro0 in Sour% Loo# U#dat% Tar&%tKTa3l% S%t Tar&%tKFi%ld = Ro0"Sour%KIn8ormation @h%r% /%. = Ro0"/%.H End Loo#H E!%#tion @h%n OTDERS Th%n NullH EndH Thi' PLBSQL 'ri#t loo#' throu&h %ah o8 th% r%ord' in th% Sour%KTa3l% and u#dat%' th% a##ro#riat% ro0 in th% Tar&%tKTa3l%) i8 an." E''%ntiall.) thi' tran'8%r' th% 8ull ta3l% 'an to th% Sour%KTa3l% and allo0' th% ind%! on th% Tar&%tKTa3l% to 3% u'%d %88%ti2%l." P%r8orman% Gain Runnin& a t.#ial orr%lat%d u#dat% on an ;)--- ro0 ta3l% to u#dat% + r%ord' r%?uir%d *+"4 CPU '%ond'" Th% 'am% u#dat% u'in& th% PLBSQL 'ri#t %!%ut%d in *"*, CPU '%ond' LL a +4Q #%r8orman% im#ro2%m%nt" Th% PLBSQL 'ri#t 0ill out#%r8orm th% orr%lat%d u#dat% 0h%n%2%r th% Sour%KTa3l% i' 'mall%r than th% Tar&%tKTa3l%" Th% lar&%r th% Tar&%tKTa3l% om#ar%d to th% Sour%KTa3l%) th% mor% 'u3'tantial th% #%r8orman% &ain" @ith an %88%ti2% ind%!) th% 'iR% o8 th% Tar&%tKTa3l% i' no lon&%r a 8ator in th% tim% r%?uir%d to u#dat% th% ta3l%H th% num3%r o8 r%ord' 3%in& u#dat%d d%t%rmin%' th% #%r8orman%" R%#lain& th% EPISTS 'u3?u%r. 0ith IN 'u3?u%r. 0ill &i2% 'am% im#ro2%m%nt in mo't a'%'" APPENDIP D GUIDELINES FOR USING TDE OUTER 5OIN SENTAP Guid%lin%' 8or U'in& th% Out%r 5oin S.nta! RDA>S RDA>S Su##ort V7"- Oto3%r *++- * INTRODUCTION Th% #ur#o'% o8 thi' t%hnial not% i' to #ro2id% 'om% &uid%lin%' on ho0 to u'% th% out%r :oin 8ailit. #ro2id%d 3. ORACLE" *"* Out%r 5oin S%manti' L D%8inition' Th% 8ollo0in& t%rm') u'%d to d%'ri3% th% o#%ration on out%r :oin') ar% d%8in%d :L 9out%rL:oin olumn9 L a olumn r%8%r%n% 8ollo0%d 3. th% '.m3ol GIC) %"&" E>PNOGIC and DEPT"DEPTNOGIC ar% out%r :oin olumn' 9'im#l% #r%diat%9 L a lo&ial %!#r%''ion ontainin& no AND9') OR9') or NOT9' G u'uall. a 'im#l% r%lation 'uh a' A = A C 9out%r :oin #r%diat%9 L a 'im#l% #r%diat% ontainin& on% or mor% out%r :oin olumn'" , OUTER 5OIN SENTAP L RULES An out%r :oin #r%diat% ma. onl. ontain out%r :oin olumn' 8rom on% ta3l% G in oth%r 0ord') all out%r :oin olumn' in a 'in&l% out%r :oin #r%diat% mu't 3%lon& to th% 'am% ta3l%C" Thi' m%an') 8or %!am#l%) that th% 8ollo0in& 'tat%m%nt i' ill%&al :L E>P"E>PNOGIC = DEPT"DEPTNOGIC L out%r :oin olumn' 8rom t0o ta3l%' Al'o) i8 a olumn in a #r%diat% i' an out%r :oin olumn) th%n all olumn' 8rom th% 'am% ta3l% mu't 3% out%r :oin olumn' in that #r%diat%" Thi' m%an') 8or %!am#l%) that th% 8ollo0in& 'tat%m%nt i' ill%&al :L E>P"SAL I E>P"CO>>GIC = SALGRADE"DIGD L mi!%d olumn' 8rom on% ta3l% In a #r%diat%) th% ta3l% r%8%r%n%d 0ith a GIC i' dir%tl. 9out%r :oin%d9 to all oth%r ta3l%' in th% #r%diat%" It i' indir%tl. 9out%r :oin%d9 to an. ta3l%' to 0hih th%'% oth%r ta3l%' ar% th%m'%l2%' 9out%r :oin%d9" A #r%diat% ma. not 3% dir%tl. or indir%tl. 9out%r :oin%d9 to it'%l8" Thi' m%an') 8or %!am#l%) that th% 8ollo0in& om3ination o8 #r%diat%' i' ill%&al :L E>P"E>PNOGIC = PERS"E>PNO AND PERS"DEPTNOGIC = DEPT"DEPTNO AND DEPT"5OAGIC = E>P"5OA L irular out%r :oin r%lation'hi# 1 OUTER 5OIN EPECUTION For a &i2%n ta3l%) T) th%r% ma. 3% 3oth out%r :oin and nonLout%r :oin #r%diat%'" E!%ution our' G on%#tuall. C a' 8ollo0' :L *" Th% r%'ult o8 :oinin& all ta3l%' m%ntion%d in ta3l% T9' out%r :oin #r%diat%' i' 8orm%d G 3. r%ur'i2% a##liation o8 thi' al&orithm C" ," For %ah ro0 o8 th% r%'ult) a '%t o8 om#o'it% ro0' i' 8orm%d) %ah on'i'tin& o8 th% ori&inal ro0 in th% r%'ult :oin%d to a ro0 in ta3l% T 8or 0hih th% om#o'it% ro0 'ati'8i%' all o8 ta3l% T9' out%r :oin #r%diat%'" 1" I8 a '%t o8 om#o'it% ro0' i' th% null '%t) a om#o'it% ro0 i' r%at%d on'i'tin& o8 th% ori&inal ro0 in th% r%'ult :oin%d to a ro0 'imilar to tho'% in ta3l% T) 3ut 0ith all 2alu%' '%t to null" 4" Ro0' that do not #a'' th% nonLout%r :oin #r%diat%' ar% r%mo2%d" Thi' ma. 3% 'ummari'%d a' 8ollo0'" Out%r :oin #r%diat%' G tho'% 0ith GIC a8t%r a olumn o8 ta3l% T C) ar% %2aluat%d AEFORE ta3l% T i' au&m%nt%d 0ith a null ro0" Th% null ro0 i' add%d onl. i8 th%r% ar% NO ro0' in ta3l% T that 'ati'8. th% out%r :oin #r%diat%'" NonLout%r :oin #r%diat%' ar% %2aluat%d AFTER ta3l% T i' au&m%nt%d 0ith a null ro0 Gi8 n%%d%dC 4 OUTER 5OIN L RECO>>ENDATIONS C%rtain t.#%' o8 out%r :oin' in om#liat%d lo&ial %!#r%''ion' ma. not 3% 0%ll 8orm%d" In &%n%ral) out%r :oin olumn' in #r%diat%' that ar% 3ranh%' o8 an OR 'hould 3% a2oid%d" Inon'i't%ni%' 3%t0%%n th% 3ranh%' o8 th% OR an r%'ult in an am3i&uou' ?u%r.) and thi' ma. not 3% d%t%t%d" It i' 3%'t to on8in% out%r :oin olumn' to th% to# l%2%l o8 th% 90h%r%9 lau'%) or to n%'t%d AND9' onl." 6 OUTER 5OIN L ILLUSTRATIVE EPA>PLES 6"* Sim#l% Out%r 5oin SELECT ENA>E) LOC FRO> DEPT) E>P @DERE DEPT"DEPTNO = E>P"DEPTNOGIC Th% #r%diat% i' %2aluat%d AEFORE null au&m%ntation" I8 th%r% i' a DEPT ro0 8or 0hih th%r% ar% no E>P ro0') th%n a null E>P ro0 i' onat%nat%d to th% DEPT ro0" 6", Out%r 5oin @ith Sim#l% Po'tL5oin Pr%diat%' SELECT ENA>E) LOC FRO> DEPT) E>P @DERE DEPT"DEPTNO = E>P"DEPTNOGIC AND E>P"DEPTNO IS NULL Th% '%ond 'im#l% #r%diat% i' %2aluat%d AFTER null au&m%ntation) 'in% th%r% i' no GIC) r%mo2in& ro0' 0hih 0%r% not th% r%'ult o8 null au&m%ntation and h%n% l%a2in& onl. DEPT ro0' 8or 0hih th%r% 0a' no orr%'#ondin& E>P ro0" 6"1 Out%r 5oin @ith Additional Pr%L5oin Pr%diat%' SELECT ENA>E) LOC FRO> DEPT) E>P @DERE DEPT"DEPTNO = E>P"DEPTNOGIC AND 9CLER/9 = E>P"5OAGIC AND E>P"DEPTNO IS NULL Th% #r%diat% on E>P"5OA i' %2aluat%d at th% 'am% tim% a' th% on% on E>P"DEPTNO L 3%8or% null au&m%ntation" A' a r%'ult) a null ro0 i' au&m%nt%d to an. DEPT ro0 8or 0hih th%r% ar% no orr%'#ondin& l%r$'9' in th% E>P ta3l%" Th%r%8or%) thi' ?u%r. di'#la.' d%#artm%nt' ontainin& no l%r$'" Not% that it th% GIC 0%r% omitt%d 8rom th% E>P"5OA #r%diat%) no ro0' 0ould 3% r%turn%d" In thi' a'%) 3oth th% E>P"5OA and E>P"DEPTNO IS NULL #r%diat%' ar% %2aluat%d AFTER th% out%r :oin) and th%r% an 3% no ro0' 8or 0hih 3oth ar% tru%" APPENDIP E Th% T%n ommandm%nt' 8or 8a't Qu%ri%' TDOU SDALL ACCIDENTLE NOT DISAALE TDE USE OF INDEPES AE >ODIFEING A COLU>N @ITDIN TDE @DERE CLAUSE
TDOU SDALL PUT TDE TAALE TDAT RETURNS TDE FE@EST RO@S LAST IN TDE FRO> LIST OF TDE QUERE"
TDOU @ILL INVESTIGATE RE@ORDING TDE QUERE IN SO>E OTDER @AE" TDOU SDALL USE TDE EPISTS OPERATOR @DENEVER POSSIALE" TDOU SDALL NOT AS/ TDE /ERNEL FOR >ORE TDAN TDOU @ANTEST" TDOU SDALL NOT FORCE REPARSING OF A QUERE @DEN ONLE CDANGING AIND VARIAALES TDOU SDALL /NO@ TDE DATA VOLU>ETRICS AND USE REALISTIC TEST DATA" TDOU SDALL USE TAALE ALIASES IN TDE QUERIES TDOU SDALL NOT USE TDE <= UNLESS AASOLUTELE NECESSARE TDOU SDALL USE TDE TRACE FACILITE TO >ONITOR EOUR QUERIES APPENDIP F EPPLAIN Failit. >OTIVATION Th%r% i' a n%%d 8or u'%r' to 3% a3l% to d%t%rmin% th% 't%#' th% '.'t%m ta$%' in #%r8ormin& 2ariou' o#%ration' on a data3a'%" Th% EPPLAIN 8ailit. #ro2id%' u'%r' 0ith a on2%ni%nt m%hani'm 8or &%ttin& thi' in8ormation" Th% 8ailit. 'tor%' thi' in8ormation in a 'tandard data3a'% ta3l% that an 3% mani#ulat%d 0ith 'tandard SQL 'tat%m%nt'" SENTAP Th% 8ollo0in& '.nta! i' 3a'%d on th% '.nta! u'%d 3. DA, 8or th%ir EPPLAIN 8ailit.: EPPLAIN PLAN TSET STATE>ENTKID T=U M'trin& lit%ralFU TINTO Mta3l%Knam%FU FOR M'?lK'tat%m%ntF 0h%r% STATE>ENTKID L a uni?u% o#tional id%nti8i%r 8or th% 'tat%m%ntH INTO L allo0' u'%r to 'a2% th% r%'ult' o8 th% anal.'i' in th% '#%i8i%d ta3l%" Th% ta3l% mu't on8orm to th% 8ormat 8or th% ta3l% u'%d to 'tor% th% anal.'i' G '%% TAALE FOR>ATS '%tion 8or a d%'ri#tion o8 th% ta3l% 8ormat C" I8 thi' lau'% i' not '#%i8i%d) th% '.'t%m 0ill th%n att%m#t to 'tor% th% in8ormation in a ta3l% nam%d Mu'%rKidF"PLANKTAALE " I8 th% %!#liit or im#liit ta3l% do%' not %!i't th% EPPLAIN ommand 0ill 8ail"
M'?l 'tat%m%ntF L an in'%rt) d%l%t%) u#dat%) or ?u%r. 'tat%m%ntH
TAALE FOR>ATS Cor% Ta3l% Format Th% or% ta3l% u'%d to r%#r%'%nt th% #lan in8ormation on'i't' o8 th% 8ollo0in& 8i%ld': STATE>ENTKID L An id%nti8i%r a''oiat%d 0ith th% 'tat%m%nt" I8 not '%t 3. th% u'%r) th% id%nti8i%r 0ill 3% NULL" Not% that a u'%r ma. id%nti8. a 'tat%m%nt 3. th% tim%'tam# 8i%ld" TI>ESTA>P L Th% dat% and tim% 0h%n th% 'tat%m%nt 0a' anal.'%d" RE>AR/S L An. omm%nt th% u'%r 0i'h%' to a''oiat% 0ith thi' 't%# o8 th% anal.'i'" OPERATION L th% nam% o8 th% o#%ration 3%in& #%r8orm%d" Th% 8ollo0in& ta3l% #ro2id%' a li'tin& o8 th% o#%ration' d%'ri3%d 3. th% 8ailit." O#%ration D%'ri#tion LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL AndLE?ual A r%tri%2al utili'in& int%r'%tion o8 ro0id' 8rom ind%! '%arh%' Conn%t 3. A r%tri%2al that i' 3a'%d on a tr%% 0al$ Conat%nation A r%tri%2al 8rom a &rou# o8 ta3l%'" It i' %''%ntiall. a UNION ALL o#%ration o8 th% 'our%'" U'%d 8or OR o#%ration'" Countin& A nod% that i' u'%d to ount th% num3%r o8 ro0' r%turn%d 8rom a ta3l%" U'%d 8or ?u%ri%' that u'% th% RO@NU> m%taLolumn" Filt%r A r%'trition o8 th% ro0' r%turn%d 8rom a ta3l% Fir't Ro0 A r%tri%2al o8 onl. th% 8ir't ro0 For U#dat% A r%tri%2al that i' u'%d 8or u#datin& Ind%! A r%tri%2al 8rom an ind%! Int%r'%tion A r%tri%2al o8 ro0' ommon to t0o ta3l%' >%r&% 5oin A :oin utili'in& m%r&% 'an' >inu' A r%tri%2al o8 ro0' in Sour% * ta3l% 3ut not in Sour% , ta3l% N%'t%d Loo#' A :oin utili'in& n%'t%d loo#'" Eah 2alu% in th% 8ir't 'u3nod% i' loo$%d u# in th% '%ond 'u3nod%" Thi' i' o8t%n u'%d 0h%n on% ta3l% in a :oin i' ind%!%d and th% oth%r i' not" Pro:%t A r%tri%2al o8 a 'u3'%t o8 olumn' 8rom a ta3l% R%mot% A r%tri%2al 8rom a data3a'% oth%r than th% urr%nt data3a'% S%?u%n% An o#%ration in2ol2in& a '%?u%n% ta3l% Sort A r%tri%2al o8 ro0' ord%r%d on 'om% olumn or &rou# o8 olumn' Ta3l% A r%tri%2al 8rom a 3a'% ta3l% Union A r%tri%2al o8 uni?u% ro0' 8rom t0o ta3l%' Vi%0 A r%tri%2al 8rom a 2irtual ta3l%
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL Not% that th% o#%ration 'ho0n 0h%n ountin& th% num3%r o8 ro0' r%turn%d 3. a ?u%r. Gi"%" '%l%t ountGJCC i' SORT" Thi' i' du% to th% 0a. that COUNT i' im#l%m%nt%d int%rnall." Th% ta3l% 0ill not r%all. 3% 'ort%d"
OPTIONS L an o#tion that modi8i%' th% o#%ration) %"&") OUTER o#tion on :oin o#%ration') rational% 8or 'ortin&) t.#% o8 ind%! 'an) t.#% o8 8ilt%r) %t" Th% 8ollo0in& ta3l%
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Pa&% 4*
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Pa&% 4, AUTDOR : Tina London DATE : Au&u't () *++, #ro2id%' a li't o8 th% o#tion' 8or th% o#%ration' that ha2% o#tion'" OPERATION OPTIONS DESCRIPTION
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL Ind%! UNIQUE /EE Uni?u% $%. loo$u# on ind%! RANGE Ind%! ran&% 'an >%r&% 5oin OUTER 5oin i' an out%r :oin N%'t%d Loo#' OUTER 5oin i' an out%r :oin Sort DISTINCT Sort i' to #rodu% di'tint 2alu%' GROUP AE Sort i' 8or &rou#in& o#%ration 5OIN Sort i' 8or m%r&% :oin ORDER AE Sort i' 8or ord%r 3. Ta3l% AE RO@ID Ta3l% 'an i' 3. ro0id FULL S%?u%ntial ta3l% 'an CLUSTER Ta3l% 'an 3. lu't%r $%.
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL OA5ECTKNODE L th% nam% o8 th% nod% that o0n' th% data3a'% o3:%t" OA5ECTKO@NER L th% nam% o8 th% 'h%ma th% o0n' th% data3a'% o3:%t" OA5ECTKNA>E L th% nam% o8 th% data3a'% o3:%t" OA5ECTKTEPE L a modi8i%r that #ro2id%' d%'ri#ti2% in8ormation a3out th% data3a'% o3:%t) %"&") NONLUNIQUE 8or ind%!%') %t" OA5ECTKINSTANCE L a num3%r orr%'#ondin& to th% ordinal #o'ition o8 th% o3:%t a' it a##%ar' in th% ori&inal ?u%r." Th% num3%rin& #ro%%d' 8rom l%8t to ri&ht) out%r to inn%r 0ith r%'#%t to th% ori&inal ?u%r. t%!t" Not% that at thi' l%2%l) 2i%0 %!#an'ion 0ill r%'ult in rath%r int%r%'tin& o3:%t in'tan% num3%r'" @% 0ill 3% addr%''in& thi' i''u% 8ull. in 8utur% r%l%a'%'" SEARCDKCOLU>NS L th% num3%r o8 l%adin& olumn' u'%d 0h%n '%arhin& an ind%!" ID L a num3%r a''i&n%d to thi' o#%ration in th% tr%%" Corr%'#ond' to a #r%ord%r tra2%r'al o8 th% ro0 'our% tr%%" PARENTKID L th% num3%r a''i&n%d to th% #r%2iou' o#%ration that r%%i2%' in8ormation 8rom thi' o#%ration" Thi' 8i%ld om3in%d 0ith th% ID 8i%ld allo0' u'%r' to do a tr%%0al$ o8 th% '#%i8i%d #lan 0ith th% CONNECT AE 'tat%m%nt" POSITION L th% #o'ition thi' data3a'% o3:%t ou#i%' 8or th% #r%2iou' o#%ration" OTDER L oth%r in8ormation that i' '#%i8i to th% ro0 'our% that a u'%r ma. 8ind u'%8ul" For %!am#l%) th% '%l%t 'tat%m%nt to a r%mot% nod%) %t" Sam#l% Ta3l% D%8inition r%at% ta3l% PLANKTAALE G 'tat%m%ntKid harG1-C) tim%'tam# dat%) r%mar$' harG;-C) o#%ration harG1-C) o#tion' harG1-C) o3:%tKnod% harG1-C) o3:%tKo0n%r harG1-C) o3:%tKnam% harG1-C) o3:%tKin'tan% num%ri) o3:%tKt.#% harG1-C) '%arhKolumn' num%ri) id num%ri) #ar%ntKid num%ri) #o'ition num%ri) oth%r lon&CH An SQL 'ri#t to r%at% thi' ta3l% r%'id%' in 8il% !#lain#l"'?l in th% 'am% dir%tor. ontainin& th% 8il% atalo&"'?l" Thi' ta3l% mu't r%'id% in th% urr%nt 'h%ma unl%'' .ou u'% th% o#tional INTO lau'% o8 th% EPPLAIN ommand" EPA>PLES Su##o'% 0% i''u% th% 8ollo0in& 'tat%m%nt': EPPLAIN PLAN SET STATE>ENTKID = 9?u%r.*9 INTO QUEREKPLANS FOR SELECT J FRO> T*)T,)T1 @DERE T*"F* = T,"F* AND T,"F, = T1"F,H SELECT OPERATION) OPTIONS) OA5ECTKNA>E) ID) PARENTKID) POSITION FRO> QUEREKPLANS @DERE STATE>ENTKID = 9?u%r.*9 ORDER AE IDH Th% 8ollo0in& out#ut 0ould 3% r%at%d: OPERATION OPTIONS OA5ECTKNA>E ID PARENTKID POSITION LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL LL >ERGE 5OIN * >ERGE 5OIN , * * SORT 5OIN 1 , * TAALE ACCESS FULL T* 4 1 * SORT 5OIN 6 , , TAALE ACCESS FULL T, 7 6 * SORT 5OIN ( * * TAALE ACCESS FULL T1 ; ( * ; RECORDS '%l%t%d Su##o'% that an ind%! i' r%at%d on 8i%ld F* on ta3l% T* and th% 8ollo0in& 'tat%m%nt' ar% i''u%d: EPPLAIN PLAN SET STATE>ENTKID = 9?u%r.,9 INTO QUEREKPLANS FOR SELECT J FRO> T* @DERE F* F *H
SELECT OPERATION) OPTIONS) OA5ECTKNA>E) OA5ECTKTEPE) SEARCDKCOLU>NS) ID) PREVIOUSKID FRO> QUEREKPLANS @DERE STATE>ENTKID=9?u%r.,9 ORDER AE IDH Th% 8ollo0in& out#ut i' #rodu%d: OPERATION OPTIONS OA5ECTKNA>E OA5ECTKTEPE SEARCDKCOLU>NS ID POSITION LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL LLLLL TAALE SCAN AE RO@ID T* * INDEP SCAN RANGE IT* NONLUNIQUE * , * , RECORDS '%l%t%d @ith th% 'am% ondition' a' 3%8or%) 'u##o'% 0% i''u% th% 8ollo0in& 0hih d%mon'trat%' an ind%! onl. r%tri%2al: EPPLAIN PLAN SET STATE>ENTKID = 9?u%r.19 INTO QUEREKPLANS FOR SELECT F* FRO> T* @DERE F* F *H SELECT OPERATION) OPTIONS) OA5ECTKNA>E) OA5ECTKTEPE) SEARCDKCOLU>NS) ID FRO> QUEREKPLANS @DERE STATE>ENTKID=9?u%r.19H Th% 8ollo0in& out#ut i' #rodu%d: OPERATION OPTIONS OA5ECTKNA>E OA5ECTKTEPE SEARCDKCOLU>NS ID LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL LL INDEP SCAN RANGE IT* NONLUNIQUE * * RECORDS '%l%t%d Th% n%!t %!am#l% illu'trat%' th% out#ut i8 a &rou#in& o#%ration i' '#%i8i%d in th% 'tat%m%nt: EPPLAIN PLAN SET STATE>ENTKID = 9?u%r.49 INTO QUEREKPLANS FOR SELECT AVGGF*C)F, FRO> T* GROUP AE F,H SELECT OPERATION) OPTIONS) OA5ECTKNA>E) ID) PARENTKID FRO> QUEREKPLANS @DERE STATE>ENTKID = 9?u%r.49 ORDER AE IDH OPERATION OPTIONS OA5ECTKNA>E ID PARENTKID LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL SORT GROUP AE * TAALE SCAN FULL T* , * , RECORDS '%l%t%d Th% n%!t %!am#l% illu'trat%' th% ou#tut i8 DISTINCT i' '#%i8i%d in th% 'tat%m%nt: EPPLAIN PLAN SET STATE>ENTKID = 9?u%r.69 INTO QUEREKPLANS FOR SELECT DISTINCT F* FRO> T*H SELECT OPERATION) OPTIONS) OA5ECTKNA>E) ID) PREVIOUSKID FRO> QUEREKPLANS @DERE STATE>ENTKID = 9?u%r.69 ORDER AE IDH OPERATION OPTIONS OA5ECTKNA>E ID PREVIOUSKID LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL SORT DISTINCT * TAALE SCAN FULL T* , * , RECORDS '%l%t%d Th% n%!t %!am#l% illu'trat%' th% out#ut i8 a 'u3?u%r. i' '#%i8i%d in th% 'tat%m%nt: EPPLAIN PLAN SET STATE>ENTKID = 9?u%r.79 INTO QUEREKPLANS FOR SELECT J FRO> T* @DERE F* M G SELECT F, FRO> T, @DERE F*=1CH SELECT OPERATION) OPTIONS) OA5ECTKNA>E) ID) PARENTKID) POSITION FRO> QUEREKPLANS @DERE STATE>ENTKID = 9?u%r.79 ORDER AE IDH OPERATION OPTIONS OA5ECTKNA>E ID PARENTKID POSITION LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL L FILTER OUT * TAALE SCAN FULL T* , * * TAALE SCAN FULL T, 1 * , 1 RECORDS '%l%t%d Th% 8inal %!am#l% di'#la.' a om#l%! ?u%r. 0ho'% out#ut i' '%nt to th% d%8ault #lan ta3l%" G It i' a''um%d that thi' ta3l% ha' 3%%n r%at%d 3%8or% i''uin& th% 'tat%m%nt"C EPPLAIN PLAN SET STATE>ENTKID = 9?u%r.(9 FOR SELECT J FRO> T*)T, @DERE T*"F* = T,"F* UNION SELECT J FRO> T,)T1 @DERE T,"F* = T1"F*H SELECT OPERATION) OPTIONS) OA5ECTKNA>E) ID) PARENTKID) POSITION FRO> PLANKTAALE @DERE STATE>ENTKID = 9?u%r.(9 ORDER AE IDH Th% 8ollo0in& out#ut i' #rodu%d: OPERATION OPTIONS OA5ECTKNA>E ID PARENTKID POSITION LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL LL PRO5ECTION * UNION , * * SORT DISTINCT 1 , * NEST LOOP 4 1 * TAALE SCAN AE RO@ID T* 6 4 * INDEP SCAN RANGE IT* 7 6 * TAALE SCAN FULL T, ( 4 , SORT DISTINCT ; , , >ERGE 5OIN + ; * SORT 5OIN *- + * TAALE SCAN FULL T, ** *- * SORT 5OIN *, + , TAALE SCAN FULL T1 *1 *, * *1 RECORDS '%l%t%d Th% 8ollo0in& %!am#l% i' 3a'%d on th% #r%2iou' ?u%r." It illu'trat%' th% u'% o8 th% tr%%0al$in& a#a3ilit. in Oral%9' 2%r'ion o8 SQL" SELECT LPADG9 9),JLEVELCNNOPERATION)OPTIONS) OA5ECTKNA>E FRO> PLANKTAALE @DERE STATE>ENTKID=9?u%r.(9 CONNECT AE PRIOR ID = PARENTKID and STATE>ENTKID = 9?u%r.(9 START @ITD ID = * and STATE>ENTKID = 9?u%r.(9 ORDER AE IDH LPADG9 9),JLEVELCNNOPERATION LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL LLLLLL OPTIONS OA5ECTKNA>E LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL PRO5ECTION UNION SORT DISTINCT NEST LOOP TAALE SCAN AE RO@ID T* INDEP SCAN RANGE IT* TAALE SCAN FULL T, SORT DISTINCT >ERGE 5OIN SORT 5OIN TAALE SCAN FULL T, SORT 5OIN TAALE SCAN FULL T1 *1 RECORDS '%l%t%d