Oracle Query Optimization

download Oracle Query Optimization

of 9

Transcript of Oracle Query Optimization

  • 7/26/2019 Oracle Query Optimization

    1/9

    SQL Query Optimization******** Nested loop information *******************Performance tuning focuses on writing efficient SQL, allocating computer resources and analyzing wait events andcontention in the system. he design approach to a data!ase is critical to ensuring the !est performance from a data!ase,here are the steps when designing a data!ase"

    #. $esign the application correctly

    %. une the application SQL code

    &. une memory

    '. une ()O

    . une contention and other issues

    here are two approaches to performance tuning

    pro-active+ try to see the pro!lem !efore it happens i.e. warning alert on a ta!lespace threshold !efore it !ecomes a

    pro!lem

    reactive - the pro!lem has already occurred and action is reuired

    pro+active means that it does not impact the end user where as reactive is generally when the user tells you he)she has apro!lem.O-provides many graphs that can shown you trends a!out a particular area of the data!ase for moreinformation see O-.One rule is first try and fi/ the SQL code, especially if it is old code, no matter how you tune the data!ase if the code is

    !adly written it won0t ma1e any difference, however oracle does provide some parameters that can help with poorly writtencode.Oracle Query process

    Query processing reuires the transformation of your SQL uery into an efficient e/ecution plan, Query optimizationreuires that the !est e/ecution plan is the one e/ecuted, the goal is to use the least amount of resources possi!le 23P4 and()O5, the more resources a uery uses the more impact it has on the general performance of the data!ase.6 users uery will go through & phases

    Parsing

    6t this stage the synta/ 2 decomposed into a relational alge!ra that0s analyzed to see if its syntacticallycorrect5 and semantics 2ma1e sure that ta!les and columns e/ist and you have permissions to access theo!7ects5 are chec1ed, at the end you have a parse tree which represents the uery0s structure.

    he statement is normalized so that it can !e processed more efficiently, once all the chec1s have completedit is considered a valid parse tree and is sent to the logical uery plan generation stage.

    6ll this is done in the li!rary cache of the S86

    Optimization

    he optimizer is used at this stage, which is a cost+!ased optimizer 239O + see !elow for more info5, thischooses the !est access method to retrieve the reuested data. (t uses statistics and any hints specified in theSQL uery, the 39O produces an optimal e/ecution plan for the SQL statement.

    he optimization process can !e divided in two partsQuery rewrite phase : he parse tree is converted into an a!stract logical uery plan, the various nodes and

    !ranches are replaced !y operators of relational alge!ra.

    Execution plan generation phase: Oracle transforms the logical uery plan into a physical uery plan, the

    physical uery or e/ecution plan ta1es into account the following factors

    he various operations 27oins5 to !e performed during the uery

  • 7/26/2019 Oracle Query Optimization

    2/9

    he order in which the operations are performed

    he algorithm to !e used for performing each operation

    he !est way to retrieve data from dis1 or memory

    he !est way to pass data from operation to another during the uery

    he optimizer may well come up with multiple physical plans, all of which are potential e/ecution plans.he optimizer then chooses among them !y estimating the costs of each possi!le plan 2!ased on ta!le andinde/ statistics5 and selecting the plan with the lowest cost. his is called the cost+!ased uery optimization239O5.

    Execution he final stage is to e/ecute the physical uery plan that was selected !y the 39O

    Optimization Mode: (n previous versions of Oracle you had two choices :9O 2rule+!ased optimizer5 or 39O 2cost+!asedoptimizer5, :9O is availa!le in Oracle #;g !ut is a deprecated product the 39O is the preferred and default method.

    RBO

    Rule-Base

    Optimizer

    used a heuristic method to select among several alternative access paths with the help of certain rules. 6llpaths were ran1ed and the lowest was chosen i.e. using the :O

  • 7/26/2019 Oracle Query Optimization

    3/9

    $ata distri!ution histograms

    Num!er if distinct inde/ 1eys

    3ardinality 2the num!er of columns with similar values for each column5

    inimum and a/imum values for each column

    System statistics, which include ()O characteristics of your system and 3P4 statistics which include 3P4 speed and

    other related statistics.

    he 39O will use all of the a!ove statistics and other statistics 23P4, ()O and O)S statistics5 to help with finding theoptimal plan, using the a!ove statistics the 39O can estimate costs of individual operations. he less num!er of statisticscollected will result in less physical plans the 39O can come up with, thus the less num!er of choices the 39O can ma1e.

    3olumn statistics select column@name, num@distinct fro d!a@col@statistics where ta!le@name B 0P-:SONN-L0A

    Optimizer Con#iguration

    he mode level can !e set to a num!er of levels, depending on what your application reuirements are you can start sending

    data to the user uic1ly, if the user reuires all the data to !e seen altogether then the 6LL@:O

  • 7/26/2019 Oracle Query Optimization

    4/9

    method@opt + can specify several things , li1e include histograms, 64O means collect histogramsgranularity + only applies to ta!les, 6LL means collect statistics for su!partitions, partitions, etccascade + collect all statistics for !oth ta!le and inde/option + gather B collect statistics for all o!7ects even it the o!7ect has stale or fresh statistics gather@auto B Oracle will decide on what to collect gather@empty B collect only for o!7ects that do not have any statistics gather@stale B collect only for o!7ects with stale statistics

    Schema [email protected]@schema@stats2 ownname BD 0E6LL-P05A

    a!le [email protected]@ta!le@stats2 0hr0, 0employees05A

    (nde/ [email protected]@inde/@stats2 0hr0, 0employ@id/05A

    System

    [email protected]@stat@ta!le2ownname BD 0vallep0, stat@ta! BD 0stats@ta!le0, t!lspace BD 0stat@t!s05A

    [email protected]@system.stats20start05A 2wait a while then run [email protected]@system.stats20stop05A

    [email protected]@system@stats2interval BD F%;, stat@ta! BD 0stats@ta!le0, stat@id BD 0OLP05A

    Note" you can use the create@stat@ta!le to transfer system stats to other data!ases.

    Gou can also collect statistics !y using the analyzecommand

    6nalyze a ta!leanalyze ta!le employee compute statisticsA

    Note" additional columns are filled in when this is run i.e. average@row@size, last@analyzed,size@of@ta!le 2in data !loc1s5

    /se#ul 0iews

    B&',&B$E) loo1 at the last_analyzedcolumn to ma1e sure statistics are !eing collected

    B&'OB1EC,',&B$E) loo1 at the last_analyzedcolumn to ma1e sure statistics are !eing collected

    ynamic )ampling

    $ynamic sampling is controlled !y the optimizer_dynamic_samplingparameter which accepts values from ; 2off5 to #;2aggressive sampling5 with % as the default.$ynamic sampling can !e a !enefit

    he sample time is small compared to the overall uery e/ecution time

    :esults in !etter performing uery

    Query may !e e/ecuted multiple times

    Con#iguration

    $isplay show parameter optimizer@dynamic@samplingA

    (nstance alter system set optimizer@dynamic@sampling B %A

    Session alter session set optimizer@dynamic@sampling B A

    /sage

    SQL Hintselect )*I dynamic@sampling2employees 5 *) emp@id, fname, lname, 7o!, salfrom employees where dept@num B ;A

  • 7/26/2019 Oracle Query Optimization

    5/9

    E##icient )Q$: o elimate the num!er of rows that the optimizer has to retrieve we use whereclauses, however theoptimizer may not end up writing the !est e/ecution plan, you have !etter 1nowledge of the application than the optimizerand with this 1nowledge you can use hintswhich force the optimizer to use that 1nowledge. 4sing whereclauses efficientlywill reduce the ()O, thus increasing performance, the optimizer will use statistics 2row count5 to determine how to create the

    !est plan, if no statistics are availa!le then it has no option !ut to perform a full ta!le scan.

    Sometimes the optimizer will not use a inde/, even if you 1now one e/ists, the possi!le reason for these could !e any of thefollowing

    Eiewsin a uery sometimes prevent the use of inde/es

    (f you thin1 heavy data s1ew is in the ta!le, using histograms will help the optimizer to provide more accurate

    representations of the data distri!ution in the ta!le.

    (f for some reason it still refuses to use the inde/ you can force it !y using a hint.

    ry using the where clause instead of the havingclause, as the havingclause incurs the additional overhead of sorting andsumming.

    2ints: Sometimes the 39O does not 1now !est and needs help to point it in the right direction, this is where hints help !y

    forcing the optimizer to ta1e a path that you have 1nowledge a!out, hintscan alter the 7oin methods, 7oin order or evenaccess paths. he are many hint options you would need to see the Oracle documentation for a full listing !ut i have listedsome of the more common one

    all_rows+ optimize throughput, not optimizer the response time of the statement

    first_rows - return the first rows uic1ly

    full+ perform a full ta!le scan

    ordered+ force the 7oin order of the ta!les in the uery

    index+ force the use of inde/es

    index_ffs+ force a full scan of a inde/, can use parallelization for this to improve performance.

    -/amples

    select )*I C4LL 2employees5 *) ... from employeesselect )*I (N$-J@CCS 2employees5 *) ... from employeesselect )*I 4S-@NL 2employees, dept5 *) .. from employees, dept

    Note" see oracle documentation for full listing of all the hints

    1oin Methods

    Gou should choose a 7oin method !ased on how many rows you e/pect to !e returned from the 7oin

    Cartesian

    1oins

    cartesian 7oins are normally a result of not using a where clause, it !asically 7oins every row in all ta!les, sofor an e/ample if one ta!le as ;,;;; rows and the other ta!le has #;; rows then a cartesian 7oin of the ta!leswould !e ;,;;; * #;; B ;;,;;; rows

    3ested

    $oops

    (f you are 7oin ta!les with few than #;,;;; rows then a Nested Loop would !e the way to go, see hintssectiona!ove for more details on how to use a Nested Loop

    2ash 1oin 4se if the 7oin will produce large su!sets of data or a su!stantial proportion of a ta!le is going to !e 7oined.

    Merge 1oin (f the ta!les in the 7oin are !eing 7oined with an ineuality condition 2not an eui 7oin5, then use a merge 7oin

    Bitmap 1oin9asically used for data warehouses, do not use if running OLP system, they are used with low cardinalitycolumns 2columns having low distinct values + gender, marital @status, relation, etc5

  • 7/26/2019 Oracle Query Optimization

    6/9

    See7oin methodsfor more information regarding different types of 7oins

    +ndex )trategy: 6n inde/ is a data structure that ta1es the value of one or more columns of a ta!le 2the 1ey5 and returns allrows 2or the reuested columns in that row5 with that value of the column uic1ly. he efficiency of the inde/ is that it letsyou find the necessary rows without having to perform a full ta!le scan, this leads to few ()O0s.6s a general rule you so only use a inde/ if you select a!out #;+#? of a ta!le, when using a inde/ this prevents you from

    performing a full ta!le scan.

  • 7/26/2019 Oracle Query Optimization

    7/9

    alter system set cursor@sharing B e/tactANote"force + forces the use of !ind varia!les, oracle will su!stitute the hard coded varia!lesimilar + force the use of a !ind varia!le only if oracle thin1s it will not adversely affectsoptimization.e/tact + only use the already parsed uery if it is identical 2hash 1ey matches5

    Materialized 0iews: (f you are dealing with very large amounts of data, you should consider using materialized views toimprove response time. aterialized views are o!7ects with summary data from the underlying ta!le. -/pensive ta!le 7oinscan !e done !eforehand and saved in the materialized view. Gou can use the pac1age dbms_olappac1age to getrecommendations on ideal materialized views.

    )tored Outlines: he 39O doesn0t always use the same e/ecution strategy, changes within the data!ase can force the 39Oto change its plan. Gou can force oracle to use the same plan !y using the plan sta!ility featurestored outlinesto preservethe current e/ecution plans, even if the statistics and optimizer mode changes. he only catch is that the SQL statementmust !e identical if you wish to use the stored outline.

    Stored outlines are use when you plan to migrate from one oracle version to another, thus you can cut ris1s and preserve the

    applications present performance via stored outlines. Outlines ensure that the e/ecution paths the ueries used in a testinstance successfully carry over to the production instance. 6lso it can !e used to override the code that is im!edded in theapplication.he more common name for this feature is called optimizer stability. 6ll the information on stored outlines is stored in theO4LN schema in two ta!les OLK and OLKH(NS 2these are created with you install Oracle5.

    System parameters

    alter system set uery@rewrite@ena!led B trueAalter system set star@transformation@ena!led B trueAalter system set optimizer@features@ena!led B #;.%.;A

    Note" the a!ove values must !e the same on all instances, when using this feature across

    different data!ases.

    Stored Outlines2data!ase5

    alter system set create@stored@outlines B trueANote" this can use lots of dis1 space

    Stored Outlines 2session5 alter session set create@stored@outlines B trueA

    3reate Outline

    create outline test@outline

    on select employee@id, last@name from hr.employeesA

    4se Outlinealter system set use@stored@outline B trueAalter session set use@stored@outline B trueA

    -dit Outline

    4se the d!ms@outln@edit pac1ageOptions++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++change@7oin@pos + changes the 7oin position for the hint identified !y outline namecreate@edit@ta!les + creates outline editing ta!les in calling a users schemadropedit@ta!les + drops outline editong ta!les

    generate@signature + generates a signature for the specified SQL te/treferesh@private@outline + refreshes the in+memory copy of the outline

    :emove Outline drop outline test@outlineA

    /se#ul 0iews

  • 7/26/2019 Oracle Query Optimization

    8/9

    B&'O/,$+3E) 6 view of the !elow ta!les 2name, owner, category, used, sl@te/t5

    O$53ontains the outlines 2name, sl@te/t, signature, category, flags, etc5

    Note" flags is useful as it can tell you if the outline is !eing used.

    O$52+3,) 3ontains the outlines hints 2name, hint@type, hint@te/t, ta!le@name, etc5

    O$53OE) 3ontains the outlines nodes

    Pac.agesBM)'O/,$3 anage stored outlines and their outline categories

    BM)'O/,$3'E+, anage stored outlines and their outline categories

    2istograms: he 39O normally assumes that the data is uniformly distri!uted in the ta!le, however there are times whenthe data is e/tremely s1ewed which means you are !etter off using histograms to store column statistics, histograms providemore efficient access methods. Histograms use !uc1ets to represent distri!ution of data in a column and Oracle uses these

    !uc1ets to how s1ewed the data distri!ution is.

    Gou can use the following histograms

    height-based+ divide column values into !ands, with each !and containing a roughly eual num!er of rows

    frequency-based+ determine the num!er of !uc1ets !ased on the distinct values in the column, each !uc1et contains

    all the data that has the same value

    height+!ased

    [email protected]@ta!le@stats2 ownname BD 0H:0, ta!name BD 0!enefits0, method@opt BD 0for column size #; num!er@of@vists05AendA)

    [email protected]@ta!le@stats2 ownname BD 0H:0, ta!name BD 0!enefits0, method@opt BD 0for column size s1ewonly05AendA)

    [email protected]@ta!le@stats2 ownname BD 0H:0, ta!name BD 09-N-C(S0, method@opt BD 0for all columns size auto05AendA)

    Note" for column + your own histogram creation auto + let oracle decide what to do!ased on data distri!ution and wor1load s1ewonly + !ased the decision only on thedata distri!ution of the columns

    freuency+!ased

    [email protected]@ta!le@stats2 ownname BD 0H:0, ta!name BD 0!enefits0, method@opt BD 0for column size %; department@i+d05AendA)

    /se#ul 0iews

    B&',&B'2+),O6R&M) descri!es histograms on columns of all ta!les in the data!ase

    B&'2+),O6R&M) is a synonym for $96@69@H(SO8:6S

    B&'P&R,'2+),O6R&M)provides the histogram data 2end+points per histogram5 for histograms on all ta!lepartitions in the data!ase

    B&')/BP&R,'2+),O6R&M)lists actual histogram data 2end+points per histogram5 for histograms on all ta!lesu!partitions in the data!ase

  • 7/26/2019 Oracle Query Optimization

    9/9

    P$7)Q$ Per#ormance: