You are on page 1of 41

BUY GUYS BUY BUY QUEST QUEST PRODUCTS PRODUCTS BOOK

Top tips for Oracle SQL tuning

Guy Harrison Senior Software Architect, Quest Software

Top 10 Oracle SQL tuning tips


1. 2. ". %. '. .. /. 0. 2. 14. Design and develop with performance in mind sta!lish a tuning environment #nde$ wisely &educe parsing (a)e advantage of *ost +ased ,ptimi-er Avoid accidental ta!le scans ,ptimi-e necessary ta!le scans ,ptimi-e 1oins 3se array processing *onsider 567SQ6 for 8tric)y9 SQ6

Hint #1: Design and develop with performance in mind


$plicitly identify performance targets

:ocus on critical transactions


; (est the SQ6 for these transactions against simulations of production data

<easure performance as early as possi!le *onsider prototyping critical portions of the applications *onsider de=normali-ation and other performance !y design features early on

Hint # : !sta"lish a tuning and development environment


A significant portion of SQ6 that performs poorly in production was originally crafted against empty or nearly empty ta!les. <a)e sure you esta!lish a reasona!le su!=set of production data that is used during development and tuning of SQ6 <a)e sure your developers understand >56A#? 56A? and t)prof, or e@uip them with commercial tuning tools.

#nderstanding SQL tuning tools


(he foundation tools for SQ6 tuning areA
; (he >56A#? 56A? command ; (he SQ6 (race facility ; (he t)prof trace file formatter

ffective SQ6 tuning re@uires either familiarity with these tools or the use of commercial alternatives such as SQ6a!

!$%L&'( %L&(
(he >56A#? 56A? reveals the e$ecution plan for an SQ6 statement. (he e$ecution plan reveals the e$act se@uence of steps that the ,racle optimi-er has chosen to employ to process the SQ6. (he e$ecution plan is stored in an ,racle ta!le called the 8plan ta!le9 Suita!ly formatted @ueries can !e used to e$tract the e$ecution plan from the plan ta!le.

& simple !$%L&'( %L&(


SQL> EXPLAIN PLAN FOR select count(*) from sales where ro!uct"#!$%& E' la#ne!( SQL> SELE)* R*RI+ (LPA, (- -. / * LE0EL) 11 R*RI+ (o erat#on) 11- -11R*RI+ (o t#ons) 11 - - 11 o23ect"name) 4uer5" lan / FRO+ lan"ta2le 6 )ONNE)* 78 PRIOR #! $ arent"#! 9* S*AR* :I*; #! $ < Q=ER8"PLAN >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SELE)* S*A*E+EN* SOR* A??RE?A*E *A7LE A))ESS F=LL SALES

