12c Optimizer Overview

Embed Size (px)

Citation preview

  • 8/13/2019 12c Optimizer Overview

    1/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.1

  • 8/13/2019 12c Optimizer Overview

    2/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.2

    Oracle Database 12cOptimizer Overview

    Maria ColganMaster Product Manager

  • 8/13/2019 12c Optimizer Overview

    3/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.3

    Agenda

    Adaptive Query Optimization

    Optimizer Statistics enhance

    SQL Plan Management enha

  • 8/13/2019 12c Optimizer Overview

    4/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.4

    Adaptive PlansJoin methods

    Parallel distribution methods

    Adaptive Statistics

    Discovered at compile timeDiscovered at run time

    Adaptive Query OptimizationOverview

    Adaptive Query

    Optimization

    Adaptive Plans

    Join

    Methods

    Parallel

    distribution

    Methods

    At

    compil

    time

  • 8/13/2019 12c Optimizer Overview

    5/102Copyright 2012, Oracle and/or its affiliates. All rights reserved.5

    Query: Find all of theproducts with a unit price of15 that we have sold morethat 1 of

    Two possible join methods

    for this query

    Nested Loops

    Hash Join

    Adaptive Execution PlansAdapt join methods

  • 8/13/2019 12c Optimizer Overview

    6/102Copyright 2012, Oracle and/or its affiliates. All rights reserved.6

    Alternative sub-plans arepre-computed

    Sub-plans stored in thecursor

    Stats collect inserted beforejoin

    Rows buffered until finaldecision is made

    Adaptive Execution PlansAdapt join methods

    Table scan

    Order _items

    NESTED LOOPS

    Index Scan

    Prod_info_ind

    StatisticsCollector

    Default Plan is a nested lo

    HASH JO

    Rows coming out of order_itemstabl

    to a point If row count is less than the th

    nested Loops otherwise switch to hash j

  • 8/13/2019 12c Optimizer Overview

    7/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.7

    Number of rows seen instatistics collector exceedsthreshold

    Plan switches to hash join

    Statistics collect disabled

    Plan resolved on firstexecution & remains thesame for subsequentexecutions

    Adaptive Execution PlansAdapt join methods

    Final Plan is a hash join

    Statistics collector disabled after decisio

    made and becomes a pass through ope

    Table scan

    Order _items

    NESTED LOOPS

    Index ScanProd_info_ind

    HASH JOI

    T

    P

    StatisticsCollector

  • 8/13/2019 12c Optimizer Overview

    8/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.8

    Explain plan commandalways shows default plan

    Example shows a nestedloops join as default plan

    No statistics collector shownin plan

    Adaptive Execution PlansDisplaying the default plan

  • 8/13/2019 12c Optimizer Overview

    9/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.9

    After the statement hascompleted useDBMS_XPLAN.DISPLAY_CURSOR

    tosee the final plan selected

    Example shows that hash

    join picked at execution timeAgain the statistics collectoris not visible in the plan

    Adaptive Execution PlansDisplaying the final plan

  • 8/13/2019 12c Optimizer Overview

    10/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.10

    Full adaptive plan displayedwhen format parameter+adaptive is set

    Example shows both the

    nested loops and hash joinin the plan

    Adaptive Execution PlansDisplaying the full adaptive plan

  • 8/13/2019 12c Optimizer Overview

    11/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.11

    Additional informationdisplayed on why operationsare inactive can be seen

    with format parameter+report

    Adaptive Execution PlansDisplaying plan with +adaptive & +report formats

  • 8/13/2019 12c Optimizer Overview

    12/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.12

    New column in V$SQLIS_RESOLVED_ADAPTIVE_PLAN

    Indicates statement had anadaptive plan which wasresolved on first execution

    Resolved plan is used forsubsequent executions

    Statistics collectors andbuffering is disabled

    Adaptive Execution PlansIndicator in V$SQL

  • 8/13/2019 12c Optimizer Overview

    13/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.13

    Adaptive plans are enabled bydefault

    Can be put in reporting mode

    OPTIMIZER_ADAPTIVE_REPORTING_ONLY

    Reporting mode shows what

    would have happened duringexecution in the plan

    Adaptive Execution PlansReporting mode

  • 8/13/2019 12c Optimizer Overview

    14/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.14

    Adaptive Execution Plans

    Join method decision deferred until runtime Default plan is computed using available statistics

    Alternate sub-plans are pre-computed and stored in the curso

    Statistic collectors are inserted at key points in the plan

    Final decision is based on statistics collected during execut

    Default plan and sub-plans have a valid range for statistics co

    If statistics prove to be out of range, the sub-plans will be swa

    Requires buffering near the swap point to avoid returning rows

    Possible sub-plans are nested loop joins or hash joins and

    Adaptive join methods

  • 8/13/2019 12c Optimizer Overview

    15/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.15

    Adaptive Execution PlansAdaptive parallel data distribution

    User connects to the

    databaseUser

    Shadow process is

    spawned

    WhparthebecCo

    Parallel servers communicate among themselves & theQC using messages that are passed via memory buffersin the shared pool

    How the data is distributed among the processes isbased on the operation being done & number of rowsexpected

    QCsepo

    the

    Paindpe

  • 8/13/2019 12c Optimizer Overview

    16/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.16

    Adaptive Distribution Methods

    Cardinality based distribution skew common scenario Crucial for parallel join of very small data sets with very large data

    Distribution method decision based on expected number of row

    New adaptive distribution method HYBRID-HASH

    Statistic collectors inserted in front of PX process on the left hand

    If actual number of rows less than threshold, switch from HASH to

    Threshold number of total rows < 2x DOP

    Enabled by default

    Hybrid-HASH Join

  • 8/13/2019 12c Optimizer Overview

    17/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.17

    Hybrid hash join betweenEMP and DEPT

    Distribution method basedon runtime stats

    Statistics collector insertedin front of PX processesscanning DEPT

    Adaptive Execution PlansAdaptive parallel data distribution Query

    coordinator

    P1 P2 P3

    P5

    P6

    P7

    P8

    DEPT

    Statistics Collectorthreshold 2X DOP

  • 8/13/2019 12c Optimizer Overview

    18/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.18

    Hybrid hash join betweenEMP and DEPT

    Distribution methodbased on runtime stats

    Statistics collectorinserted in front of PXprocesses scanningDEPT

    Adaptive Distribution Methods

  • 8/13/2019 12c Optimizer Overview

    19/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.19

    Hybrid hash join betweenEMP and DEPT

    Distribution methodbased on runtime stats

    If DEPT uses Broadcast- EMP uses Round-Robin

    Adaptive Distribution Methods

    Broadcast/Round Robin

    DOP used is 6Number of rows returned is 4

    Broadcast used because 24 rows dist

  • 8/13/2019 12c Optimizer Overview

    20/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.20

    Hybrid hash join betweenEMP and DEPT

    Distribution methodbased on runtime stats

    If DEPT uses Broadcast

    - EMP uses Round-Robin

    If DEPT used HASH- EMP uses HASH

    Adaptive Distribution Methods

    Broadcast/Round Robin

    Hash/Hash

    DOP used is 2Hash used because only 4 rows distrib

  • 8/13/2019 12c Optimizer Overview

    21/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.21

    Adaptive PlansJoin methods

    Parallel distribution methods

    Adaptive Statistics

    Discovered at compile timeDiscovered at run time

    Adaptive Query OptimizationOverview

    Adaptive Query

    Optimization

    Adaptive Plans

    Join

    Methods

    Parallel

    distribution

    Methods

    At

    compil

    time

  • 8/13/2019 12c Optimizer Overview

    22/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.22

    Dynamic Statistics

    During compilation optimizer decides if statistics are sufficiegenerate a good plan or not

    Dynamic statistics are used to compensate for missing, staincomplete statistics

    They can be used for table scans, index access, joins and g

    One type of dynamic statistics is dynamic sampling

  • 8/13/2019 12c Optimizer Overview

    23/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.23

    Dynamic sampling has anew level 11(AUTO)

    Decision to use dynamicsampling depends on thecomplexity of predicate,existing statistics and

    total execution time

    Dynamic statistics sharedamong queries

    Dynamic StatisticsDynamic Sampling

  • 8/13/2019 12c Optimizer Overview

    24/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.24

    Dynamic Statistics SGA

    SHARED_POOL

    Select *

    Fromproduct_information

    Where list_price-min_price=29

    And category_id not in (11,22)

    And prod_name like Smart%;

    Optimizer checksfor existing statisticsin data dictionary

    2

    SHARED DYNAMIC

    DATA DICTIONARY

    SQL statementis submitted

    1

    Optimizer determines

    plan using dynamicstatistics

    5

    Cursor 0: Select * from prod

    Dynamic samplingoccurs on small number ofblocks from tableResulting dynamic statisticsare stored in cache

    4

    Statistics found but need to beaugmented due to complex predicates

    3

    Table cardinality estima

  • 8/13/2019 12c Optimizer Overview

    25/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.25

    Dynamic Statistics SGA

    SHARED_POOL

    Select supplier_id, prod_name

    Fromproduct_information

    Where list_price-min_price=29

    And category_id not in (11,22)

    And prod_name like Smart%;

    Optimizer checksfor existing statisticsin data dictionary

    7

    SHARED DYNAMIC

    DATA DICTIONARY

    Different SQLstatement issubmitted withsame predicates

    6

    Optimizer determines

    plan using dynamicstatistics

    10

    Cursor 0: Select supplier_id

    Necessary Dynamicstatistics found in sharedcache

    9

    Statistics found but need to beaugmented due to complex predicates

    8

    Table cardinality estima

  • 8/13/2019 12c Optimizer Overview

    26/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.26

    Adaptive Statistics

    During execution optimizer estimates are compared to exec If statistics vary significantly then a new plan will be chosen

    subsequent executions based on execution statistics

    Re-optimization uses statistics gathered from previous exec

    First introduced as Cardinality Feedback in 11.2

    Re-optimization

  • 8/13/2019 12c Optimizer Overview

    27/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.27

    Adaptive Statistics

    Statistics gathered about data volume and data type seen d If execution statistics vary significantly statement will be har

    the next execution using the execution statistics instead

    Statements are only monitored once if they dont show sign

    differences initially they wont change in the future

    Only individual table cardinalities and group by estimates e Information is stored in the cursor only and is lost if cursor a

    Cardinality Feedback pre 12c

  • 8/13/2019 12c Optimizer Overview

    28/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.28

    Initial execution of a queryis monitored

    Example shows initial planis hash join between sales

    and customers

    Cardinality estimates 8X off

    Adaptive StatisticsCardinality Feedback pre 12c

    Initial Cardinality estimat

  • 8/13/2019 12c Optimizer Overview

    29/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.29

    Execution statistics used toreparse the statement onthe second execution

    New plan shows correctcardinality estimates and a

    new join order

    Information learnt is storedin the cursor only and is lostif cursor ages out

    Adaptive StatisticsCardinality Feedback pre 12c

    Execution Plan after the second execu

  • 8/13/2019 12c Optimizer Overview

    30/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.30

    Adaptive Statistics

    Join statistics are also monitored

    Works with adaptive cursor sharing for statement with binds

    New Column in V$SQL IS_REOPTIMIZABLE

    Information found at execution time is persisted as SQL Pla

    Re-optimization in 12c

  • 8/13/2019 12c Optimizer Overview

    31/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.31

    New column in V$SQLIS_REOPTIMIZABLE

    Indicates that the statementwill be re-parsed on the nextexecution

    Adaptive StatisticsRe-optimizationindicator in V$SQL

  • 8/13/2019 12c Optimizer Overview

    32/102

  • 8/13/2019 12c Optimizer Overview

    33/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.33

    On second executionstatement is re-parsedusing execution statistics

    New child cursor createdwith new plan

    New plan changes the joinorder

    Adaptive StatisticsAdaptive Plans & Re-optimization working together

  • 8/13/2019 12c Optimizer Overview

    34/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.34

    Adaptive Statistics

    Directives are additional information used during optimizatiogenerate a better plan

    For example, when table T1 is joined to T2 use dynamic statisaccurate cardinality estimate

    Directives are collected on query expressions not at a stateme

    Allows for directives to be used for multiple statements

    Persisted on disk in the SYSAUX tablespace

    Directives will be automatically maintained

    Managed using the new package DBMS_SPD

    SQL Plan Directives

  • 8/13/2019 12c Optimizer Overview

    35/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.35

    SQL Plan Directives SGA

    SHARED_POOL

    Select *

    FROM customers

    where state =CA

    AND country=USA;

    SQL statementis submitted

    Cursor 0: Select * from cust..

    Optimizer determines

    plan

    2

    DIRECTIV

    During executioncardinality estimatediscovered to be wrongand directive created

    3

    IS_REOPTIMIZABLE

    Directive: Use DS fowhen column city, co

    1

    Sysaux Tablespace

  • 8/13/2019 12c Optimizer Overview

    36/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.36

    SQL Plan Directives

    Select *

    FROM customers

    where state=CA

    AND country=USA;

    SQL Directives

    Sysaux Tablespace

    SGA

    This execution andsubsequent executionuse child cursor 1

    6

    Directive: Use DS fowhen column city, co

    SHARED_POOLCursor 0: Select * from cust

    IS_REOPTIMIZABLE =

    Optimizer determinesnew plan usingexecution statistics

    5

    Cursor 1: Select * from custoIS_REOPTIMIZABLE =

    Directpersissysau

    Extension Binds Stats Sqlid

    SQL Directives

    DIRECTIVDIRECTIV

    Same SQLstatement issubmittedagain

    4

  • 8/13/2019 12c Optimizer Overview

    37/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.37

    SQL Plan Directives

    Select email

    FROM customers

    where state=MA

    AND country=USA;

    New SQLstatement issubmitted oncustomers table

    7

    Optimizerchecks fordirectives oncustomers tableand finds one onthe columns stateand country

    8

    Cursor 0: Select email from

    Optimizer determinesplan with help of

    directive

    9

    Optimizer addscolumn group creationfor state & countrycolumns to next statisticsgather on customer table

    10

    SGADIRECTIV

    SHARED_POOL

    Sysaux Tablespace

    Extension Binds Stats Sqlid

    SQL Directives

    DIRECTIV

  • 8/13/2019 12c Optimizer Overview

    38/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.38

    Number of SQL PlanDirectives used for astatement is displayed inthe notes section of theexecution plan

    In this case one SQL

    Plan Directive was used

    SQL Plan DirectivesIn use

  • 8/13/2019 12c Optimizer Overview

    39/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.39

    SQL Plan DirectivesMonitoring

    Auto flushed every 15 minutes

  • 8/13/2019 12c Optimizer Overview

    40/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.40

    SQL Plan DirectivesMonitoring

    Auto flushed every 15 minutes

  • 8/13/2019 12c Optimizer Overview

    41/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.41

    SQL Plan Directive statewas MISSING_STATS

    Indicates Oracle canautomatically gatherstatistics to improve theoriginal mis-estimation

    In this case a columngroup on STATEandCOUNTRYwill addressthe problem

    SQL Plan DirectivesGather statistics to allow automatic creation of missing statist

  • 8/13/2019 12c Optimizer Overview

    42/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.42

    SQL Plan DirectivesMonitoring

  • 8/13/2019 12c Optimizer Overview

    43/102

  • 8/13/2019 12c Optimizer Overview

    44/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.44

    Adaptive PlansJoin methods

    Parallel distribution methods

    Adaptive StatisticsDiscovered at compile time

    Discovered at run time

    Adaptive Query OptimizationOverview

    Adaptive Query

    Optimization

    Adaptive Plans

    Join

    Methods

    Parallel

    distribution

    Methods

    At

    compil

    time

  • 8/13/2019 12c Optimizer Overview

    45/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.45

    Summary

    Optimizer begins to learn from its mistakes

    Adaptive Plans

    Adapt join methods

    Adapt parallel distribution methods

    Adaptive Statistics

    Dynamic statistics Re-optimization

    SQL Plan Directives

    And lots of other great features!

  • 8/13/2019 12c Optimizer Overview

    46/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.46

    Agenda

    Adaptive Query Optimization

    Optimizer Statistics enhance

    SQL Plan Management enha

  • 8/13/2019 12c Optimizer Overview

    47/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.47

    Statistics

    Enhancements

    New types of histograms

    Online statistics gathering

    Session level statistics for G

    Enhanced incremental statis

    Concurrent statistics gatheri

    Automatic detection of colum

    Statistic gathering reporting

    Hi t

  • 8/13/2019 12c Optimizer Overview

    48/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.48

    Histograms

    Histograms tell Optimizer about the data distribution in a Co

    Creation controlled by METHOD_OPT parameter

    Default create histogram on any column that has been usedWHERE clause or GROUP BY of a statement AND has a d

    Relies on column usage information gathered at compilatio

    stored in SYS.COL_USAGE$ Four types of histograms

    Frequency

    Top-Frequency

    Oracle Confidential

    Height balanced

    Hybrid

    Hi t

  • 8/13/2019 12c Optimizer Overview

    49/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.49

    Histograms

    A frequency histogram is only created if the number of distin

    a column (NDV) is less than 254 values

    Frequency Histograms (FREQUENCY)

    Frequency histogram

    Oracle Confidential

    Hi t

  • 8/13/2019 12c Optimizer Overview

    50/102

    Copyright 2012, Oracle and/or its affiliates. All rights reserved.50

    Histograms

    Traditionally a frequency histogram is only created if NDV