Oracle Explain Plan

download Oracle Explain Plan

of 52

Transcript of Oracle Explain Plan

  • 7/29/2019 Oracle Explain Plan

    1/52

    1

  • 7/29/2019 Oracle Explain Plan

    2/52

    Explaining the Explain Plan

  • 7/29/2019 Oracle Explain Plan

    3/52

    Disclaimer

    The goal of this session to provide you with a guidefor reading SQL execution plans and to help youdetermine if that plan is what you should be expecting

    This session will not provide you with suddenenlightenment making you an Optimizer expert or giveyou the power to tune SQL statements with the flick ofyour wrist!

  • 7/29/2019 Oracle Explain Plan

    4/52

    Agenda

    What is an execution plan and how to generate one

    What is a good plan for the optimizer

    Understanding execution plans Cardinality

    Access paths

    Join order

    Join type

    Partitioning pruning

    Parallelism Execution plan examples

  • 7/29/2019 Oracle Explain Plan

    5/52

    What is an execution

    plan and how togenerate one

  • 7/29/2019 Oracle Explain Plan

    6/52

    What is an Execution plan?

    Execution plans show the detailed steps necessary toexecute a SQL statement

    These steps are expressed as a set of databaseoperators that consumes and produces rows

    The order of the operators and their implementation isdecided by the optimizer using a combination of querytransformations and physical optimization techniques

    The display is commonly shown in a tabular format,

    but a plan is in fact tree-shaped

  • 7/29/2019 Oracle Explain Plan

    7/52

    What is an Execution plan?

    QuerySELECT prod_category,avg(amount_sold)FROM sales s, products pWHERE p.prod_id = s.prod_idGROUP BY prod_category;

    Tabular representation of plan

    -----------------------------------------------------------Id Operation Name-----------------------------------------------------------0 SELECT STATEMENT1 HASH GROUP BY2 HASH JOIN3 TABLE ACCESS FULL PRODUCTS4 PARTITION RANGE ALL5 TABLE ACCESS FULL SALES----------------------------------------------------------

    Tree-shaped representation of plan

    GROUP BY|JOIN_______|_______| |TABLE ACCESS TABLE ACCESSPRODUCTS SALES

  • 7/29/2019 Oracle Explain Plan

    8/52

    How to get an Execution Plan

    Two methods for looking at the execution plan

    1.EXPLAIN PLAN command Displays an execution plan for a SQL statement without actually

    executing the statement

    2.V$SQL_PLAN A dictionary view introduced in Oracle 9i that shows the execution

    plan for a SQL statement that has been compiled into a cursor inthe cursor cache

    Use DBMS_XPLAN package to display plans

    Under certain conditions the plan shown with EXPLAIN PLANcan be different from the plan shown using V$SQL_PLAN

  • 7/29/2019 Oracle Explain Plan

    9/52

    How to get an Execution Plan

    Example 1 EXPLAIN PLAN command & dbms_xplan.display functionSQL> EXPLAIN PLAN FOR

    SELECT prod_category, avg(amount_sold)

    FROM sales s, products p

    WHERE p.prod_id = s.prod_id

    GROUP BY prod_category;

    Explained

    SQL> SELECT plan_table_outputFROM table(dbms_xplan.display('plan_table',null,'basic'));

    ------------------------------------------Id Operation Name------------------------------------------

    0 SELECT STATEMENT1 HASH GROUP BY2 HASH JOIN3 TABLE ACCESS FULL PRODUCTS4 PARTITION RANGE ALL5 TABLE ACCESS FULL SALES-------------------------------------------

  • 7/29/2019 Oracle Explain Plan

    10/52

    How to get an Execution Plan

    Example 2 Generate & display execution plan for the last SQL stmtsexecuted in a session

    SQL>SELECT prod_category, avg(amount_sold)FROM sales s, products pWHERE p.prod_id = s.prod_idGROUP BY prod_category;

    no rows selected

    SQL> SELECT plan_table_outputFROM table(dbms_xplan.display_cursor(null,null,'basic'));

    ------------------------------------------Id Operation Name------------------------------------------0 SELECT STATEMENT1 HASH GROUP BY2 HASH JOIN3 TABLE ACCESS FULL PRODUCTS4 PARTITION RANGE ALL5 TABLE ACCESS FULL SALES-------------------------------------------

  • 7/29/2019 Oracle Explain Plan

    11/52

    How to get an Execution Plan

    Example 3 Displaying execution plan for any other statementfrom V$SQL_PLAN

    1.Directly:

    SQL> SELECT plan_table_output FROMtable(dbms_xplan.display_cursor('fnrtqw9c233tt',null,'basic'));

    2.Indirectly:SQL> SELECT plan_table_outputFROM v$sql s,TABLE(dbms_xplan.display_cursor(s.sql_id,s.child_number,'basic')) tWHERE s.sql_text like 'select PROD_CATEGORY%';

    Note More information on www.optimizermagic.blogspot.com

  • 7/29/2019 Oracle Explain Plan

    12/52

    What is a good plan

    for the optimizer

  • 7/29/2019 Oracle Explain Plan

    13/52

    Whats a Good Plan for the Optimizer?

    The Optimizer has two different goals

    Serial execution: Its all about cost The cheaper, the better

    Parallel execution: its all about performance The faster, the better

    Two fundamental questions:

    What is cost?

    What is performance?

  • 7/29/2019 Oracle Explain Plan

    14/52

    What is Cost?

    A magically number the optimizer makes up?

    Resources required to execute a SQL statement?

    Result of complex calculations?

    Estimate of how long it will take to execute a statement?

    Actual Definition

    Cost represents units of work or resources used

    Optimizer uses CPU & memory usage plus IO as units of work

    Cost is an estimate of the amount of CPU and memory plus thenumber of disk I/Os, used in performing an operation

    Cost is an internal Oracle measurement

  • 7/29/2019 Oracle Explain Plan

    15/52

    What is performance?

    Getting as many queries completed as possible?

    Getting fastest possible elapsed time using the fewestresources?

    Getting the best concurrency rate?

    Actual Definition

    Performance is fastest possible response time for query

    Goal is to complete the query as quickly as possible

    Optimizer does not focus on resources needed to execute the plan

  • 7/29/2019 Oracle Explain Plan

    16/52

    Understanding an

    Execution Plan

  • 7/29/2019 Oracle Explain Plan

    17/52

    SQL Execution Plan

    When looking at a plan can you determine if the following iscorrect? Cardinality

    Are the correct number of rows coming out of each object?

    Access paths

    Is the data being accessed in the best way? Scan? Index lookup?

    Join order

    Are tables being joined in the correct order to eliminate as much data asearly as possible?

    Join type

    Are the right join types being used?

    Partitioning pruning

    Did I get partition pruning? Is it eliminating enough data?

    Parallelism

  • 7/29/2019 Oracle Explain Plan

    18/52

    Cardinality

    What is it?

    Estimate of number rows that will be returned Cardinality for a single value predicate = num_rows total /

    num_distinct total E.g. 100 rows total, 10 distinct values => cardinality=10 rows

    OR if histogram present num_rows * Density

    Why should you care? Influences access method and Join Order

    If estimate is off it can have a huge impact on a plan

    What causes Cardinality to be wrong? Data Skews

    Multiple single column predicates on a table

    A function wrapped where clause predicate

  • 7/29/2019 Oracle Explain Plan

    19/52

    Cardinality or Selectivity

    Cardinality the estimated # of rows returned

    To determine correct cardinality using a simple SELECT COUNT(*) from eachtable applying any WHERE Clause predicates belonging to that table

  • 7/29/2019 Oracle Explain Plan

    20/52

    Data Skew

    Cardinality = num_rows / num_distinct

    If there is a data skew the selectivity could be way off

    Create a histogram to correct the selectivity calculation

    Oracle automatically creates a histogram if it suspects a data skew

    Be careful

    Histograms have an interesting side effects on statements with binds

    Less relevant for data warehousing

    Prior to 11g stmt with binds had only one plan based on first literal value

    But presence of a histogram indicate skew unlikely one plan good for allbind values

    In 11g multiple execution plans allowed for a single statement

  • 7/29/2019 Oracle Explain Plan

    21/52

    Multiple Single Column Predicates

    Optimizer always assumes each additional predicate

    increases the selectivitySelectivity of predicate 1 * selectivity of predicate 2 etc

    But real data often shows correlations

    Job title influences salary, car model influences make

    How do you tell the Optimizer about the correlation?Extended Optimizer Statistics provides a mechanism tocollect statistics on a group of columns

    Full integration into existing statistics framework

    Automatically maintained with column statistics

    Instantaneous and transparent benefit for any migrated application

  • 7/29/2019 Oracle Explain Plan

    22/52

    A function Wrapped Where Clause

    Predicate

    SELECT *

    FROM customers

    WHERE lower(country_id) = 'us';

    Applying a function to a column means the optimizer does not know how itwill effect the cardinality

    Most likely the optimizer will under-estimate the cardinality

    Creating extended statistics for this function allows the optimizer to get thecorrect cardinality

    exec dbms_stats.gather_table_stats(sh,'customers', method_opt => -

    'for all columns size skewonly for columns(lower(country_id))');

  • 7/29/2019 Oracle Explain Plan

    23/52

    Access Paths

    How to get data out of the table

    The access path can be: Full table scan

    Table access by Rowid

    Index unique scan

    Index range scan (descending)

    Index skip scan

    Full index scan

    Fast full index scan

    Index joins Bitmap indexes

  • 7/29/2019 Oracle Explain Plan

    24/52

    Access Path

    If you know the wrong access method is being used check cardinality, joinorder

    Look in Operation session to seehow obj is being accessed

  • 7/29/2019 Oracle Explain Plan

    25/52

    Access Path examplesA table countries contains 10K rows & has a primary key oncountry_id What plan would you expect for these queries?Select country_id, name from countries

    where country_id in ('AU','FR','IE);

    Select country_id, name from countries

    where country_id between 'AU' and 'IE';

    Select country_id, name from countries where name='USA';

  • 7/29/2019 Oracle Explain Plan

    26/52

    Join Type

    A Join retrieve data from more than one table

    Possible join types are

    Nested Loops joins

    Hash Joins

    Partition Wise Joins

    Sort Merge joins

    Cartesian Joins

    Outer Joins

  • 7/29/2019 Oracle Explain Plan

    27/52

    Join Type Example 1

    What Join type should be use for this Query?

    SELECT e.name, e.salary, d.dept_nameFROM hr.employees e, hr.departments d

    WHERE d.dept_name IN ('Marketing,'Sales')

    AND e.department_id=d.department_id;

    Employees has 107 rows

    Departments has 27 rows

    Foreign key relationship between Employees and Departments on dept_id

  • 7/29/2019 Oracle Explain Plan

    28/52

    Join Type Example 2

    What Join type should be use for this Query?

    SELECT o.customer_id, l.unit_price * l.quantity

    FROM oe.orders o ,oe.order_items l

    WHERE l.order_id = o.order_id;

    Orders has 105 rows

    Order Items has 665 rows

  • 7/29/2019 Oracle Explain Plan

    29/52

    Join Type Example 3

    What Join type should be use for this Query?

    SELECT o.order_id,0.order_date,e.nameFROM oe.orders o , hr.employees e;

    Orders has 105 rows

    Employees has 107 rows

  • 7/29/2019 Oracle Explain Plan

    30/52

    Join Type Example 4

    What Join type should be use for this Query?

    SELECT d.department_id,e.emp_idFROM hr.employees e FULL OUTER JOIN hr.departments d

    ON e.department_id = d.department_id;

    Employees has 107 rows

    Departments has 27 rows

    Foreign key relationship between Employees and Departments on dept_id

  • 7/29/2019 Oracle Explain Plan

    31/52

    Join Type

    If the wrong join type is used go back and check the stmt is written correctlyand the cardinality estimates are accurate

    Look in the Operation section tocheck the right join type is used

  • 7/29/2019 Oracle Explain Plan

    32/52

    Join Orders

    The order in which the tables are join in a multi table stmt

    Ideally start with the table that will eliminate the most rows

    Strongly effected by the access paths available

    Some basic rules Joins that definitely results in at most one row always go first

    When outer joins are used the table with the outer joinoperator must come after the other table in the predicate

    If view merging is not possible all tables in the view will bejoined before joining to the tables outside the view

  • 7/29/2019 Oracle Explain Plan

    33/52

    Join order

    If the join order is not correct, check the statistics, cardinality & access methods

    1

    2

    3

    Want to start with the table thatreduce the result set the most

  • 7/29/2019 Oracle Explain Plan

    34/52

    Sales Table

    May 22nd 2008

    May 23rd 2008

    May 24th 2008

    May 18th 2008

    May 19th 2008

    May 20th 2008

    May 21st 2008

    Select sum(sales_amount)From SALES

    Where sales_date between

    to_date(05/20/2008,MM/DD/YYYY)

    And

    to_date(05/23/2008,MM/DD/YYYY);

    Q: What was the total

    sales for the weekend of

    May 20-

    22 2008?

    Only the 3relevant

    partitions are

    accessed

    Partition Pruning

  • 7/29/2019 Oracle Explain Plan

    35/52

    Partition Pruning

    If you see the word KEY listed it means the partitions touched will be decidedat Run Time

    Pstart and Pstop list the partitiontouched by the query

  • 7/29/2019 Oracle Explain Plan

    36/52

    Bloom Filter

    DFO

    Hash Join

    Filter Create

    Receive

    DFO

    Send

    DFO

    Send

    Scan Time

    Receive

    Filter Use

    Scan Sales

    1.Table scan:Time table isscanned and sent

    Shared Bloom filter

    Set

    2.Bloom Filter create:Consumer set creates a

    hash table and a BITVECTOR. Bit vectorsets a bit for each rowthat matches the searchconditions

    4.Bloom Filter send:BIT VECTOR is sent as

    an additional filtercriteria to the scan of thesales table

    3.Table Scan:Sales table is scanand rows are filteredbased on querypredicates

    6.Reduced row sent:Only rows that have amatch in the bit vectorget sent to theconsumers

    Test

    5.Bloom Filter apply:Join column is hashedand compared to BITVECTOR

    7.Hash Join:Consumers will complete the hash

    join by probing into the hash tablefrom the time time to find actualmatching rows

  • 7/29/2019 Oracle Explain Plan

    37/52

    Parallelism

    Goal is to execute all aspects of the plan in parallel Identify if one or more sets of parallel server processes are used

    Producers and Consumers

    Identify if any part of the plan is running serial

  • 7/29/2019 Oracle Explain Plan

    38/52

    Parallelism

    If you see any lines beginning with the letter S you are running Serial checkDOP for each table & index used

    IN-OUT column shows whichstep is run in parallel and if it is asingle parallel server set or not

  • 7/29/2019 Oracle Explain Plan

    39/52

    Identifying Granules of Parallelism

    during scans in the plan

    Data is Partitioned into Granules either block range

    Partition

    Each parallel server is allocated one or more granules

    The granule method is specified on line above thescan in the operation section

  • 7/29/2019 Oracle Explain Plan

    40/52

    Identifying Granules of Parallelism

    during scans in the plan

  • 7/29/2019 Oracle Explain Plan

    41/52

    Access Paths and how they are

    parallelized

    Access Paths Parallelization method

    Full table scan Block Iterator

    Table accessed by Rowid Partition

    Index unique scan PartitionIndex range scan (descending) Partition

    Index skip scan Partition

    Full index scan Partition

    Fast full index scan Block IteratorBitmap indexes (in Star Transformation) Block Iterator

  • 7/29/2019 Oracle Explain Plan

    42/52

    Parallel Distribution

    Necessary when producers & consumers sets are used

    Producers must pass or distribute their data intoconsumers

    Operator into which the rows flow decides the distribution

    Distribution can be local or across other nodes in RAC

    Five common types of redistribution

  • 7/29/2019 Oracle Explain Plan

    43/52

    Parallel Distribution

    HASH Assumes one of the tables is hash partitioned

    Hash function applied to value of the join column

    Distribute to the consumer working on the corresponding hash partition

    Broadcast The size of one of the result sets is small

    Sends a copy of the data to all consumers

    Range Typically used for parallel sort operations Individual parallel servers work on data ranges

    QC doesnt have to sort just present the parallel server results in the correct order

    Partitioning Key Distribution PART (KEY) Assumes that the target table is partitioned

    Partitions of the target tables are mapped to the parallel servers Producers will map each scanned row to a consumer based on the partitioning column

    Round Robin Randomly but evenly distributes the data among the consumers

  • 7/29/2019 Oracle Explain Plan

    44/52

    Parallel Distribution

    Shows how the PQ serversdistribute rows betweeneach other

  • 7/29/2019 Oracle Explain Plan

    45/52

    Example of reading a

    plan

  • 7/29/2019 Oracle Explain Plan

    46/52

    Example SQL Statement and Block

    DiagramSELECT '(' || pcode || ')' || pcode_desc AS PRODUCT, CNT

    FROM (SELECT a.pcode, b.pcode_desc, count(a.pcode) CNT

    FROM BMG.t_acct_master_hd a

    ,BMG.hogan_pcode_hd_ref b

    ,BMG.t_tran_detail_hd c

    WHERE a.pcode = b.pcode

    AND a.acct_num=c.acct_numAND a.co_id=c.co_id

    AND c.asof_yyyymm=200102

    AND c.tran_amt

  • 7/29/2019 Oracle Explain Plan

    47/52

    Example Cont d Execution plan

    2. Are the cardinalityestimates correct?

    1. Check the rows returned isapprox correct

    Means no stats gatheredstrong indicate this wontbe best possible plan

    3.Are the access method correct?

    Example Contd Execution plan

  • 7/29/2019 Oracle Explain Plan

    48/52

    Example Cont d Execution plan

    4. Has partitionpruning happen?

    5. Are the correct join methods used?

    1

    2

    3

    6. Is the join order correct? Is the table thateliminates the most rows accessed first?

    Example Contd Execution plan

  • 7/29/2019 Oracle Explain Plan

    49/52

    Example Cont d Execution plan

    7. Check all aspects of the plan are executing inparallel

    8. Check the distribution method and make sure weare not broadcasting a large table?

    Example Contd Execution plan Solution

  • 7/29/2019 Oracle Explain Plan

    50/52

    Example Cont d Execution plan - Solution

    4. Partitionpruning Onerangepartition 4hashpartitions

    8. Rowdistributionis now allhash

    2. Cardinalitiesare correct andwith each joinnumber of rowsreduced

    1. Only 1 row is actually returnedand the cost is 4 X lower now

    5. Join types are stillhash joins but now aPWJ

    1

    2

    3

    6. The join order has changed - PWJthem join hash to look-up table

    3. Access methodsremains the same

    7. All aspectsof the plan areexecuting inparallel

    D t i i if t th i ht l

  • 7/29/2019 Oracle Explain Plan

    51/52

    Determining if you get the right plan

    Query

    SELECT quantity_soldFROM sales s, customers cWHERE s.cust_id =c.cust_id;ID

    What do you expect the plan to look like for this statement?S NOT NULL)

    Explanation

    Join to customers is redundant as no columns are selected

    Presence of primaryforeign key relationship means we can remove table

    Q & A

  • 7/29/2019 Oracle Explain Plan

    52/52

    Q & A