'nterpreting !$%L&'( %L&(


(he more heavily indented an access path is, the earlier it is e$ecuted. #f two steps are indented at the same level, the uppermost statement is e$ecuted first. Some access paths are 81oined9 ; such as an inde$ access that is followed !y a ta!le loo)up.

& more comple) !$%L&'( %L&(


SELECT STATEMENT VIEW SYS_DBA_SEGS UNION-ALL NESTED LOOPS NESTED LOOPS NESTED LOOPS NESTED LOOPS NESTED LOOPS VIEW SYS_OBJECTS UNION-ALL TABLE ACCESS FULL TAB$ TABLE ACCESS FULL TABPART$ TABLE ACCESS FULL CLU$ TABLE ACCESS FULL IND$ TABLE ACCESS FULL INDPART$ TABLE ACCESS FULL LOB$ TABLE ACCESS FULL TABSUBPART$ TABLE ACCESS FULL INDSUBPART$ TABLE ACCESS FULL LOBFRAG$ TABLE ACCESS BY INDEX ROWID OBJ$ INDEX UNIQUE SCAN I_OBJ1

SQL*T+&,! and t-prof


A6( & S SS#,? S ( SQ6B(&A* (&3 causes a trace of SQ6 e$ecution to !e generated. (he (C5&,: utility formats the resulting output. ()prof output contains !rea)down of e$ecution statistics, e$ecution plan and rows returned for each step. (hese stats are not availa!le from any other source. ()prof is the most powerful tool, !ut re@uires a significant learning curve.

T-prof output
!"#$2 ------ -----P)1*') 1+ E9' "$': 1' F'$ ; 27< ------ -----$!$)( 22 %"& '()%*'+, +-*./ 0"'123 "11'#$4 ------ -------- ------- -------- -------7872 7871 7 7 7 7877 7877 7 7 7 1,1817 1,183/ 12&4 1,/7711 &63&&2 ------ -------- ------- -------- -------1,1812 1,1833 12&4. 1,/7711> &63&&2? 1!5*6 -----7 7 ==-----==;

R!5*( E9' "$-!# P()#@ ------- --------------------------------------------------7 SELECT STATEMENT GOALA CBOOSE == FILTER =3361 TABLE ACCESS GOALA ANALYCED DFULLE OF FCUSTOMERSF =3/62 TABLE ACCESS GOALA ANALYCED DFULLE OF FEMPLOYEESF

#sing SQLa"
+ecause >56A#? 56A? and t)prof are unwieldy and hard to interpret, third party tools that automate the process and provide e$pert advice improve SQ6 tuning efficiency. (he Quest SQ6a! productA
; #dentifies SQ6 your data!ase that could !enefit from tuning ; 5rovides a sophisticated tuning environment to e$amine, compare and evaluate e$ecution plans. ; #ncorporates an e$pert system to advise on inde$ing and SQ6 statement changes

SQLa" SQL tuning la"


; Display e$ecution plan in a variety of intuitive ways ; 5rovide easy access to statistics and other useful data ; <odel changes to SQ6 and immediately see the results

SQLa" !)pert &dvice


; SQ6a! provides specific advice on how to tune an SQ6 statement

SQLa" SQL trace integration


; SQ6a! can also retrieve the e$ecution statistics that are otherwise only availa!le through t)prof

Hint #.: 'nde) wisel/


#nde$ to support selective DH & clauses and 1oin conditions 3se concatenated inde$es where appropriate *onsider overinde$ing to avoid ta!le loo)ups *onsider advanced inde$ing options
; Hash *lusters ; +it mapped inde$es ; #nde$ only ta!les

!ffect of adding columns to a concatenated inde)


; ?ovice SQ6 programmers often are satisfied if the e$ecution plan shows an inde$ ; <a)e sure the inde$ has all the columns re@uired

Surname index onl& 40

700

%erge 3 indexes

Index on Surname+f irstname Index on Surname+f irstname+#O$ Index on Surname+f irstname+do +!"oneo 0

20

4 100 200 300 400 Logical IO 500 600 700 800

0it1map inde)es
; *ontrary to widespread !elief, can !e effective when there are many distinct column values ; ?ot suita!le for ,6(5 however
100

*la!sed time +s,

10

0'1

0'01 1 10 $itma! index 100 1(000 #istinct )alues $-./ree index 0ull ta le scan 10(000 100(000 1(000(000

Hint #2: +educe parsing


3se !ind varia!les
; +ind varia!les are )ey to application scala!ility ; #f necessary in 0.1..E, set cursor *3&S,&BSHA&#?G to :,&*

&euse cursors in your application code


; How to do this depends on your development language

3se a cursor cache


; Setting S SS#,?B*A*H DB*3&S,&S Fto 24 or soG can help applications that are not re=using cursors

Hint #3: Ta-e advantage of the ,ost 0ased Optimi4er


