understanding explain plans dan hot ka

Upload: debashis-mallick

Post on 04-Jun-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    1/49

    www.DanHotka.com

    Dan HotkaAuthor Instructor

    Oracle ACE Director

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    2/49

    www.DanHotka.com

    www.DanHotka.com, LLC

    (c) www.danhotka.com LLC.

    Any reproduction or copying of this manual without the express written consent of www.danhotka.com LLC is expressly prohibited.

    Limitation on Warranty. THERE ARE NO WARRANTIES, EXPRESS OR IMPLIED, WITH RESPECT THERETO, INCLUDING, WITHOUT

    LIMITATION, ANY WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. PURCHASER SHALL BE

    SOLELY RESPONSIBLE FOR THE SELECTION, USE, EFFICIENCY AND SUITABILITY OF USE OF INFORMATION CONTAINED

    HEREIN TO ANY PARTICULAR APPLICATION OR PROBLEM. WWW.DANHOTKA.COM LLC SHALL HAVE NO LIABILITY

    THEREFOR.

    Limitation of Liability. IN NO EVENT SHALL WWW.DANHOTKA.COM LLC BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING,WITHOUT LIMITATION, ANY DAMAGES RELATINGTO LOSS OF DATA, AND ANY INDIRECT, SPECIAL OR CONSEQUENTIAL

    DAMAGES OR LOST PROFITS, ARISING OUT OF OR IN ANY WAY RELATED TO YOUR USE OF THE INFORMATION CONTAINED

    IN THIS MANUAL. IN THE EVENT THAT THE FORGOING IS HELD UNINFORCABLE THE PARTIES AGREE THAT

    WWW.DANHOTKA.COM LLC'S LIABILITY TO YOU HEREUNDER, IF ANY, SHALL IN NO EVENT EXCEED THE FEE PAID BY THE

    INJURED PARTY FOR THE MANUAL TO WWW.DANHOTKA.COM. LLC.

    Dan Hotka

    Author/Instructor/Oracle Expert

    CEO

    [email protected]

    515 279 3361

    Last Updated: 4/2006

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    3/49

    www.DanHotka.com

    Dan is a Training

    Consultant Dan Hotka

    Oracle Authored Expert

    31 Years in IT 26 years working with Oracle

    11 books hundreds of articles

    www.DanHotka.com

    Training at your Desk! Web-based training

    Morning lectures with hands-on lab exercises Almost like being there!

    Flat Fee Training1 Course Fee Price forup to 15 Attendees!

    Price includes my portable computer lab!

    On-site or over the Web!

    Training Courses Include: Oracle Advanced PL/SQL

    Oracle SQL Tuning

    TOAD and Unix/Linux

    Intro to Oracle, Intro to PL/SQL Oracle Discoverer/Oracle

    Analytics

    Register for my quarterlyNewsletter Full of tips and techniques,

    downloads, book reviews, etc

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    4/49www.DanHotka.com

    Training at your Desk!

    Web-based Training at your Desk!

    Feb 22 25 Intro to PL/SQL

    March 7 - 11 SQL Performance Tuning

    April 4 - 8 Advanced PL/SQL April 18 21 Intro to PL/SQL

    May 2 - 6 SQL Performance Tuning

    June 6 - 10 Advanced PL/SQL

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    5/49www.DanHotka.com

    Additional Reading

    Oracle SQL Tuning:

    A Close Look at

    Explain Plans

    By Dan Hotka; www.Amazon.com

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    6/49www.DanHotka.com

    New Book

    ISBN:978-0321649102

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    7/49www.DanHotka.com

    Introductions

    Please tell me a little about your background Show of Hands:

    DBAs

    Developers

    Working with: Oracle11, 10, 9, 8, 7 prior to 7?

    Years of Oracle experience < 1, 1-3, 4-8, 8+

    No tuning experience

    Some tuning experience

    Extensive tuning experience

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    8/49www.DanHotka.com

    Agenda

    Explain Plan Tools

    Xplan.display

    JS Tuner

    Understanding Explain Plans Reading the Explain Plan

    What does it mean

    Parallel Explain Plan Review

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    9/49www.DanHotka.com

    Additional Information

    Ask me for:

    White Papers

    Free Tools

    Scripts [email protected]

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    10/49www.DanHotka.com

    Explain Plan Tools

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    11/49www.DanHotka.com

    Explain Plan Tools

    DBMS_XPLAN.DISPLAY

    Available with Oracle 8.1.7+

    Comes with Database

    Used via SQL*Plus

    JS Tuner

    Available for Oracle8+ Download from www.DanHotka.com

    Executable Jar File

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    12/49

    www.DanHotka.com

    Explain Plan Tools

    PLAN_TABLE

    Use /rdbms/admin/utlxplan.sql for current

    plan_table

    Use the explain plan for syntax to populate this table Use tools to populate this table (TOAD, SQL Developer)

    Use SHOW_PLAN.sql to display contents

    Available on my website

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    13/49

    www.DanHotka.com

    Using the Tools

    All tools use the

    PLAN_TABLE /rdbms/admin

    /utlxplan.sql

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    14/49

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    15/49

    www.DanHotka.com

    Explain Plan Tools

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    16/49

    www.DanHotka.com

    Explain Plan Tools

    JSTuner

    Incorporates SQL Scripts with enhanced Trace

    Index Info

    Includes index statistics Enhanced Explain Plan

    Works with V8+

    Works with rule-based optimizer!

    Available from www.DanHotka.com

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    17/49

    www.DanHotka.com

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    18/49

    www.DanHotka.com

    Understanding theExplain Plan

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    19/49

    www.DanHotka.com

    Reading Explain Plans

    Oracle:

    Parses the SQL Checks SQL syntax

    Checks for available indexes/stats

    Reads from bottom to the top

    Arrives at an Execution Plan

    Decides how it will access the tables and indexes

    Executes the SQL

    Oracle9/10 peaks once at bind variables Oracle11 tracks explain plans with various bind vars

    Called Bind Aware

    Runs the Execution plan

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    20/49

    www.DanHotka.com

    Reading Explain Plans

    Explain Plan

    Visualizes the execution plan

    Uses the Plan_Table

    Which varies slightly from release to release Utlxplan.sql

    /RDBMS/ADMIN

    Used heavily to tune SQL

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    21/49

    www.DanHotka.com

    Reading Explain Plans

    How does it work?

    Reads from bottom up Syntax checks/tracks available indexes

    Regular queries - generally does the table joins first

    Utilizes Where clause predicates

    Parallel queries generally does partition pruning first

    Sometimes accesses tables again Oracle does rewrites

    Views to joins, sub queries to joins, sub queries to inline

    views, etc

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    22/49

    www.DanHotka.com

    Reading Explain Plans

    How does it work?

    RBO - follows rules for index selection Arrives at an execution plan in single pass

    CBO - tries a variety of combinations

    Combinations called permutations Generally # perms = # tables * # where predicates

    Regular queries: Uses lowest cost method

    *** Queries in this course are regular queries unless otherwisenoted

    Parallel queries (covered in my class)

    Uses fastest access method

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    23/49

    www.DanHotka.com

    Reading Explain Plans

    CBO Permutations

    9i+ Query Transformations Complex View Merging

    Converts views to joins

    Subquery Unesting

    Converts subqueries to inline views

    Join Predicate Push Down

    Moves where clause predicates into subquery

    - Hints for each item above- No_Query_Transformation prevents this behavior

    - Discussed later in this chapter

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    24/49

    www.DanHotka.com

    Single Table SQL

    Single Table SQL

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    25/49

    www.DanHotka.com

    Single Table SQL

    Single Table SQL Answers

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    26/49

    www.DanHotka.com

    Multiple Table SQL

    Multiple Table SQL

    Multiple Table SQL

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    27/49

    www.DanHotka.com

    Multiple Table SQL

    Answers

    Multiple Table SQL Answers

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    28/49

    www.DanHotka.com

    Reading Explain Plans

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    29/49

    www.DanHotka.com

    Reading Explain Plans

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    30/49

    www.DanHotka.com

    Reading Explain Plans

    Access Rule Description

    AND-EQUAL Index values will be used to join rows.

    CONCATENATION SQL statement UNION command. FILTER FILTERs apply other criteria in the query to further qualify the

    matching rows. The other criteria include correlated sub queries, and HAVINGclause.

    TABLE ACCESS When not associated with a join condition, they act like

    Filterprocessing additional Where Clause predicates. VIEW OF Processed SQL from a view.

    FOR UPDATE SQL statement clause for update of placed row level locks onaffected rows.

    INDEX (UNIQUE) SQL statement utilized a unique index to search for aspecific value.

    INDEX (RANGE SCAN) SQL statement contains a non-equality or BETWEENcondition.

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    31/49

    www.DanHotka.com

    Reading Explain Plans

    Access Rule Description

    INLIST ITERATOR SQL statement has an IN clause, or, values being treated as

    an IN clause HASH JOIN SQL statement initiated a hash-join operation.

    MERGE JOIN SQL statement references two or more tables, sorting the tworesult sets being joined over the join columns and then merging the results viathe join columns.

    MERGE JOIN (CARTESIAN) SQL statement references two or more tablesbut without a joining column (generally not a good thing)

    NESTED LOOPS This operation is one form of joining tables, as opposed toa merge join. One row is retrieved from the row source identified by the first

    child operation, and then joined to all matching rows in the other table,identified in the second child operation.

    NONUNIQUE INDEX (RANGE SCAN) The RANGE SCAN optionindicates that ORACLE expects to return multiple matches (ROWIDs) fromthe index search

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    32/49

    www.DanHotka.com

    Reading Explain Plans

    Access Rule Description

    BITMAP CONVERSION Bitmap Index being merged

    BITMAP MERGE Generally used with Bitmap Range Scan

    BITMAP MINUS Bitmap Index handling a not = condition.

    BITMAP INDEX SINGLE VALUE Bitmap index being used for

    single value lookup. BITMAP INDEX (RANGE SCAN) Bitmap index being used for

    multiple value lookup.

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    33/49

    www.DanHotka.com

    Reading Explain Plans

    Access Rule Description

    PARTITION (CONCATTENATED) SQL statement will access a

    partitioned object and merge the retrieved rows from the accessed partitions. PARTITION (SINGLE) SQL statement will access a single partition.

    PARTITION (EMPTY) The SQL statement makes reference to an emptypartition.

    SORT (ORDER BY) SQL statement contains an ORDER BY SORT(AGGREGATE) SQL statement initiated a sort to resolve a MIN or MAXtype function.

    SORT (GROUP BY) SQL statement contains a GROUP BY

    TABLE ACCESS (FULL) All rows are retrieved from the table withoutusing an index.

    TABLE ACCESS (BY ROWID) A row was retrieved from a table based onthe ROWID of the row.

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    34/49

    www.DanHotka.com

    Reading Explain Plans

    Index Scans Unique Scan

    via root -> branch -> leaf for a single row access Range Scan

    via root -> branch -> leaf for first row, then leafto leaf for remaining rows

    Full Scan (Index-Full)

    Scans leaf blocks using single-block access

    Fast Full Scan (Index-FFS)

    scans all index leaf blocks using multi-blockread

    Index Skip Scan (Index-SS)

    Useful for multi-column indexes, accessing only

    2nd column and first column has low cardinality

    R di E l i Pl

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    35/49

    www.DanHotka.com

    Reading Explain Plans

    More Information:

    Database Performance Tuning Guide & Reference

    Chapter 9 Using Explain Plans

    Oracle Documentation

    Download from www.Oracle.com search on Documentation

    R di E l i Pl

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    36/49

    www.DanHotka.com

    Reading Explain Plans

    Oracle only joins 2 tables at a time

    The smaller the initial result sets, the faster thewhole query runs

    Drive off the item that will eliminate the most rows first

    R di E l i Pl

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    37/49

    www.DanHotka.com

    Reading Explain Plans

    Nested Loops

    Inner table looped for each rowreturned in outer table

    Lg table should be outer

    Sm table (or unique indexed

    lookup) should be inner

    Rows returned to the result set

    that qualify the driving WHERE

    clause

    Cost = outer access + (inner

    table access * outer

    cardinality)

    Merge Scan Join

    Both tables are sorted

    Rows are inserted into result setbased on key value

    THEN WHERE clause applied

    Cost = outer access + inner

    access + sort costs Hash Join

    Hashes join keys and cachesobject into a hash table

    Driving table should be smaller ofthe 2

    Cost = inner cost + (outer cost *inner cardinality/hash partitions)

    Reading Explain Plans

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    38/49

    www.DanHotka.com

    Reading Explain Plans

    Nested Loop Join

    driving table

    Default order(rule)

    Merge Scan Join

    sort & match

    Hash Join

    Full scans with no

    sorts

    Join column to rowaddress

    R di E l i Pl

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    39/49

    www.DanHotka.com

    Reading Explain Plans

    Nested Loop Join

    If join condition is ANDed, make a compound index on the inner table

    Inner and Outer join column should have same data type

    Outer Table: Larger of Result set

    Inner Table: Smaller of Result set

    Foreign key indexes helps CBO choose between nested loops and hash joins

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    40/49

    Join Formula

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    41/49

    www.DanHotka.com

    Join Formula

    RBO Join Formula

    Indexes, type of indexes, Full Table Scans

    CBO Join Formula High # values matching High # values

    Tends to choose Merge Join

    High # values matching Low # values

    Tends to choose Nested Loop Low # values matching Low # values

    Tends to choose Hash Join

    The Oracle Optimizers

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    42/49

    www.DanHotka.com

    Oracle Optimizer gives you choices:

    Rule-based

    Based on a set of rules (Index existence, SQL coding)

    Does NOT consider object statistics

    Cost-based

    Uses object statistics (from DBMS_STATS command)

    User has more control in tuning

    2 goals:

    First row: response time for interactive apps (OLTP)

    All rows: throughput for batch processing (DSS)

    Makes Assumptions

    Hints

    Cost Based directives, always used unless Oracle cant use them

    Can be specified in combinations

    The Oracle Optimizers

    The Oracle OptimizersC t b d O ti i

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    43/49

    www.DanHotka.com

    Cost-based Optimizer

    Cost based Optimizer

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    44/49

    www.DanHotka.com

    Cost-based Optimizer

    Phys IO + Logical IO/1000 + NetIO*1.5

    Cost Factor is an estimated number of physical I/O operations Oracle thinks it

    will have to do, based on statistics. Assumes even data distribution when using

    indexes (unless histograms are implemented). Costs out each join to select driving

    table

    Cost Factor Heavily relies upon assumed multiblock read-ahead

    8i Cost reflects number of multiblock reads based on statistics

    9i Cost reflects machine speed at performing multiblock reads along with 8i10g Takes into account high water mark,

    10g index_fast_full_scans considers number of leaf blocks

    Cost based Optimizer

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    45/49

    www.DanHotka.com

    Cost-based Optimizer

    CBO Formulas

    Mra Multi block read ahead Selectivity discussed with Where Clause section

    Clustering Factor discussed with Index section

    Full Table Scan = num rows / mra Fast Full Scan = num rows / mra

    Unique Scan = #branch levels + 1

    Range Scan = (#branch levels + selectivity) * (#leaf blocks +selectivity) * Clustering Factor

    Index-Only Scan = (#branch levels + selectivity) * #leaf blocks

    Source: Fallacies of the CBO: Wulfgang Brietling

    The Oracle Optimizers

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    46/49

    www.DanHotka.com

    Cost-based Optimizer

    Oracle Documentation

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    47/49

    www.DanHotka.com

    Oracle Documentation

    Oracle Documentation

    Complete database doc set

    www.Oracle.com

    Search on Documentation

    380,000+ mbytes zip file

    www.oracle.com/technology/documentation/database.html

    Has doc sets back to Oracle9r2

    View Oracle Documentation

    SQL Tuning Class

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    48/49

    www.DanHotka.com

    SQL Tuning Class

    Hands-on Labs

    Instructor-led courses

    Understanding Explain Plans

    Index Review

    How they are used/not used

    Controlling the Explain Plan

    Using the Tools

    Oracle Trace

    StatsPack AWR

    PL/SQL Tuning/Profiling

    SGA Topics

    Cursor management Multiple buffer caches

    Result Cache

    Finding the SQL problems

    SQL Tuning Tips and Techniques

    Training at your Desk! (sm)

    Onsite

    What have we learned?

  • 8/13/2019 Understanding Explain Plans Dan Hot Ka

    49/49

    www.DanHotka.com

    What have we learned?

    Explain Plan Tools

    Xplan.display

    JS Tuner

    Understanding Explain Plans Thank you! Keep in touch