SlideShare a Scribd company logo
1 of 49
Download to read offline
1
<Insert Picture Here>




Explaining the Explain Plan:
Interpreting Execution Plans for SQL Statements

Maria Colgan
Senior Principal Product Manager
Agenda


•  What is an execution plan               <Insert Picture Here>

•  How to generate a plan
•  What is a good plan for the optimizer
•  Understanding execution plans
  –  Cardinality
  –  Access paths
  –  Join type
  –  Join order
•  Execution plan examples




                                                              3
What is an Execution plan?


•  Execution plans show the detailed steps necessary to
   execute a SQL statement
•  These steps are expressed as a set of database
   operators that consumes and produces rows
•  The order of the operators and their implementation is
   decided by the optimizer using a combination of query
   transformations and physical optimization techniques
•  The display is commonly shown in a tabular format,
   but a plan is in fact tree-shaped




                                                            4
What is an Execution plan?


Query:

SELECT prod_category, avg(amount_sold)
FROM sales s, products p
WHERE p.prod_id = s.prod_id
GROUP BY prod_category;

Tabular representation of plan                                Tree-shaped representation of plan
-----------------------------------------------------------
Id Operation                                Name                          GROUP BY
-----------------------------------------------------------
0 SELECT STATEMENT
1 HASH GROUP BY                                                           HASH JOIN
2 HASH JOIN
3 TABLE ACCESS FULL                       PRODUCTS
4 PARTITION RANGE ALL
5      TABLE ACCESS FULL SALES                                 TABLE ACCESS      TABLE ACCESS
----------------------------------------------------------
      PRODUCTS           SALES



                                                                                                   5
Agenda


•  What is an execution plan               <Insert Picture Here>

•  How to generate a plan
•  What is a good plan for the optimizer
•  Understanding execution plans
  –  Cardinality
  –  Access paths
  –  Join type
  –  Join order
•  Execution plan examples




                                                              6
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 in
     the cursor cache

Either way use DBMS_XPLAN package to display plans

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

                                                                          7
How to get an Execution Plan Example 1


•  EXPLAIN PLAN command & dbms_xplan.display
   function
SQL> EXPLAIN PLAN FOR SELECT prod_name, avg(amount_sold)
      FROM sales s, products p
      WHERE p.prod_id = s.prod_id
      GROUP BY prod_name;
SQL> SELECT plan_table_output
       FROM table(dbms_xplan.display('plan_table',null,'basic'));

      ------------------------------------------









      Id Operation                Name
      ------------------------------------------
      0 SELECT STATEMENT
      1 HASH GROUP BY
      2   HASH JOIN
      3    TABLE ACCESS FULL      PRODUCTS
      4    PARTITION RANGE ALL
      5     TABLE ACCESS FULL     SALES
      -------------------------------------------
                                                                     8
How to get an Execution Plan Example 2


•           Generate & display execution plan for last SQL
          stmts executed in a session
          SQL>SELECT prod_category, avg(amount_sold)
              FROM sales s, products p
              WHERE p.prod_id = s.prod_id
              GROUP BY prod_category;

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

      ------------------------------------------









      Id Operation                Name
      ------------------------------------------
      0 SELECT STATEMENT
      1 HASH GROUP BY
      2   HASH JOIN
      3    TABLE ACCESS FULL      PRODUCTS
      4    PARTITION RANGE ALL
      5     TABLE ACCESS FULL     SALES
      -------------------------------------------

                                                                       9
Agenda


•  What is an execution plan               <Insert Picture Here>

•  How to generate a plan
•  What is a good plan for the optimizer
•  Understanding execution plans
  –  Cardinality
  –  Access paths
  –  Join type
  –  Join order
•  Execution plan examples




                                                             10
What’s a Good Plan for the Optimizer?


The Optimizer has two different goals
•  Serial execution: It’s all about cost
  –  The cheaper, the better
•  Parallel execution: it’s all about performance
  –  The faster, the better

Two fundamental questions:
•  What is cost?
•  What is performance?




                                                    11
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 & IO as units of work
  •  Cost is an estimate of the amount of CPU and the number of disk
     I/Os, used to perform an operation


    Cost is an internal Oracle measurement

                                                                       12
What is performance?


•  Getting as many queries completed as possible?
•  Getting fastest possible elapsed time using the fewest
   resources?
•  Getting the best concurrency rate?

Actual Definition
•  Performance is fastest possible response time for a query
  •  Goal is to complete the query as quickly as possible
  •  Optimizer does not focus on resources needed to execute the plan




                                                                        13
Agenda


•  What is an execution plan               <Insert Picture Here>

•  How to generate a plan
•  What is a good plan for the optimizer
•  Understanding execution plans
  –  Cardinality
  –  Access paths
  –  Join type
  –  Join order
•  Execution plan examples




                                                             14
Cardinality


What is it?
•  Estimate of number rows that will be returned by each operation
How does the Optimizer Determine it?
•  Cardinality for a single column equality predicate = total num of rows
                                                      num of distinct values
    –  For example: A table has 100 rows, a column has 10 distinct values
                    => cardinality=10 rows
•  More complicated predicates have more complicated cardinality
   calculation
Why should you care?
•  Influences everything! Access method, Join type, Join Order etc




                                                                           15
How to Identify Cardinality in an Execution Plan




                                                   Cardinality the estimated
                                                   # of rows returned




Determine correct cardinality using a SELECT COUNT(*) from each table applying
any WHERE Clause predicates belonging to that table

                                                                            16
Check Cardinality Estimates

SELECT /*+ gather_plan_statistics */ p.prod_name, SUM(s.quantity_sold)
FROM      sales s, products p
WHERE     s.prod_id =p.prod_id GROUP By p.prod_name ;

SELECT * FROM table (
 DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'ALLSTATS LAST'));
               Compare the estimated number of rows returned for each operation in the
                                    plan to actual rows returned




                                                                                         17
Check Cardinality using SQL Monitor