(he older rule !ased optimi-er is inferior in almost every respect to the modern cost !ased optimi-er 3sing the cost !ased optimi-er effectively involvesA
; &egular collection of ta!le statistics using the A?A6HI or D+<SBS(A(S command ; 3nderstand hints and how they can !e used to influence SQ6 statement e$ecution ; *hoose the appropriate optimi-er modeA :#&S(B&,DS is !est for ,6(5 applicationsJ A66B&,DS suits reporting and ,6A5 1o!s

Hint #5: &void accidental ta"lescans


(a!lescans that occur unintentionally are a ma1or source of poorly performing SQ6. *auses includeA
; <issing #nde$ ; 3sing 8KL8, 8MN9 or ?,( O 3se inclusive range conditions or #? lists ; 6oo)ing for values that are ?366 O 3se ?,( ?366 values with a default value ; 3sing functions on inde$ed columns O 3se 8functional9 inde$es in ,racle0i

Hint #6: Optimi4e necessar/ ta"le scans


(here are many occasions where a ta!le scan is the only option. #f soA
; *onsider parallel @uery option ; (ry to reduce si-e of the ta!le O Ad1ust 5*(:& and 5*(3S D O &elocate infre@uently used long columns or +6,+s O &e!uild when necessary to reduce the high water mar) ; #mprove the caching of the ta!le O 3se the *A*H hint or ta!le property O #mplement C 5 and & *H*6 pools ; 5artition the ta!le Fif you really see) a large su!set of dataG ; *onsider the fast full inde$ scan

7ast full inde) scan performance


; 3se when you must read every row, !ut not every column ; *ounting the rows in a ta!le is a perfect e$ample

Index range scan +34L*, 0ull index scan 0ull ta le scan 2arallel ta le scan fast full index 2arallel fast full index 0 2'44 5 10 *la!sed time +s, 15 20 5'23 4'14 12'53 17'76

11'83

Hint #8: Optimi4e 9oins


5ic) the !est 1oin method
; ?ested loops 1oins are !est for inde$ed 1oins of su!sets ; Hash 1oins are usually the !est choice for 8!ig9 1oins

5ic) the !est 1oin order


; 5ic) the !est 8driving9 ta!le ; liminate rows as early as possi!le in the 1oin order

,ptimi-e 8special9 1oins when appropriate


; ; ; ; ; S(A& 1oins for data=warehousing applications S(A&B(&A?S:,&<A(#,? if you have !itmap inde$es A?(#=P,#? methods for ?,( #? su!=@ueries S <#=P,#? methods for >#S(S su!=@ueries 5roperly inde$ *,?? *( +H hierarchical @ueries

Oracle 8 semi19oins
,ptimi-es @ueries using >#S(S where there is no supporting inde$
No index on employees *'(' $ G >1!@ "*$!@'1* 5;'1' '9-*$* D*'(' $ 1 >1!@ '@%(!2''* ' 5;'1' '8*"1#)@'H 8 !#$) $_*"1#)@' )#+ '8>-1*$#)@'H 8 !#$) $_>-1*$#)@' )#+ '8+)$'_!>_:-1$;H 8+)$'_!>_:-1$;E

Oracle 8 semi19oins
Dithout the semi=1oin or supporting inde$, @ueries li)e the one on the preceding slide will perform very !adly. ,racle will perform a ta!lescan of the inner ta!le for each row retrieved !y the outer ta!le #f customers has 144,444 rows, and employees 044 rows then 04 <#66#,? rows will !e processedK #n ,racle/, you should create the inde$ or use an #?= !ased su!@uery #n ,racle0, the semi=1oin facility allows the @uery to !e resolved !y a sort=merge or hash 1oin.

To #se semi19oins
Set A6DAHSBS <#BP,#?LHASH or < &G in #?#(.,&A, OR 3se a < &G BSP or HASHBSP hint in the su!@uery of the SQ6 statement
SELECT * FROM customers c WHERE exists (select /*+merge_sj*/ 1 from employees e where .)

Oracle8 semi19oins
(he performance improvements are impressive Fnote the logarithmic scaleG
1(343'11 *7IS/S no semi.8oin or indexes

31'01 *7IS/S no semi.8oin ut 9it" index

6'83 *7IS/S . merge semi.8oin

6'61 I5. ased su 6uer&

10

100 Elapsed time (logarithmic scale)

1(000

10(000

Star :oin improvements


A S(A& 1oin involves a large 8:A*(9 ta!le !eing 1oined to a num!er of smaller 8dimension9 ta!les

Star :oin improvements


(he ,racle/ Star 1oin algorithm wor)s well when there is a concatenated inde$ on all the :A*( ta!le columns +ut when there are a large num!er of dimensions, creating concatenated inde$es for all possi!le @ueries is impossi!le. ,racle0Qs 8Star transformation9 involves re=wording the @uery so that it can !e supported !y com!inations of !itmap inde$es. Since !itmap inde$es can !e efficiently com!ined, a single !itmap inde$ on each column can support all possi!le @ueries.

To ena"le the star transformation


*reate !itmap inde$es on each of the :A*( ta!le columns which are used in star @ueries <a)e sure that S(A&B(&A?S:,&<A(#,?B ?A+6 D is (&3 , either !y changing init.ora or using an A6( & S SS#,? statement. 3se the S(A&B(&A?S:,&<A(#,? hint if necessary.

Draw"ac- of Star transformation


+itmap inde$es reduce concurrency Frow=level loc)ing may !rea) downG. +ut remem!er that large num!er of distinct column values may not matter

Star transformation performance


Dhen there is no suita!le concatenated inde$, the Star transformation results in a significant improvement
0'35 5o suita le concatenated index

1'14

Star;transformation 0'24 :oncatenated index Star

0'01

5 Elapsed time (s)

10

Hint #;: #se &++&< processing


; &etrieve or insert rows in !atches, rather than one at a time. ; <ethods of doing this are language specific
60

50

*la!sed time

40

30

20

10

0 0 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300 <rra& si=e

Hint #10: ,onsider %L=SQL for >tric-/? SQL


Dith SQ6 you specify the data you want, not how to get it. Sometime you need to specifically dictate your retrieval algorithms. :or e$ampleA
; ; ; ; ; Getting the second highest value Doing loo)ups on a low=high loo)up ta!le *orrelated updates SQ6 with multiple comple$ correlated su!@ueries SQ6 that seems to hard to optimi-e unless it is !ro)en into multiple @ueries lin)ed in 567SQ6

Oracle8i %L=SQL 'mprovements


; Array processing ; ?,*,5H ; (emporary ta!les ; (he profiler ; Dynamic SQ6

0onus hint: @hen /our SQL is tunedA loo- to /our Oracle configuration
Dhen SQ6 is inefficient there is limited !enefit in investing in ,racle server or operating system tuning. However, once SQ6 is tuned, the limiting factor for performance will !e ,racle and operating system configuration. #n particular, chec) for internal ,racle contention that typically shows up as latch contention or unusual wait conditions F!uffer !usy, free !uffer, etcG (hird party tools ; such as QuestQs Spotlight on ,racle product ; can !e invalua!le

www.quest.com

You might also like