Professional Documents
Culture Documents
w w "t is much harder to !rite efficient SQL than it is to !rite functionally correct SQL w w # SQL choice is correct only if it produces the right result in the shortest possible amount of time, !ithout impeding the performance of any other system resources.
w w
3
w ,or example the follo!ing t!o select statements are -O+ the same. w
w Since the 0L"/-+3-145/6 is li'ely to be different for every execution, !e !ill almost never find a matching statement in the Shared $ool and conse uently the statement !ill have to be reparsed every time w 0onsider the follo!ing approach = w
S/L/0+ ,"6S+3-#4/, L#S+3-#4/ ,6O4 0lient &7/6/ 0L"/-+3-14 8 :Client_Num
w You do not need to create a ne! cursor or re%parse the SQL statement if the value of the bind variable changes. #lso, if another session executes the same statement, it is li'ely to find them in the Shared $ool, since the name of the bind variable does not change from execution to execution. w
6
Using ROWI
When !ossible
w /ach record added to the database has a uni ue 6O&"2 and !ill never change until the delete statement issued on that record. w "f the record bloc' or location !as changed for any reason, the original 6O&"2 points to the ne! location or the ne! 6O&"2 and so on. w 1se 6O&"2 !henever possible to get the best performance out of your retrievals
w
7
cursor accounts3cur is select acct3no, currency, branch 6o!id acct3ro!id, ,rom account !here . . . . for acct3rec in accounts3cur loop update account set !here ro!id 8 acct3rec.acct3ro!id? end loop?
w
12
Using Joints in Place of EXISTS for Uni ue Scan Indexes and small ta!les
w "n general Boin tables rather than specifying sub% ueries for them such as the follo!ing.
8 w &ith Boin %
select acct3"2, currency, branch from account !here exists (select > from branch !here code branch and def3curr 8 C99>C* select acct3"2,currency, branch from account #, branch 5 !here b.code 8 #.branch and #.def3curr 8 C99>C
13
16
18
19
PositionofJoinsintheWHEREClause
w Ta"#e $oins s%ou#& "e 'ri((en firs( "efore an) con&i(ion of *+,-, c#ause! .n& (%e con&i(ions '%ic% fi#(er ou( (%e ma/imum recor&s s%ou#& "e p#ace& a( (%e en& af(er (%e $oins as (%e parsing is &one from 01TT12 (o T1P! w Leas( ,fficien( 3 w S,L,4T ! ! ! !
5-12 ,2P , *+,-, S.L 6 5 .78 910 : 4L,-; .78 25 < =S,L,4T 41>7T=?@ 5-12 ,2P *+,-, 2A- : ,!,2P71@B
2
w w 2os( ,fficien( 3
w
S,L,4T ! ! ! ! 5-12 ,2P , *+,-, 25 < =S,L,4T 41>7T=?@ 5-12 ,2P *+,-, 2A- : ,!,2P71 @ .78 S.L 6 5 .78 910 : 4L,-;B
21
Sort'Merge Join
,ash Join
*luster
(qui 'oins on
w w
Optimi-er hint$
use*nl
Use*merge
use*hash
"one
%esource concerns$ +!U Temporar& segments ,emor& Storage is) I-O init.ora parameters$"one sort*area*si.e hash*'oin*enabled "one db*$ile*multi bloc)* hash*area*si.e
read3count hash3multibloc'3io3count
#eatures$Wor)s with an& 'oin Better than nested Better than nested Reduces I-O $or master/ loop when inde% is loop when inde% is detail Queries missing or search missing or search critiria is not restrictive criteria is not restrictive /raw!ac0s$ 0er& ine$$icient ,ust per$orm an e%tra sort +an require lots +lustered data can ta)e when no suitable +annot return $irst o$ memor&more space to store inde% e%ist rows quic)l& slows down updates and $ull scan
22
w 1-.4L, parser a#'a)s processes (a"#e names from rig%( (o #ef(C so (%e (a"#e name )ou specif) #as( =&riDing (a"#e@ is ac(ua##) (%e firs( (a"#e processe&! w Ef )ou specif) more (%an one (a"#e in a 5-12 c#ause of a S,L,4T s(a(emen(C )ou mus( c%oose (%e (a"#e con(aining (%e #o'es( num"er of ro's as (%e &riDing (a"#e! w *%en 1-.4L, processes mu#(ip#e (a"#esC i( uses an in(erna# sor(Fmerge proce&ure (o $oin (%ose (a"#es! w 5irs(C i( scans an& sor(s (%e firs( (a"#e =(%e one specifie& #as( in (%e 5-12 c#ause@! w 7e/(C i( scans (%e secon& (a"#e =(%e one prior (o (%e #as( in (%e 5-12 c#ause@ an& merges a## of (%e ro's re(rieDe& from (%e secon& (a"#e 'i(% (%ose re(rieDe& from (%e firs( (a"#e! w 5or e/amp#e3 Ta"#e T.0. %as 16C384 ro's! Ta"#e T.00 %as 1 ro'! S,L,4T 41>7T=?@ 5-12 T.0.C T.00 !96 secon&s e#apse&
S,L,4T 41>7T=?@ 5-12 T.00C T.0. e#apse& 26! 9 secon&s
23
w Ef (%ree (a"#es are "eing $oine&C se#ec( (%e in(ersec(ion (a"#e as (%e &riDing (a"#e! w T%e in(ersec(ion (a"#e is (%e (a"#e (%a( %as man) (a"#es &epen&en( on i(! w ,!g!! T%e ,2P (a"#e represen(s (%e in(ersec(ion "e('een (%e L14.TE17 (a"#e an& (%e 4.T,A1-G (a"#e! S,L,4T ! ! ! 5-12 L14.TE17 LC 4.T,A1-G 4C ,2P , *+,-, ,!,2PH71 0,T*,,7 1 .78 2 .78 ,!4.TH71 : 4!4.TH71 .78 ,!L147 : L!L147 is more efficien( (%an (%is ne/( e/amp#e3 S,L,4T ! ! ! 5-12 ,2P ,C L14.TE17 LC 4.T,A1-G 4 *+,-, ,!4.TH71 : 4!4.TH71 .78 ,!L147 : L!L147 .78 ,!,2PH71 0,T*,,7 1 .78 2
24
w 5ut the follo!ing statement. Select @ ,rom acc3txn &here acc3txn3ref3no 8 >>FFF99>:IF9 w &ill be processed as. Select @ ,rom acc3txn &here acc3txn3ref3no to3number(>>FFF99>:IF9 *
26
27
SELECT CO#$T%DECODE%DE-T0$O(2232( 6=8( $#LL'' D22320CO#$T( CO#$T%DECODE%DE-T0$O(22:2(6=8($#LL'' D22:20CO#$T( S#)%DECODE%DE-T0$O(2232( S*L( $#LL'' D22320S*L( S#)%DECODE%DE-T0$O( 22:2( S*L( $#LL'' D22:20S*L +,O) E)./E,E E$*)E L45E 6S)4T/789 w Similarl ( DECODE can be used in !" clause effectivel . w ROUP !" or ORDER
w
28
w To improve performance( minimize the number of table loo>ups in ?ueries( particularl if our statements include sub@?uer SELECTs or multi@column #-D*TEs. For example: #east E$$icient : SELECT T*A0$*)E +,O) T*ALES ./E,E T*A0$*)E 1 %%E#EC& T*A0$*)E +,O) T*A0COL#)$S ./E,E BE,S4O$ 1 C2D' *$D DA0BE, 1 %%E#EC& DA0BE, +,O) T*A0COL#)$S ./E,E BE,S4O$ 1 C2D'
29
Most E$$icient :
SELECT T*A0$*)E +,O) T*ALES ./E,E %T*A0$*)E(DA0BE,'1 %SELECT T*A0$*)E( DA0BE, +,O) T*A0COL#)$S ./E,E BE,S4O$ 1 C2D'
Some
o1s and
on1ts
w Some S/L/0+ statement &7/6/ clauses do not use indexes at all. "f you have specified an index over a table that is referenced by a clause of type sho!n in this section Oracle !ill simply ignore the index! w ,or each clause that cannot use an index, an alternative approach, !hich !ill allo! you to get better performance out of your S/L/0+ statements is suggested.
w
32
w Do Not Use: Use Select @ from #ccount &here substr(ac3acct3no,>,>* 8 F w Use: Select @ from #ccount &here ac3acct3no li'e FE w Do Not Use: Select @ ,rom fin3trxn &here ft3trxn3ref3no J8 9 w Use: Select @ ,rom fin3trxn &here ft3trxn3ref3no K 9
33
Do Not Use: Select @ ,rom account &here ac3type LL ac3branch 8 sav99> Use: Select @ ,rom account &here ac3type 8 sav #nd ac3branch 8 sav99> Do Not Use: Select @ ,rom 0L"/-+ !here to3char(01++3O,,3+"4/,yyyymmdd* 8 to3char(sysdate,yyyymmdd* Use: Select @ ,rom 0L"/-+ &here 01+3O,,32#+/ K8 trunc(sysdate* and 01+3O,,3+"4/ M trunc(sysdate* N >
34
w w Do Not Use: w Select @ ,rom acct3trxn &here to3char(at3value3date,yyyymmdd* K to3char(sysdate,yyyymmdd* w Use: Select @ ,rom acct3trxn &here at3value3date K8 trunc(sysdate* N >
35
w w Do Not Use: Select @ ,rom acct3trxn &here to3char(at3value3date,yyyymmdd* M to3char(sysdate,yyyymmdd* w Use: Select @ ,rom acct3trxn &here at3value3date M trunc(sysdate* w
36
w w Do Not Use: Select @ ,rom acct3trxn &here to3char(at3value3date,yyyymmdd* K8 to3char(sysdate,yyyymmdd* w Use: Select @ ,rom acct3trxn &here at3value3date K8 trunc(sysdate*
w
37
w w Do Not Use: Select @ ,rom acct3trxn &here to3char(at3value3date,yyyymmdd* to3char(sysdate,yyyymmdd* w Use: Select @ ,rom acct3trxn &here at3value3date M trunc(sysdate* N > w Do Not Use: Select count( @* ,rom 56OO/6 w Use:
M8
AvoidUsingSELECT*Clauses
w T%e &)namic SQL co#umn reference =?@ giDes )ou a 'a) (o refer (o a## of (%e co#umns of a (a"#e! w w 8o no( use (%e ? fea(ure "ecause i( is Der) inefficien( II (%e ? %as (o "e conDer(e& (o eac% co#umn in (urn! w w T%e SQL parser %an&#es a## (%e fie#& references ") o"(aining (%e names of Da#i& co#umns from (%e &a(a &ic(ionar) an& su"s(i(u(es (%em on (%e comman& #ineC '%ic% is (ime consuming! w w
w w
39
: =
6 rows selected. Execution Plan '''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 7 SE+E*T ST)TEMENT Optimi-er8*,OOSE 9*ost86 *ard8:7 31tes8:;7< 7 SO%T 9O%/E% 34< 9*ost86 *ard8:7 31tes8:;7< = : T)3+E )**ESS 9#U++< O# >)?U)TI*2)NIM)+@ 9*ost8: *ard8:7 31tes8:;7<
4
w w w SQL?P#us &oes e/ecu(e (%e Kuer)! Ef a Kuer) genera(es a #o( of EF1 an& consumes a #o( of 4P>C )ou 'on( 'an( (o JicJ i( off $us( (o see (%e e/ecu(ion p#an! w En (%a( case use fo##o'ing 3 S?+A SET )UTOT%)*E T%)*EON+4 EXP+)IN w )ou are (%roug% using au(o(raceC )ou can (urn (%e fea(ure off ") issuing (%e S(T #UTOTR#+( O33 comman&!
41