SQL Monitor is the easiest way to compare the estimated number of rows returned for each
operation in a parallel plan to actual rows returned                                       18
Solutions to incorrect Cardinality estimates


Cause                               Solution

Stale or missing statistics         DBMS_STATS

Data Skew                           Create a histogram

Multiple single column predicates   Create a column group using
on a table                          DBMS_STATS.CREATE_EXTENDED_STATS
Function wrapped column             Create statistics on the funct wrapped
                                    column using
                                    DBMS_STATS.CREATE_EXTENDED_STATS
Multiple columns used in a join     Create a column group on join columns using
                                    DBMS_STATS.CREATE_EXTENDED_STAT
Complicated expression              Use dynamic sampling level 4 or higher
containing columns from multiple
tables


                                                                                  19
Agenda


•  What is an execution plan               <Insert Picture Here>

•  How to generate a plan
•  What is a good plan for the optimizer
•  Understanding execution plans
  –  Cardinality
  –  Access paths
  –  Join type
  –  Join order
•  Execution plan examples




                                                             20
Access Paths – Getting the data
Access Path                Explanation
                           Reads all rows from table & filters out those that do not meet the where clause
   Full table scan         predicates. Used when no index, DOP set etc
                           Rowid specifies the datafile & data block containing the row and the location of
   Table access by Rowid   the row in that block. Used if rowid supplied by index or in where clause
                           Only one row will be returned. Used when stmt contains a UNIQUE or a
   Index unique scan       PRIMARY KEY constraint that guarantees that only a single row is accessed
                           Accesses adjacent index entries returns ROWID values Used with equality on
   Index range scan        non-unique indexes or range predicate on unique index (<.>, between etc)
                           Skips the leading edge of the index & uses the rest Advantageous if there are
   Index skip scan         few distinct values in the leading column and many distinct values in the non-
                           leading column
                           Processes all leaf blocks of an index, but only enough branch blocks to find 1st
   Full index scan         leaf block. Used when all necessary columns are in index & order by clause
                           matches index struct or if sort merge join is done
                           Scans all blocks in index used to replace a FTS when all necessary columns
   Fast full index scan    are in the index. Using multi-block IO & can going parallel
                           Hash join of several indexes that together contain all the table columns that are
   Index joins             referenced in the query. Wont eliminate a sort operation
                           uses a bitmap for key values and a mapping function that converts each bit
   Bitmap indexes          position to a rowid. Can efficiently merge indexes that correspond to several
                           conditions in a WHERE clause




                                                                                                           21
How to Identify Access Path in an Execution Plan




                                          Look in Operation section to
                                          see how an object is being
                                          accessed




If the wrong access method is being used check cardinality, join order…

                                                                          22
Access Path examples


A table countries contains 10K rows & has a primary key on
country_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';




                                                             23
Access Path examples


 A table countries contains 10K rows & has a primary key on
 country_id – What plan would you expect for these queries?

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




                                                              24
Common Access Path issues



Issue                                Cause

Uses a table scan instead of index   DOP on table but not index, value of MBRC

Picks wrong index                    Stale or missing statistics
                                     Cost of full index access is cheaper than
                                     index look up followed by table access
                                     Picks index that matches most # of column




                                                                                 25
Agenda


•  What is an execution plan               <Insert Picture Here>

•  How to generate a plan
•  What is a good plan for the optimizer
•  Understanding execution plans
  –  Cardinality
  –  Access paths
  –  Join type
  –  Join order
•  Execution plan examples




                                                             26
Join Types


Join Type                Explanation
                         For every row in the outer table, Oracle accesses all the rows in the
   Nested Loops joins       inner table Useful when joining small subsets of data and there is
                            an efficient way to access the second table (index look up)
                        The smaller of two tables is scan and resulting rows are used to build
   Hash Joins              a hash table on the join key in memory. The larger table is then
                           scan, join column of the resulting rows are hashed and the values
                           used to probing the hash table to find the matching rows. Useful
                           for larger tables & if equality pred
                         Consists of two steps:
   Sort Merge joins      1.  Both the inputs are sorted on the join key.
                         2.  The sorted lists are merged together.
                         Useful when the join condition between two tables is an inequality
                             condition or one of the table is already ordered e.g. index access
                         Joins every row from one data source with every row from the other
   Cartesian Joins          data source, creating the Cartesian Product of the two sets. Only
                            good if tables are very small. Only choice if there is no join
                            condition specified in query
                         Returns all rows that satisfy the join condition and also returns all of
   Outer Joins              the rows from the table without the (+) for which no rows from the
                            other table satisfy the join condition




                                                                                                    27
How to Identify Join Type in an Execution Plan




                                                Look in the Operation section
                                                to check the right join type is
                                                used




If wrong join type is used check stmt is written correctly & cardinality estimates

                                                                                  28
Join Type Example 1
What Join type should be use for this Query?
  SELECT e.last_name, e.salary, d.department_name
  FROM hr.employees e, hr.departments d
  WHERE d.departments_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




                                                                        29
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




                                                    30
Join Type Example 3


What Join type should be use for this Query?
  SELECT o.order_id,0.order_date,e.name
  FROM oe.orders o , hr.employees e;
Orders has 105 rows
Employees has 107 rows




                                               31
Join Type Example 3
   Cartesian product not always bad

  •  Chosen when the number of rows being joined is low
  •  Commonly used to join multiple small dimensions to
     one large fact table



           s.prod_id=p.prod_id           s.cust_id=c.cust_id
PRODUCTS                         SALES                         CUSTOMERS

                                    s.promo_id=c.promo_id


                                 PROMOTIONS



                                                                           32
Join Type Example 3
 Cartesian product

•  By joining the three small dimension tables together
   first we minimize the number of rows being carried
   through the joins




                                                          33
Join Type Example 4
What Join type should be use for this Query?
  SELECT d.department_id,e.emp_id
  FROM hr.employees e FULL OUTER JOIN hr.departments d
    ON e.department_id = d.department_id
  ORDER BY d.department_id;
Employees has 107 rows
Departments has 27 rows
Foreign key relationship between Employees and Departments on dept_id




                                                                    34
What causes the wrong Join Type to be selected


Issue                   Cause


   Nested loop             Cardinality estimate on the left side is under
   selected instead        estimated triggers Nested loop to be selected
   of hash join
   Hash join selected      In case of a hash join the Optimizer doesn’t taken
   instead of nested       into consideration the benefit of caching. Rows on
   loop                    the left come in a clustered fashion or (ordered)
                           so the probe into the right is less expensive
   Cartesian Joins         Cardinality underestimation




                                                                                35
Agenda


•  What is an execution plan               <Insert Picture Here>

•  How to generate a plan
•  What is a good plan for the optimizer
•  Understanding execution plans
  –  Cardinality
  –  Access paths
  –  Join type
  –  Join order
•  Execution plan examples




                                                             36
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 affected by the access paths available
Some basic rules
•  Joins guaranteed to produce at most one row always go first
  –  Joins between two row sources that have only one row each
•  When outer joins are used the table with the outer join
   operator must come after the other table in the predicate
•  If view merging is not possible all tables in the view will be
   joined before joining to the tables outside the view


                                                                    37
How to Identify Join order in an Execution Plan




                    1
                    2
                   3
               4

           5

                                                 Want to start with the table that
                                                 reduce the result set the most




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

                                                                                 38
Finding the Join Order for complex SQL statement

•  It can be hard to determine Join Order for Complex SQL
   statements but it is easily visible in the outline data of plan
  SELECT *
  FROM
  table(dbms_xplan.display_cursor(FORMAT=>’TYPICAL +outline’);




                                   The leading hint tells
                                   you the join order




                                                                 39
What causes the wrong Join Order



      Causes


         Incorrect single table cardinality estimates

         Incorrect join cardinality estimates




                                                        40
Agenda


•  What is an execution plan               <Insert Picture Here>

•  How to generate a plan
•  What is a good plan for the optimizer
•  Understanding execution plans
  –  Cardinality
  –  Access paths
  –  Join type
  –  Join order
•  Execution plan examples




                                                             41
Example SQL Statement

•  Find all the employees who make as much or more than
   their manager
SELECT e1.last_name, e1.job_title, e1.total_comp
FROM     (SELECT e.manager_id, e.last_name, j.job_title,
                 (e.salary+e.commission_pct) total_comp
           FROM employees e, jobs j, departments d
           WHERE d.department_name = 'Sales'
           AND    e.department_id     = d.department_id
           AND    e.job_id            = j.job_id ) e1,
          (SELECT e.employee_id, (e.salary+e.commission_pct) tc
           FROM employees e, departments d
           WHERE d.department_name = ‘Sales'
           AND   e.department_id      = d.department_id ) e2
WHERE e1.manager_id = e2.employee_id
AND   e1.total_comp >= e2.tc;

                                                                  42
Is it a good Execution plan?              1. Is the estimated number of
                                          rows being returned
                                          accurate?




                                                               3.Are the access
                                                               method correct?



                               2. Are the cardinality
                               estimates accurate?



                                              Means no stats gathered
                                              strong indicator this won’t be
                                              best possible plan


                                                                                  43
Example Cont’d Execution plan




               1

               2
           3
       4


   5

                                4. Are the right join types being used?


                                 5. Is the join order correct? Is the table that
                                 eliminates the most rows accessed first?




                                                                                   44
What does the Plan Tree look like?

                                   NESTED LOOP




                             NESTED LOOP      INDEX UNIQUE SCAN -
                                              TABLE ACCESS JOBS



                                     INDEX UNIQUE SCAN -
                      HASH JOIN         TABLE ACCESS
                                         DEPARTMENT


         MERGE JOIN           TABLE ACCESS
         CARTESIAN             EMPLOYEES


TABLE ACCESS   TABLE ACCESS
 DEPARTMENT     EMPLOYEES


                                                                    45
Example Cont’d Execution plan - Solution
                                                       1. Only 1 row is actually returned and
                                                       the cost is 4 lower now




                                 4. Join types have
                                changed to be all NL


                          1
5. The join               2
order has
 changed
                      3

                      4

                  5



                                               3. Access methods
                                                have changed for    2. Cardinalities are correct
                                                   some tables      and with each join number of
                                                                    rows reduced




                                                                                                   46
What does the Plan Tree look like?
                                NESTED LOOP



                                              INDEX UNIQUE SCAN -
                         NESTED LOOP             TABLE ACCESS
                                                  DEPARTMENT



                NESTED LOOP          INDEX UNIQUE SCAN -
                                     TABLE ACCESS JOBS



                          INDEX RANGE SCAN -
       NESTED LOOP
                             TABLE ACCESS
                              EMPLOYEES

TABLE ACCESS    INDEX RANGE SCAN -
 DEPARTMENT        TABLE ACCESS
                    EMPLOYEES


                                                                    47
Q&A




      48
The preceding is intended to outline our general
product direction. It is intended for information
purposes only, and may not be incorporated into any
contract. It is not a commitment to deliver any
material, code, or functionality, and should not be
relied upon in making purchasing decisions.
The development, release, and timing of any
features or functionality described for Oracle’s
products remains at the sole discretion of Oracle.




                                                      49

More Related Content

What's hot

Overview of query evaluation
Overview of query evaluationOverview of query evaluation
Overview of query evaluationavniS
 
Part4 Influencing Execution Plans with Optimizer Hints
Part4 Influencing Execution Plans with Optimizer HintsPart4 Influencing Execution Plans with Optimizer Hints
Part4 Influencing Execution Plans with Optimizer HintsMaria Colgan
 
Query processing and optimization (updated)
Query processing and optimization (updated)Query processing and optimization (updated)
Query processing and optimization (updated)Ravinder Kamboj
 
How to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better PerformanceHow to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better Performanceoysteing
 
How to understand and analyze Apache Hive query execution plan for performanc...
How to understand and analyze Apache Hive query execution plan for performanc...How to understand and analyze Apache Hive query execution plan for performanc...
How to understand and analyze Apache Hive query execution plan for performanc...DataWorks Summit/Hadoop Summit
 
Managing Statistics for Optimal Query Performance
Managing Statistics for Optimal Query PerformanceManaging Statistics for Optimal Query Performance
Managing Statistics for Optimal Query PerformanceKaren Morton
 
Db2 performance tuning for dummies
Db2 performance tuning for dummiesDb2 performance tuning for dummies
Db2 performance tuning for dummiesAngel Dueñas Neyra
 
phoenix-on-calcite-nyc-meetup
phoenix-on-calcite-nyc-meetupphoenix-on-calcite-nyc-meetup
phoenix-on-calcite-nyc-meetupMaryann Xue
 
Identify SQL Tuning Opportunities
Identify SQL Tuning OpportunitiesIdentify SQL Tuning Opportunities
Identify SQL Tuning OpportunitiesCuneyt Goksu
 
Useful PL/SQL Supplied Packages
Useful PL/SQL Supplied PackagesUseful PL/SQL Supplied Packages
Useful PL/SQL Supplied PackagesMaria Colgan
 
DB2 LUW Access Plan Stability
DB2 LUW Access Plan StabilityDB2 LUW Access Plan Stability
DB2 LUW Access Plan Stabilitydmcmichael
 
Part1 of SQL Tuning Workshop - Understanding the Optimizer
Part1 of SQL Tuning Workshop - Understanding the OptimizerPart1 of SQL Tuning Workshop - Understanding the Optimizer
Part1 of SQL Tuning Workshop - Understanding the OptimizerMaria Colgan
 
Efficient spatial queries on vanilla databases
Efficient spatial queries on vanilla databasesEfficient spatial queries on vanilla databases
Efficient spatial queries on vanilla databasesJulian Hyde
 
Heuristic approch monika sanghani
Heuristic approch  monika sanghaniHeuristic approch  monika sanghani
Heuristic approch monika sanghaniMonika Sanghani
 
Cost-based Query Optimization in Apache Phoenix using Apache Calcite
Cost-based Query Optimization in Apache Phoenix using Apache CalciteCost-based Query Optimization in Apache Phoenix using Apache Calcite
Cost-based Query Optimization in Apache Phoenix using Apache CalciteJulian Hyde
 
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory optionStar Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory optionFranck Pachot
 

What's hot (19)

Chapter15
Chapter15Chapter15
Chapter15
 
Overview of query evaluation
Overview of query evaluationOverview of query evaluation
Overview of query evaluation
 
Part4 Influencing Execution Plans with Optimizer Hints
Part4 Influencing Execution Plans with Optimizer HintsPart4 Influencing Execution Plans with Optimizer Hints
Part4 Influencing Execution Plans with Optimizer Hints
 
Query processing and optimization (updated)
Query processing and optimization (updated)Query processing and optimization (updated)
Query processing and optimization (updated)
 
How to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better PerformanceHow to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better Performance
 
How to understand and analyze Apache Hive query execution plan for performanc...
How to understand and analyze Apache Hive query execution plan for performanc...How to understand and analyze Apache Hive query execution plan for performanc...
How to understand and analyze Apache Hive query execution plan for performanc...
 
Managing Statistics for Optimal Query Performance
Managing Statistics for Optimal Query PerformanceManaging Statistics for Optimal Query Performance
Managing Statistics for Optimal Query Performance
 
Db2 performance tuning for dummies
Db2 performance tuning for dummiesDb2 performance tuning for dummies
Db2 performance tuning for dummies
 
phoenix-on-calcite-nyc-meetup
phoenix-on-calcite-nyc-meetupphoenix-on-calcite-nyc-meetup
phoenix-on-calcite-nyc-meetup
 
Identify SQL Tuning Opportunities
Identify SQL Tuning OpportunitiesIdentify SQL Tuning Opportunities
Identify SQL Tuning Opportunities
 
Useful PL/SQL Supplied Packages
Useful PL/SQL Supplied PackagesUseful PL/SQL Supplied Packages
Useful PL/SQL Supplied Packages
 
Query compiler
Query compilerQuery compiler
Query compiler
 
DB2 LUW Access Plan Stability
DB2 LUW Access Plan StabilityDB2 LUW Access Plan Stability
DB2 LUW Access Plan Stability
 
Part1 of SQL Tuning Workshop - Understanding the Optimizer
Part1 of SQL Tuning Workshop - Understanding the OptimizerPart1 of SQL Tuning Workshop - Understanding the Optimizer
Part1 of SQL Tuning Workshop - Understanding the Optimizer
 
Efficient spatial queries on vanilla databases
Efficient spatial queries on vanilla databasesEfficient spatial queries on vanilla databases
Efficient spatial queries on vanilla databases
 
Heuristic approch monika sanghani
Heuristic approch  monika sanghaniHeuristic approch  monika sanghani
Heuristic approch monika sanghani
 
Cost-based Query Optimization in Apache Phoenix using Apache Calcite
Cost-based Query Optimization in Apache Phoenix using Apache CalciteCost-based Query Optimization in Apache Phoenix using Apache Calcite
Cost-based Query Optimization in Apache Phoenix using Apache Calcite
 
Chapter16
Chapter16Chapter16
Chapter16
 
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory optionStar Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
 

Viewers also liked

Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007paulguerin
 
Cooper Oracle 11g Overview
Cooper Oracle 11g OverviewCooper Oracle 11g Overview
Cooper Oracle 11g Overviewmoin_azeem
 
10x Performance Improvements
10x Performance Improvements10x Performance Improvements
10x Performance ImprovementsRonald Bradford
 
11. transaction sql
11. transaction sql11. transaction sql
11. transaction sqlUmang Gupta
 
In Sync11 Presentation The Biggest Loser
In Sync11 Presentation The Biggest LoserIn Sync11 Presentation The Biggest Loser
In Sync11 Presentation The Biggest Loserpaulguerin
 
Oracle Database 11g Security and Compliance Solutions - By Tom Kyte
Oracle Database 11g Security and Compliance Solutions - By Tom KyteOracle Database 11g Security and Compliance Solutions - By Tom Kyte
Oracle Database 11g Security and Compliance Solutions - By Tom KyteEdgar Alejandro Villegas
 
Oracle Database 11g vs 12c
Oracle Database 11g vs 12cOracle Database 11g vs 12c
Oracle Database 11g vs 12cDeiby Gómez
 
Oracle R12 EBS Performance Tuning
Oracle R12 EBS Performance TuningOracle R12 EBS Performance Tuning
Oracle R12 EBS Performance TuningScott Jenner
 
All of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL DeveloperAll of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL DeveloperJeff Smith
 
Oracle Database Overview
Oracle Database OverviewOracle Database Overview
Oracle Database Overviewhonglee71
 
Ten query tuning techniques every SQL Server programmer should know
Ten query tuning techniques every SQL Server programmer should knowTen query tuning techniques every SQL Server programmer should know
Ten query tuning techniques every SQL Server programmer should knowKevin Kline
 
Oracle architecture ppt
Oracle architecture pptOracle architecture ppt
Oracle architecture pptDeepak Shetty
 
Database administrator
Database administratorDatabase administrator
Database administratorTech_MX
 
Oracle RAC 12c Release 2 - Overview
Oracle RAC 12c Release 2 - OverviewOracle RAC 12c Release 2 - Overview
Oracle RAC 12c Release 2 - OverviewMarkus Michalewicz
 

Viewers also liked (17)

Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 
Cooper Oracle 11g Overview
Cooper Oracle 11g OverviewCooper Oracle 11g Overview
Cooper Oracle 11g Overview
 
10x Performance Improvements
10x Performance Improvements10x Performance Improvements
10x Performance Improvements
 
11. transaction sql
11. transaction sql11. transaction sql
11. transaction sql
 
In Sync11 Presentation The Biggest Loser
In Sync11 Presentation The Biggest LoserIn Sync11 Presentation The Biggest Loser
In Sync11 Presentation The Biggest Loser
 
Explain
ExplainExplain
Explain
 
Oracle Database 11g Security and Compliance Solutions - By Tom Kyte
Oracle Database 11g Security and Compliance Solutions - By Tom KyteOracle Database 11g Security and Compliance Solutions - By Tom Kyte
Oracle Database 11g Security and Compliance Solutions - By Tom Kyte
 
Oracle Database 11g vs 12c
Oracle Database 11g vs 12cOracle Database 11g vs 12c
Oracle Database 11g vs 12c
 
Oracle R12 EBS Performance Tuning
Oracle R12 EBS Performance TuningOracle R12 EBS Performance Tuning
Oracle R12 EBS Performance Tuning
 
All of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL DeveloperAll of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL Developer
 
Oracle 11g R2 Live Part 2
Oracle 11g R2 Live Part 2Oracle 11g R2 Live Part 2
Oracle 11g R2 Live Part 2
 
11g R2 Live Part 1
11g R2 Live Part 111g R2 Live Part 1
11g R2 Live Part 1
 
Oracle Database Overview
Oracle Database OverviewOracle Database Overview
Oracle Database Overview
 
Ten query tuning techniques every SQL Server programmer should know
Ten query tuning techniques every SQL Server programmer should knowTen query tuning techniques every SQL Server programmer should know
Ten query tuning techniques every SQL Server programmer should know
 
Oracle architecture ppt
Oracle architecture pptOracle architecture ppt
Oracle architecture ppt
 
Database administrator
Database administratorDatabase administrator
Database administrator
 
Oracle RAC 12c Release 2 - Overview
Oracle RAC 12c Release 2 - OverviewOracle RAC 12c Release 2 - Overview
Oracle RAC 12c Release 2 - Overview
 

Similar to Explaining the explain_plan

Presentation interpreting execution plans for sql statements
Presentation    interpreting execution plans for sql statementsPresentation    interpreting execution plans for sql statements
Presentation interpreting execution plans for sql statementsxKinAnx
 
Explain the explain_plan
Explain the explain_planExplain the explain_plan
Explain the explain_planMaria Colgan
 
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdfNOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdfcookie1969
 
Understanding DB2 Optimizer
Understanding DB2 OptimizerUnderstanding DB2 Optimizer
Understanding DB2 Optimizerterraborealis
 
Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ICarlos Oliveira
 
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
SQL Performance Solutions: Refactor Mercilessly, Index WiselySQL Performance Solutions: Refactor Mercilessly, Index Wisely
SQL Performance Solutions: Refactor Mercilessly, Index WiselyEnkitec
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsNirav Shah
 
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ OracleUnderstanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ OracleGuatemala User Group
 
Run your queries 14X faster without any investment!
Run your queries 14X faster without any investment!Run your queries 14X faster without any investment!
Run your queries 14X faster without any investment!Knoldus Inc.
 
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12cPresentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12cRonald Francisco Vargas Quesada
 
Advanced SQL For Data Scientists
Advanced SQL For Data ScientistsAdvanced SQL For Data Scientists
Advanced SQL For Data ScientistsDatabricks
 
Adaptive Query Optimization
Adaptive Query OptimizationAdaptive Query Optimization
Adaptive Query OptimizationAnju Garg
 
05_DP_300T00A_Optimize.pptx
05_DP_300T00A_Optimize.pptx05_DP_300T00A_Optimize.pptx
05_DP_300T00A_Optimize.pptxKareemBullard1
 
The Plan Cache Whisperer - Performance Tuning SQL Server
The Plan Cache Whisperer - Performance Tuning SQL ServerThe Plan Cache Whisperer - Performance Tuning SQL Server
The Plan Cache Whisperer - Performance Tuning SQL ServerJason Strate
 
Beginners guide to_optimizer
Beginners guide to_optimizerBeginners guide to_optimizer
Beginners guide to_optimizerMaria Colgan
 
U-SQL Query Execution and Performance Tuning
U-SQL Query Execution and Performance TuningU-SQL Query Execution and Performance Tuning
U-SQL Query Execution and Performance TuningMichael Rys
 
The art of querying – newest and advanced SQL techniques
The art of querying – newest and advanced SQL techniquesThe art of querying – newest and advanced SQL techniques
The art of querying – newest and advanced SQL techniquesZohar Elkayam
 
An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1Navneet Upneja
 

Similar to Explaining the explain_plan (20)

Presentation interpreting execution plans for sql statements
Presentation    interpreting execution plans for sql statementsPresentation    interpreting execution plans for sql statements
Presentation interpreting execution plans for sql statements
 
Explain the explain_plan
Explain the explain_planExplain the explain_plan
Explain the explain_plan
 
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdfNOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
 
Understanding DB2 Optimizer
Understanding DB2 OptimizerUnderstanding DB2 Optimizer
Understanding DB2 Optimizer
 
Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices I
 
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
SQL Performance Solutions: Refactor Mercilessly, Index WiselySQL Performance Solutions: Refactor Mercilessly, Index Wisely
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
 
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ OracleUnderstanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
 
Run your queries 14X faster without any investment!
Run your queries 14X faster without any investment!Run your queries 14X faster without any investment!
Run your queries 14X faster without any investment!
 
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12cPresentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12c
 
SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
 
Advanced SQL For Data Scientists
Advanced SQL For Data ScientistsAdvanced SQL For Data Scientists
Advanced SQL For Data Scientists
 
Adaptive Query Optimization
Adaptive Query OptimizationAdaptive Query Optimization
Adaptive Query Optimization
 
05_DP_300T00A_Optimize.pptx
05_DP_300T00A_Optimize.pptx05_DP_300T00A_Optimize.pptx
05_DP_300T00A_Optimize.pptx
 
The Plan Cache Whisperer - Performance Tuning SQL Server
The Plan Cache Whisperer - Performance Tuning SQL ServerThe Plan Cache Whisperer - Performance Tuning SQL Server
The Plan Cache Whisperer - Performance Tuning SQL Server
 
Beginners guide to_optimizer
Beginners guide to_optimizerBeginners guide to_optimizer
Beginners guide to_optimizer
 
U-SQL Query Execution and Performance Tuning
U-SQL Query Execution and Performance TuningU-SQL Query Execution and Performance Tuning
U-SQL Query Execution and Performance Tuning
 
The art of querying – newest and advanced SQL techniques
The art of querying – newest and advanced SQL techniquesThe art of querying – newest and advanced SQL techniques
The art of querying – newest and advanced SQL techniques
 
An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1An Approach to Sql tuning - Part 1
An Approach to Sql tuning - Part 1
 
Part5 sql tune
Part5 sql tunePart5 sql tune
Part5 sql tune
 

Recently uploaded

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 

Recently uploaded (20)

DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 

Explaining the explain_plan

  • 1. 1
  • 2. <Insert Picture Here> Explaining the Explain Plan: Interpreting Execution Plans for SQL Statements Maria Colgan Senior Principal Product Manager
  • 3. Agenda •  What is an execution plan <Insert Picture Here> •  How to generate a plan •  What is a good plan for the optimizer •  Understanding execution plans –  Cardinality –  Access paths –  Join type –  Join order •  Execution plan examples 3
  • 4. What is an Execution plan? •  Execution plans show the detailed steps necessary to execute a SQL statement •  These steps are expressed as a set of database operators that consumes and produces rows •  The order of the operators and their implementation is decided by the optimizer using a combination of query transformations and physical optimization techniques •  The display is commonly shown in a tabular format, but a plan is in fact tree-shaped 4
  • 5. What is an Execution plan? Query:
 SELECT prod_category, avg(amount_sold) FROM sales s, products p WHERE p.prod_id = s.prod_id GROUP BY prod_category; Tabular representation of plan Tree-shaped representation of plan ----------------------------------------------------------- Id Operation Name GROUP BY ----------------------------------------------------------- 0 SELECT STATEMENT 1 HASH GROUP BY HASH JOIN 2 HASH JOIN 3 TABLE ACCESS FULL PRODUCTS 4 PARTITION RANGE ALL 5 TABLE ACCESS FULL SALES TABLE ACCESS TABLE ACCESS ----------------------------------------------------------
 PRODUCTS SALES 5
  • 6. Agenda •  What is an execution plan <Insert Picture Here> •  How to generate a plan •  What is a good plan for the optimizer •  Understanding execution plans –  Cardinality –  Access paths –  Join type –  Join order •  Execution plan examples 6
  • 7. 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 in the cursor cache Either way use DBMS_XPLAN package to display plans Under certain conditions the plan shown with EXPLAIN PLAN can be different from the plan shown using V$SQL_PLAN 7
  • 8. How to get an Execution Plan Example 1 •  EXPLAIN PLAN command & dbms_xplan.display function SQL> EXPLAIN PLAN FOR SELECT prod_name, avg(amount_sold) FROM sales s, products p WHERE p.prod_id = s.prod_id GROUP BY prod_name; SQL> SELECT plan_table_output FROM table(dbms_xplan.display('plan_table',null,'basic'));
 ------------------------------------------ 






 Id Operation Name ------------------------------------------ 0 SELECT STATEMENT 1 HASH GROUP BY 2 HASH JOIN 3 TABLE ACCESS FULL PRODUCTS 4 PARTITION RANGE ALL 5 TABLE ACCESS FULL SALES ------------------------------------------- 8
  • 9. How to get an Execution Plan Example 2 •  Generate & display execution plan for last SQL stmts executed in a session SQL>SELECT prod_category, avg(amount_sold) FROM sales s, products p WHERE p.prod_id = s.prod_id GROUP BY prod_category; SQL> SELECT plan_table_output FROM table(dbms_xplan.display_cursor(null,null,'basic'));
 ------------------------------------------ 






 Id Operation Name ------------------------------------------ 0 SELECT STATEMENT 1 HASH GROUP BY 2 HASH JOIN 3 TABLE ACCESS FULL PRODUCTS 4 PARTITION RANGE ALL 5 TABLE ACCESS FULL SALES ------------------------------------------- 9
  • 10. Agenda •  What is an execution plan <Insert Picture Here> •  How to generate a plan •  What is a good plan for the optimizer •  Understanding execution plans –  Cardinality –  Access paths –  Join type –  Join order •  Execution plan examples 10
  • 11. What’s a Good Plan for the Optimizer? The Optimizer has two different goals •  Serial execution: It’s all about cost –  The cheaper, the better •  Parallel execution: it’s all about performance –  The faster, the better Two fundamental questions: •  What is cost? •  What is performance? 11
  • 12. 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 & IO as units of work •  Cost is an estimate of the amount of CPU and the number of disk I/Os, used to perform an operation Cost is an internal Oracle measurement 12
  • 13. What is performance? •  Getting as many queries completed as possible? •  Getting fastest possible elapsed time using the fewest resources? •  Getting the best concurrency rate? Actual Definition •  Performance is fastest possible response time for a query •  Goal is to complete the query as quickly as possible •  Optimizer does not focus on resources needed to execute the plan 13
  • 14. Agenda •  What is an execution plan <Insert Picture Here> •  How to generate a plan •  What is a good plan for the optimizer •  Understanding execution plans –  Cardinality –  Access paths –  Join type –  Join order •  Execution plan examples 14
  • 15. Cardinality What is it? •  Estimate of number rows that will be returned by each operation How does the Optimizer Determine it? •  Cardinality for a single column equality predicate = total num of rows num of distinct values –  For example: A table has 100 rows, a column has 10 distinct values => cardinality=10 rows •  More complicated predicates have more complicated cardinality calculation Why should you care? •  Influences everything! Access method, Join type, Join Order etc 15
  • 16. How to Identify Cardinality in an Execution Plan Cardinality the estimated # of rows returned Determine correct cardinality using a SELECT COUNT(*) from each table applying any WHERE Clause predicates belonging to that table 16
  • 17. Check Cardinality Estimates SELECT /*+ gather_plan_statistics */ p.prod_name, SUM(s.quantity_sold) FROM sales s, products p WHERE s.prod_id =p.prod_id GROUP By p.prod_name ; SELECT * FROM table ( DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'ALLSTATS LAST')); Compare the estimated number of rows returned for each operation in the plan to actual rows returned 17
  • 18. Check Cardinality using SQL Monitor SQL Monitor is the easiest way to compare the estimated number of rows returned for each operation in a parallel plan to actual rows returned 18
  • 19. Solutions to incorrect Cardinality estimates Cause Solution Stale or missing statistics DBMS_STATS Data Skew Create a histogram Multiple single column predicates Create a column group using on a table DBMS_STATS.CREATE_EXTENDED_STATS Function wrapped column Create statistics on the funct wrapped column using DBMS_STATS.CREATE_EXTENDED_STATS Multiple columns used in a join Create a column group on join columns using DBMS_STATS.CREATE_EXTENDED_STAT Complicated expression Use dynamic sampling level 4 or higher containing columns from multiple tables 19
  • 20. Agenda •  What is an execution plan <Insert Picture Here> •  How to generate a plan •  What is a good plan for the optimizer •  Understanding execution plans –  Cardinality –  Access paths –  Join type –  Join order •  Execution plan examples 20
  • 21. Access Paths – Getting the data Access Path Explanation Reads all rows from table & filters out those that do not meet the where clause Full table scan predicates. Used when no index, DOP set etc Rowid specifies the datafile & data block containing the row and the location of Table access by Rowid the row in that block. Used if rowid supplied by index or in where clause Only one row will be returned. Used when stmt contains a UNIQUE or a Index unique scan PRIMARY KEY constraint that guarantees that only a single row is accessed Accesses adjacent index entries returns ROWID values Used with equality on Index range scan non-unique indexes or range predicate on unique index (<.>, between etc) Skips the leading edge of the index & uses the rest Advantageous if there are Index skip scan few distinct values in the leading column and many distinct values in the non- leading column Processes all leaf blocks of an index, but only enough branch blocks to find 1st Full index scan leaf block. Used when all necessary columns are in index & order by clause matches index struct or if sort merge join is done Scans all blocks in index used to replace a FTS when all necessary columns Fast full index scan are in the index. Using multi-block IO & can going parallel Hash join of several indexes that together contain all the table columns that are Index joins referenced in the query. Wont eliminate a sort operation uses a bitmap for key values and a mapping function that converts each bit Bitmap indexes position to a rowid. Can efficiently merge indexes that correspond to several conditions in a WHERE clause 21
  • 22. How to Identify Access Path in an Execution Plan Look in Operation section to see how an object is being accessed If the wrong access method is being used check cardinality, join order… 22
  • 23. Access Path examples A table countries contains 10K rows & has a primary key on country_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'; 23
  • 24. Access Path examples A table countries contains 10K rows & has a primary key on country_id – What plan would you expect for these queries? Select country_id, name from countries where name='USA'; 24
  • 25. Common Access Path issues Issue Cause Uses a table scan instead of index DOP on table but not index, value of MBRC Picks wrong index Stale or missing statistics Cost of full index access is cheaper than index look up followed by table access Picks index that matches most # of column 25
  • 26. Agenda •  What is an execution plan <Insert Picture Here> •  How to generate a plan •  What is a good plan for the optimizer •  Understanding execution plans –  Cardinality –  Access paths –  Join type –  Join order •  Execution plan examples 26
  • 27. Join Types Join Type Explanation For every row in the outer table, Oracle accesses all the rows in the Nested Loops joins inner table Useful when joining small subsets of data and there is an efficient way to access the second table (index look up) The smaller of two tables is scan and resulting rows are used to build Hash Joins a hash table on the join key in memory. The larger table is then scan, join column of the resulting rows are hashed and the values used to probing the hash table to find the matching rows. Useful for larger tables & if equality pred Consists of two steps: Sort Merge joins 1.  Both the inputs are sorted on the join key. 2.  The sorted lists are merged together. Useful when the join condition between two tables is an inequality condition or one of the table is already ordered e.g. index access Joins every row from one data source with every row from the other Cartesian Joins data source, creating the Cartesian Product of the two sets. Only good if tables are very small. Only choice if there is no join condition specified in query Returns all rows that satisfy the join condition and also returns all of Outer Joins the rows from the table without the (+) for which no rows from the other table satisfy the join condition 27
  • 28. How to Identify Join Type in an Execution Plan Look in the Operation section to check the right join type is used If wrong join type is used check stmt is written correctly & cardinality estimates 28
  • 29. Join Type Example 1 What Join type should be use for this Query? SELECT e.last_name, e.salary, d.department_name FROM hr.employees e, hr.departments d WHERE d.departments_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 29
  • 30. 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 30
  • 31. Join Type Example 3 What Join type should be use for this Query? SELECT o.order_id,0.order_date,e.name FROM oe.orders o , hr.employees e; Orders has 105 rows Employees has 107 rows 31
  • 32. Join Type Example 3 Cartesian product not always bad •  Chosen when the number of rows being joined is low •  Commonly used to join multiple small dimensions to one large fact table s.prod_id=p.prod_id s.cust_id=c.cust_id PRODUCTS SALES CUSTOMERS s.promo_id=c.promo_id PROMOTIONS 32
  • 33. Join Type Example 3 Cartesian product •  By joining the three small dimension tables together first we minimize the number of rows being carried through the joins 33
  • 34. Join Type Example 4 What Join type should be use for this Query? SELECT d.department_id,e.emp_id FROM hr.employees e FULL OUTER JOIN hr.departments d ON e.department_id = d.department_id ORDER BY d.department_id; Employees has 107 rows Departments has 27 rows Foreign key relationship between Employees and Departments on dept_id 34
  • 35. What causes the wrong Join Type to be selected Issue Cause Nested loop Cardinality estimate on the left side is under selected instead estimated triggers Nested loop to be selected of hash join Hash join selected In case of a hash join the Optimizer doesn’t taken instead of nested into consideration the benefit of caching. Rows on loop the left come in a clustered fashion or (ordered) so the probe into the right is less expensive Cartesian Joins Cardinality underestimation 35
  • 36. Agenda •  What is an execution plan <Insert Picture Here> •  How to generate a plan •  What is a good plan for the optimizer •  Understanding execution plans –  Cardinality –  Access paths –  Join type –  Join order •  Execution plan examples 36
  • 37. 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 affected by the access paths available Some basic rules •  Joins guaranteed to produce at most one row always go first –  Joins between two row sources that have only one row each •  When outer joins are used the table with the outer join operator must come after the other table in the predicate •  If view merging is not possible all tables in the view will be joined before joining to the tables outside the view 37
  • 38. How to Identify Join order in an Execution Plan 1 2 3 4 5 Want to start with the table that reduce the result set the most If the join order is not correct, check the statistics, cardinality & access methods 38
  • 39. Finding the Join Order for complex SQL statement •  It can be hard to determine Join Order for Complex SQL statements but it is easily visible in the outline data of plan SELECT * FROM table(dbms_xplan.display_cursor(FORMAT=>’TYPICAL +outline’); The leading hint tells you the join order 39
  • 40. What causes the wrong Join Order Causes Incorrect single table cardinality estimates Incorrect join cardinality estimates 40
  • 41. Agenda •  What is an execution plan <Insert Picture Here> •  How to generate a plan •  What is a good plan for the optimizer •  Understanding execution plans –  Cardinality –  Access paths –  Join type –  Join order •  Execution plan examples 41
  • 42. Example SQL Statement •  Find all the employees who make as much or more than their manager SELECT e1.last_name, e1.job_title, e1.total_comp FROM (SELECT e.manager_id, e.last_name, j.job_title, (e.salary+e.commission_pct) total_comp FROM employees e, jobs j, departments d WHERE d.department_name = 'Sales' AND e.department_id = d.department_id AND e.job_id = j.job_id ) e1, (SELECT e.employee_id, (e.salary+e.commission_pct) tc FROM employees e, departments d WHERE d.department_name = ‘Sales' AND e.department_id = d.department_id ) e2 WHERE e1.manager_id = e2.employee_id AND e1.total_comp >= e2.tc; 42
  • 43. Is it a good Execution plan? 1. Is the estimated number of rows being returned accurate? 3.Are the access method correct? 2. Are the cardinality estimates accurate? Means no stats gathered strong indicator this won’t be best possible plan 43
  • 44. Example Cont’d Execution plan 1 2 3 4 5 4. Are the right join types being used? 5. Is the join order correct? Is the table that eliminates the most rows accessed first? 44
  • 45. What does the Plan Tree look like? NESTED LOOP NESTED LOOP INDEX UNIQUE SCAN - TABLE ACCESS JOBS INDEX UNIQUE SCAN - HASH JOIN TABLE ACCESS DEPARTMENT MERGE JOIN TABLE ACCESS CARTESIAN EMPLOYEES TABLE ACCESS TABLE ACCESS DEPARTMENT EMPLOYEES 45
  • 46. Example Cont’d Execution plan - Solution 1. Only 1 row is actually returned and the cost is 4 lower now 4. Join types have changed to be all NL 1 5. The join 2 order has changed 3 4 5 3. Access methods have changed for 2. Cardinalities are correct some tables and with each join number of rows reduced 46
  • 47. What does the Plan Tree look like? NESTED LOOP INDEX UNIQUE SCAN - NESTED LOOP TABLE ACCESS DEPARTMENT NESTED LOOP INDEX UNIQUE SCAN - TABLE ACCESS JOBS INDEX RANGE SCAN - NESTED LOOP TABLE ACCESS EMPLOYEES TABLE ACCESS INDEX RANGE SCAN - DEPARTMENT TABLE ACCESS EMPLOYEES 47
  • 48. Q&A 48
  • 49. The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 49