SlideShare a Scribd company logo
1 of 47
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Beginners Guide to Oracle Optimizer
Maria Colgan
Master Product Manager
Oracle Database Server Technologies
December 2018
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
The following 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, timing and price of any
features or functionality described for Oracle’s products may change and remains at the
sole discretion of Oracle corporation. Fees apply for new database product offerings.
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Expectations
• This is a short beginners guide to the different aspects of the Optimizer
• This material will not instantly make you an Optimizer expert!
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
What happens when a SQL statement is issued?
User
Library Cache
Shared SQL Area
Shared Pool
CnC1 C2 …
3
Optimizer
Oracle Database
Code Generator
1
4
SQL Execution
Syntax Check
Semantic Check
Shared Pool check
2
Parsing
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Oracle Optimizer
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Cost Based Optimizer
• Initial design based on IBM research paper
– Access Path Selection in a Relational Database Management System (1979)
• Approach outlined in the paper was
– Multiple execution plans generated for a statement
– Estimated cost is computed for each plan
– Optimizer selects the plan with the lowest estimated cost
A Brief History
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Understanding how the Optimizer works
Query Transformation
Rewrite query text to allow it to be processed
more efficiently
Plan Generator
Multiple plans are generated for
each SQL, using different access
paths and join types. Each plan is
costed and plan with the lowest
cost is used.
Cost Estimator
Cost is an estimate of the amount of
CPU and the number of disk I/Os, used
to perform an operation
Optimizer
Statistics
Schema definitions
Controlled by OPTIMIZER_FEATURES_ENABLED
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Optimizer Transformations
• Translate statements into semantically equivalent SQL that can be processed more
efficiently
• Initial transformations were heuristic based
– Applied to SQL statements based on their structural properties only
• Predominately cost based now
• Transformations include
– Subquery Unnesting
– View Merging
– OR Expansion
– Star transformation
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
• Transforms queries that contain OR
predicates into the form of a
UNION ALL query of two or more
branches
• Without the transformation
Optimizer treats OR predicates as a
single unit
• Can’t use index on either column
SELECT *
FROM products p
WHERE prod_category ='Photo'
OR prod_subcategory ='Camera Media';
Or Expansion
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
• The transformation adds an
LNNVL() function to the second
branch in order to avoid duplicates
being generated across branches
• The LNNVL function returns TRUE,
if the predicate evaluates to FALSE
or if the predicate involved is NULL;
otherwise it will return FALSE
• lnnvl(true) is FALSE,
lnnvl(false||null) is TRUE
SELECT *
FROM products p
WHERE prod_subcategory ='Camera Media’
UNION ALL
SELECT *
FROM products p
WHERE prod_category ='Photo’
AND lnnvl(prod_subcategory =
'Camera Media')
;
Or Expansion
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
OR Expansion
• Transformation allows an index access to be considered for each branch of
the UNION ALL
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Understanding how the Optimizer works
Query Transformation
Rewrite query text to allow it to be processed
more efficiently
Plan Generator
Multiple plans are generated for
each SQL, using different access
paths and join types. Each plan is
costed and plan with the lowest
cost is used.
Cost Estimator
Cost is an estimate of the amount of
CPU and the number of disk I/Os, used
to perform an operation
Optimizer
Statistics
Schema definitions
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
DATA DICTIONARY
OPTIMIZER STATISTICS
Index Table Column System
PROMO_PK Index
PROMOTIONS Table
Execution
plan
Optimizer
CPU & IO
Optimizer Statistics
PROMO_I
D
PROMO_NAME … PROMO_DATE
1 Promo_1 … 15-NOV-98
2 Promo_1 … 31-DEC-98
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Basic table statistics collected by Oracle
• By default the following basic table & column statistic are collected either
during a CTAS or IAS command* or via a DBMS_STATS.GATHER_*_STATS op:
– Number of Rows
– Number of blocks
– Average row length
– Number of distinct values
– Number of nulls in column
– Minimum and maximum values for a column
– Data distribution via histograms++
*Online stats available since 12cR1 ++ Histograms are not gathered online
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Basic index statistics collected by Oracle
• Index statistics are automatically gathered during creation and maintained by
DBMS_STATS.GATHER_*_STATS commands
• Index statistics include:
– Number of leaf blocks
– Branch Levels
– Clustering factor
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
How statistics are automatically gathered
• Oracle automatically collect statistics for all database objects, which are
missing statistics or have stale statistics
• AutoTask runs during a predefined maintenance window
• Internally prioritizes the database objects
– Both user schema and dictionary tables
– Objects that need updated statistics most are processed first
• Controlled by DBMS_AUTO_TASK_ADMIN package or Enterprise Manager
• Monitor using DBA_AUTOTASK_* views
and fixed object statistics in 12c
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
How you can manually gather statistics
• Use DBMS_STATS Package to manually gather statistics
• Your gather statistics commands should be this simple
BEGIN
dbms_stats.Gather_database_stats();
END; /
BEGIN
dbms_stats.Gather_schema_stats('SH');
END; /
BEGIN
dbms_stats.Gather_table_stats('SH', 'SALES');
END; /
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
How to change default parameter values for gathering stats
• Occasionally default parameter values may need to change
• For example - features not automatically on by default
– Incremental Statistics
• Ability to accurate generate global statistics from partition level statistics
BEGIN
dbms_stats.Set_global_prefs('INCREMENTAL', 'TRUE');
END;
/
BEGIN
dbms_stats.Set_table_prefs(‘SH’,’SALES’'INCREMENTAL', 'TRUE');
END;
/
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Understanding how the Optimizer works
Query Transformation
Rewrite query text to allow it to be processed
more efficiently
Plan Generator
Multiple plans are generated for
each SQL, using different access
paths and join types. Each plan is
costed and plan with the lowest
cost is used.
Cost Estimator
Cost is an estimate of the amount of
CPU and the number of disk I/Os, used
to perform an operation
Optimizer
Statistics
Schema definitions
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
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
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
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;
HASH JOIN
TABLE ACCESS
SALES
Tree-shaped representation of plan
TABLE ACCESS
PRODUCTS
GROUP BY
Tabular representation of plan
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
• Autotrace • SQL Monitor
22
• SQL Developer • TKPROF
Many Ways to View an Execution Plan
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Identifying elements of an execution plan - ID
ID COLUMN MAKES IT EASIER TO IDENTIFY EACH STEP
* INDICATES THAT STEP IS BASED OFF A PREDICATED
LISTED BELOW THE PLAN TABLE
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Identifying elements of an execution plan - OPERATION
OPERATION COLUMN SHOWS
ACTUAL STEPS TAKEN TO EXECUTE
THE PLAN INCLUDING:
• DATA ACCESS METHODS
• JOIN METHODS & TYPES
• AGGREGATIONS
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Access paths – Getting the data
Access Path Explanation
Full table scan Reads all rows from table & filters out those that do not meet the where clause predicates. Used when no index,
DOP set etc
Table access by Rowid Rowid specifies the datafile & data block containing the row and the location of the row in that block. Used if
rowid supplied by index or in where clause
Index unique scan Only one row will be returned. Used when stmt contains a UNIQUE or a PRIMARY KEY constraint that guarantees
that only a single row is accessed
Index range scan Accesses adjacent index entries returns ROWID values Used with equality on non-unique indexes or range
predicate on unique index (<.>, between etc)
Index skip scan Skips the leading edge of the index & uses the rest Advantageous if there are few distinct values in the leading
column and many distinct values in the non-leading column
Full index scan Processes all leaf blocks of an index, but only enough branch blocks to find 1st leaf block. Used when all necessary
columns are in index & order by clause matches index struct or if sort merge join is done
Fast full index scan Scans all blocks in index used to replace a FTS when all necessary columns are in the index. Using multi-block IO
& can going parallel
Index joins Hash join of several indexes that together contain all the table columns that are referenced in the query. Won’t
eliminate a sort operation
Bitmap indexes Uses a bitmap for key values and a mapping function that converts each bit position to a rowid. Can efficiently
merge indexes that correspond to several conditions in a WHERE clause
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Access path example 1
Table products contains 10K rows & has a primary key on product_id
SELECT product_id, name
FROM products
WHERE products_id IN (‘P123’,’P456’,’P789‘);
What plan would you expect for this query?
PRODUCTS
PRODUCT_PK
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Access path example 2
Table products contains 10K rows & has a primary key on product_id
SELECT product_id, name
FROM products
WHERE product_id BETWEEN ‘P123' AND ‘P789';
What plan would you expect for this query?
PRODUCTS
PRODUCT_PK
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Identifying elements of an execution plan - OPERATION
OPERATION COLUMN SHOWS
ACTUAL STEPS TAKEN TO EXECUTE
THE PLAN INCLUDING:
• DATA ACCESS METHODS
• JOIN METHODS & TYPES
• AGGREGATIONS
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Join methods
Join Methods Explanation
Nested Loops joins For every row in the outer table, Oracle accesses all the rows in the inner
table Useful when joining small subsets of data and there is an efficient
way to access the second table (index look up)
Hash Joins The smaller of two tables is scan and resulting rows are used to build 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
predicate
Sort Merge joins Consists of two steps:
1. Sort join operation: Both the inputs are sorted on the join key.
2. Merge join operation: The sorted lists are merged together.
Useful when the join condition between two tables is an inequality condition
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Join types
Join Type Explanation
Cartesian Joins Joins every row from one data source with every row from the other 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
Outer Joins Returns all rows that satisfy the join condition and also returns all of the rows
from the table without the (+) for which no rows from the other table satisfy
the join condition
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Join method example 1
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
What join method would you expect for this query?
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Join method example 1
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;
What join method would you expect for this query?
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Identifying elements of an execution plan - CARDINALITY
CARDINALITY IS THE
ESTIMATED NUMBER OF
ROWS RETURN BY EACH
OPERATION IN THE PLAN
AFTER PREDICATES HAVE
BEEN APPLIED
Determine correct cardinality
using a SELECT COUNT(*) from
each table applying any WHERE
Clause predicates belonging to
that table
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Checking 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'));
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Checking cardinality estimates
SELECT * FROM table (
DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'ALLSTATS LAST'));
Compare estimated number of rows returned with actual rows returned
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Checking cardinality estimates for Parallel Execution
SELECT * FROM table (
DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'ALLSTATS LAST'));
Note: a lot of the data is zero in the
A-rows column because we only
show last executed cursor which is
the QC. Need to use ALLSTATS ALL to
see info on all parallel server cursors
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Easiest way to compare the estimated number of rows returned with actual rows returned
Check cardinality using SQL Monitor
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Identifying elements of an execution plan - CARDINALITY
AMOUNT OF
DATA TO BE
PROCESSED IN
BYTES BY EACH
OPERATION
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Identifying elements of an execution plan - CARDINALITY
COST REPRESENTS UNITS OF WORK OR
RESOURCES (CPU & IO) USED TO PERFORM
EACH OPERATION IN THE PLAN
COST IS AN INTERNAL ORACLE
MEASUREMENT AND SHOULD BE USED
FOR COMPARISION PUROPOSES ONLY
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Understanding how the Optimizer works
Query Transformation
Rewrite query text to allow it to be processed
more efficiently
Plan Generator
Multiple plans are generated for
each SQL, using different access
paths and join types. Each plan is
costed and plan with the lowest
cost is used.
Cost Estimator
Cost is an estimate of the amount of
CPU and the number of disk I/Os, used
to perform an operation
Optimizer
Statistics
Schema definitions
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Best way to influence the Optimizer
• Statistics
• SQL Plan Management
• SQL Patch
• SQL Profile*
• Hints
* Requires Diagnostics Pack
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
What are hints?
• Hints allow you to influence the Optimizer when it has to choose between
several possibilities
• A hint is a directive that will be followed when applicable
– Hints are only evaluated when they apply to a decision that has to be made
• Can influence everything from the Optimizer mode used to each operation
in the execution
• Automatically means the Cost Based Optimizer will be used
– Only exception is the RULE hint but it must be used alone
• Use OPTIMIZER_IGNORE_HINTS parameter to test query performance
without hints
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Hints can influence all aspects of a plan
• Most hints have corresponding negative hint preceded by word ‘NO_’
• More information on hints can be found in chapter 3 of SQL Reference Guide
 Hints to influence cardinality
• DYNAMIC_SAMPLING
• CARDINALITY
 Hints to influence join methods
• USE_NL_WITH_INDEX
• USE_HASH
 Hints to influence access paths
• FULL
• INDEX
 Hints to influence join order
• LEADING
• ORDERED
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
How to use Optimizer hints
• Hints are inserted in a SQL statement in the form of a comment with an
additional + sign
• They go immediately after the keyword (SELECT, INSERT, etc)
SELECT /* this is a comment */ count(*) FROM sales;
SELECT /*+ this is a comment */ count(*) FROM sales;
Overview
Note: Hint syntax is correct but it is not a valid hint so it is treated as comment
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
If you can hint it, baseline it
• Its not always possible to add hints to third party applications
• Hints can be extremely difficult to manage over time
• Once added never removed
Alternative approach to hints
Solution
 Use SQL Plan Management (SPM)
 Influence the execution plan without adding hints directly to queries
 SPM available in both SE and EE, no additional options required
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Summary
• Learn to work with the Optimizer
and not against it
• Enjoy the challenge, knowing its
only as smart as the folks
that feed it information!
• More information can be found on
the Optimizer blog or Oracle.com
– https://blogs.oracle.com/optimizer
– https://www.oracle.com/technetwork/database/database-technologies/query-
optimization/overview/index.html
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
If you have more questions later, feel free to ask

More Related Content

What's hot

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
Guatemala User Group
 
Understanding my database through SQL*Plus using the free tool eDB360
Understanding my database through SQL*Plus using the free tool eDB360Understanding my database through SQL*Plus using the free tool eDB360
Understanding my database through SQL*Plus using the free tool eDB360
Carlos Sierra
 
Oracle SQL Performance Tuning and Optimization v26 chapter 1
Oracle SQL Performance Tuning and Optimization v26 chapter 1Oracle SQL Performance Tuning and Optimization v26 chapter 1
Oracle SQL Performance Tuning and Optimization v26 chapter 1
Kevin Meade
 

What's hot (20)

Part5 sql tune
Part5 sql tunePart5 sql tune
Part5 sql tune
 
Oracle statistics by example
Oracle statistics by exampleOracle statistics by example
Oracle statistics by example
 
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsOracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
 
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
 
Same plan different performance
Same plan different performanceSame plan different performance
Same plan different performance
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
 
Part3 Explain the Explain Plan
Part3 Explain the Explain PlanPart3 Explain the Explain Plan
Part3 Explain the Explain Plan
 
Polymorphic Table Functions in SQL
Polymorphic Table Functions in SQLPolymorphic Table Functions in SQL
Polymorphic Table Functions in SQL
 
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptxFive_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
 
Oracle AWR Data mining
Oracle AWR Data miningOracle AWR Data mining
Oracle AWR Data mining
 
Understanding my database through SQL*Plus using the free tool eDB360
Understanding my database through SQL*Plus using the free tool eDB360Understanding my database through SQL*Plus using the free tool eDB360
Understanding my database through SQL*Plus using the free tool eDB360
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning Fundamentals
 
SQLd360
SQLd360SQLd360
SQLd360
 
Tanel Poder Oracle Scripts and Tools (2010)
Tanel Poder Oracle Scripts and Tools (2010)Tanel Poder Oracle Scripts and Tools (2010)
Tanel Poder Oracle Scripts and Tools (2010)
 
Oracle Latch and Mutex Contention Troubleshooting
Oracle Latch and Mutex Contention TroubleshootingOracle Latch and Mutex Contention Troubleshooting
Oracle Latch and Mutex Contention Troubleshooting
 
Oracle SQL Performance Tuning and Optimization v26 chapter 1
Oracle SQL Performance Tuning and Optimization v26 chapter 1Oracle SQL Performance Tuning and Optimization v26 chapter 1
Oracle SQL Performance Tuning and Optimization v26 chapter 1
 
Understanding oracle rac internals part 1 - slides
Understanding oracle rac internals   part 1 - slidesUnderstanding oracle rac internals   part 1 - slides
Understanding oracle rac internals part 1 - slides
 
TFA Collector - what can one do with it
TFA Collector - what can one do with it TFA Collector - what can one do with it
TFA Collector - what can one do with it
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning Fundamentals
 
Oracle Performance Tools of the Trade
Oracle Performance Tools of the TradeOracle Performance Tools of the Trade
Oracle Performance Tools of the Trade
 

Similar to Beginners guide to_optimizer

Admin Guiding Query Plans
Admin Guiding Query PlansAdmin Guiding Query Plans
Admin Guiding Query Plans
rsnarayanan
 
Con7091 sql tuning for expert db as-oow17_oct2_1507314871265001m0x4
Con7091 sql tuning for expert db as-oow17_oct2_1507314871265001m0x4Con7091 sql tuning for expert db as-oow17_oct2_1507314871265001m0x4
Con7091 sql tuning for expert db as-oow17_oct2_1507314871265001m0x4
asifanw
 
Ssis Best Practices Israel Bi U Ser Group Itay Braun
Ssis Best Practices   Israel Bi U Ser Group   Itay BraunSsis Best Practices   Israel Bi U Ser Group   Itay Braun
Ssis Best Practices Israel Bi U Ser Group Itay Braun
sqlserver.co.il
 

Similar to Beginners guide to_optimizer (20)

Explain the explain_plan
Explain the explain_planExplain the explain_plan
Explain the explain_plan
 
How to analyze and tune sql queries for better performance
How to analyze and tune sql queries for better performanceHow to analyze and tune sql queries for better performance
How to analyze and tune sql queries for better performance
 
05_DP_300T00A_Optimize.pptx
05_DP_300T00A_Optimize.pptx05_DP_300T00A_Optimize.pptx
05_DP_300T00A_Optimize.pptx
 
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
 
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
 
Admin Guiding Query Plans
Admin Guiding Query PlansAdmin Guiding Query Plans
Admin Guiding Query Plans
 
A Deep Dive into HPCM for Planning and Essbase Professionals
A Deep Dive into HPCM for Planning and Essbase ProfessionalsA Deep Dive into HPCM for Planning and Essbase Professionals
A Deep Dive into HPCM for Planning and Essbase Professionals
 
Con7091 sql tuning for expert db as-oow17_oct2_1507314871265001m0x4
Con7091 sql tuning for expert db as-oow17_oct2_1507314871265001m0x4Con7091 sql tuning for expert db as-oow17_oct2_1507314871265001m0x4
Con7091 sql tuning for expert db as-oow17_oct2_1507314871265001m0x4
 
SQL Server 2008 Development for Programmers
SQL Server 2008 Development for ProgrammersSQL Server 2008 Development for Programmers
SQL Server 2008 Development for Programmers
 
Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2
Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2
Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2
 
Web Cloud Computing SQL Server - Ferrara University
Web Cloud Computing SQL Server  -  Ferrara UniversityWeb Cloud Computing SQL Server  -  Ferrara University
Web Cloud Computing SQL Server - Ferrara University
 
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 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
 
Ssis Best Practices Israel Bi U Ser Group Itay Braun
Ssis Best Practices   Israel Bi U Ser Group   Itay BraunSsis Best Practices   Israel Bi U Ser Group   Itay Braun
Ssis Best Practices Israel Bi U Ser Group Itay Braun
 
Upcoming changes in MySQL 5.7
Upcoming changes in MySQL 5.7Upcoming changes in MySQL 5.7
Upcoming changes in MySQL 5.7
 
MySQL 8.0 in a nutshell
MySQL 8.0 in a nutshellMySQL 8.0 in a nutshell
MySQL 8.0 in a nutshell
 
State ofdolphin short
State ofdolphin shortState ofdolphin short
State ofdolphin short
 
Optimizer overviewoow2014
Optimizer overviewoow2014Optimizer overviewoow2014
Optimizer overviewoow2014
 
18. Madhur Hemnani - Result Orientated Innovation with Oracle HR Analytics
18. Madhur Hemnani - Result Orientated Innovation with Oracle HR Analytics18. Madhur Hemnani - Result Orientated Innovation with Oracle HR Analytics
18. Madhur Hemnani - Result Orientated Innovation with Oracle HR Analytics
 
How to analyze and tune sql queries for better performance vts2016
How to analyze and tune sql queries for better performance vts2016How to analyze and tune sql queries for better performance vts2016
How to analyze and tune sql queries for better performance vts2016
 

More from Maria Colgan

More from Maria Colgan (14)

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
 
Part2 Best Practices for Managing Optimizer Statistics
Part2 Best Practices for Managing Optimizer StatisticsPart2 Best Practices for Managing Optimizer Statistics
Part2 Best Practices for Managing Optimizer Statistics
 
Ground Breakers Romania: Oracle Autonomous Database
Ground Breakers Romania: Oracle Autonomous DatabaseGround Breakers Romania: Oracle Autonomous Database
Ground Breakers Romania: Oracle Autonomous Database
 
Ground Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_planGround Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_plan
 
What to Expect From Oracle database 19c
What to Expect From Oracle database 19cWhat to Expect From Oracle database 19c
What to Expect From Oracle database 19c
 
The Changing Role of a DBA in an Autonomous World
The Changing Role of a DBA in an Autonomous WorldThe Changing Role of a DBA in an Autonomous World
The Changing Role of a DBA in an Autonomous World
 
Oracle Database in-Memory Overivew
Oracle Database in-Memory OverivewOracle Database in-Memory Overivew
Oracle Database in-Memory Overivew
 
Useful PL/SQL Supplied Packages
Useful PL/SQL Supplied PackagesUseful PL/SQL Supplied Packages
Useful PL/SQL Supplied Packages
 
JSON and the Oracle Database
JSON and the Oracle DatabaseJSON and the Oracle Database
JSON and the Oracle Database
 
Five Tips to Get the Most Out of Your Indexing
Five Tips to Get the Most Out of Your IndexingFive Tips to Get the Most Out of Your Indexing
Five Tips to Get the Most Out of Your Indexing
 
Harnessing the Power of Optimizer Hints
Harnessing the Power of Optimizer HintsHarnessing the Power of Optimizer Hints
Harnessing the Power of Optimizer Hints
 
Oracle optimizer bootcamp
Oracle optimizer bootcampOracle optimizer bootcamp
Oracle optimizer bootcamp
 
What_to_expect_from_oracle_database_12c
What_to_expect_from_oracle_database_12cWhat_to_expect_from_oracle_database_12c
What_to_expect_from_oracle_database_12c
 
Oracle database 12c_and_DevOps
Oracle database 12c_and_DevOpsOracle database 12c_and_DevOps
Oracle database 12c_and_DevOps
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Recently uploaded (20)

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

Beginners guide to_optimizer

  • 1. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Beginners Guide to Oracle Optimizer Maria Colgan Master Product Manager Oracle Database Server Technologies December 2018
  • 2. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement The following 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, timing and price of any features or functionality described for Oracle’s products may change and remains at the sole discretion of Oracle corporation. Fees apply for new database product offerings.
  • 3. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Expectations • This is a short beginners guide to the different aspects of the Optimizer • This material will not instantly make you an Optimizer expert!
  • 4. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | What happens when a SQL statement is issued? User Library Cache Shared SQL Area Shared Pool CnC1 C2 … 3 Optimizer Oracle Database Code Generator 1 4 SQL Execution Syntax Check Semantic Check Shared Pool check 2 Parsing
  • 5. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Oracle Optimizer
  • 6. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Cost Based Optimizer • Initial design based on IBM research paper – Access Path Selection in a Relational Database Management System (1979) • Approach outlined in the paper was – Multiple execution plans generated for a statement – Estimated cost is computed for each plan – Optimizer selects the plan with the lowest estimated cost A Brief History
  • 7. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Understanding how the Optimizer works Query Transformation Rewrite query text to allow it to be processed more efficiently Plan Generator Multiple plans are generated for each SQL, using different access paths and join types. Each plan is costed and plan with the lowest cost is used. Cost Estimator Cost is an estimate of the amount of CPU and the number of disk I/Os, used to perform an operation Optimizer Statistics Schema definitions Controlled by OPTIMIZER_FEATURES_ENABLED
  • 8. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Optimizer Transformations • Translate statements into semantically equivalent SQL that can be processed more efficiently • Initial transformations were heuristic based – Applied to SQL statements based on their structural properties only • Predominately cost based now • Transformations include – Subquery Unnesting – View Merging – OR Expansion – Star transformation
  • 9. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | • Transforms queries that contain OR predicates into the form of a UNION ALL query of two or more branches • Without the transformation Optimizer treats OR predicates as a single unit • Can’t use index on either column SELECT * FROM products p WHERE prod_category ='Photo' OR prod_subcategory ='Camera Media'; Or Expansion
  • 10. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | • The transformation adds an LNNVL() function to the second branch in order to avoid duplicates being generated across branches • The LNNVL function returns TRUE, if the predicate evaluates to FALSE or if the predicate involved is NULL; otherwise it will return FALSE • lnnvl(true) is FALSE, lnnvl(false||null) is TRUE SELECT * FROM products p WHERE prod_subcategory ='Camera Media’ UNION ALL SELECT * FROM products p WHERE prod_category ='Photo’ AND lnnvl(prod_subcategory = 'Camera Media') ; Or Expansion
  • 11. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | OR Expansion • Transformation allows an index access to be considered for each branch of the UNION ALL
  • 12. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Understanding how the Optimizer works Query Transformation Rewrite query text to allow it to be processed more efficiently Plan Generator Multiple plans are generated for each SQL, using different access paths and join types. Each plan is costed and plan with the lowest cost is used. Cost Estimator Cost is an estimate of the amount of CPU and the number of disk I/Os, used to perform an operation Optimizer Statistics Schema definitions
  • 13. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | DATA DICTIONARY OPTIMIZER STATISTICS Index Table Column System PROMO_PK Index PROMOTIONS Table Execution plan Optimizer CPU & IO Optimizer Statistics PROMO_I D PROMO_NAME … PROMO_DATE 1 Promo_1 … 15-NOV-98 2 Promo_1 … 31-DEC-98
  • 14. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Basic table statistics collected by Oracle • By default the following basic table & column statistic are collected either during a CTAS or IAS command* or via a DBMS_STATS.GATHER_*_STATS op: – Number of Rows – Number of blocks – Average row length – Number of distinct values – Number of nulls in column – Minimum and maximum values for a column – Data distribution via histograms++ *Online stats available since 12cR1 ++ Histograms are not gathered online
  • 15. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Basic index statistics collected by Oracle • Index statistics are automatically gathered during creation and maintained by DBMS_STATS.GATHER_*_STATS commands • Index statistics include: – Number of leaf blocks – Branch Levels – Clustering factor
  • 16. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | How statistics are automatically gathered • Oracle automatically collect statistics for all database objects, which are missing statistics or have stale statistics • AutoTask runs during a predefined maintenance window • Internally prioritizes the database objects – Both user schema and dictionary tables – Objects that need updated statistics most are processed first • Controlled by DBMS_AUTO_TASK_ADMIN package or Enterprise Manager • Monitor using DBA_AUTOTASK_* views and fixed object statistics in 12c
  • 17. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | How you can manually gather statistics • Use DBMS_STATS Package to manually gather statistics • Your gather statistics commands should be this simple BEGIN dbms_stats.Gather_database_stats(); END; / BEGIN dbms_stats.Gather_schema_stats('SH'); END; / BEGIN dbms_stats.Gather_table_stats('SH', 'SALES'); END; /
  • 18. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | How to change default parameter values for gathering stats • Occasionally default parameter values may need to change • For example - features not automatically on by default – Incremental Statistics • Ability to accurate generate global statistics from partition level statistics BEGIN dbms_stats.Set_global_prefs('INCREMENTAL', 'TRUE'); END; / BEGIN dbms_stats.Set_table_prefs(‘SH’,’SALES’'INCREMENTAL', 'TRUE'); END; /
  • 19. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Understanding how the Optimizer works Query Transformation Rewrite query text to allow it to be processed more efficiently Plan Generator Multiple plans are generated for each SQL, using different access paths and join types. Each plan is costed and plan with the lowest cost is used. Cost Estimator Cost is an estimate of the amount of CPU and the number of disk I/Os, used to perform an operation Optimizer Statistics Schema definitions
  • 20. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 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
  • 21. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 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; HASH JOIN TABLE ACCESS SALES Tree-shaped representation of plan TABLE ACCESS PRODUCTS GROUP BY Tabular representation of plan
  • 22. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | • Autotrace • SQL Monitor 22 • SQL Developer • TKPROF Many Ways to View an Execution Plan
  • 23. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Identifying elements of an execution plan - ID ID COLUMN MAKES IT EASIER TO IDENTIFY EACH STEP * INDICATES THAT STEP IS BASED OFF A PREDICATED LISTED BELOW THE PLAN TABLE
  • 24. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Identifying elements of an execution plan - OPERATION OPERATION COLUMN SHOWS ACTUAL STEPS TAKEN TO EXECUTE THE PLAN INCLUDING: • DATA ACCESS METHODS • JOIN METHODS & TYPES • AGGREGATIONS
  • 25. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Access paths – Getting the data Access Path Explanation Full table scan Reads all rows from table & filters out those that do not meet the where clause predicates. Used when no index, DOP set etc Table access by Rowid Rowid specifies the datafile & data block containing the row and the location of the row in that block. Used if rowid supplied by index or in where clause Index unique scan Only one row will be returned. Used when stmt contains a UNIQUE or a PRIMARY KEY constraint that guarantees that only a single row is accessed Index range scan Accesses adjacent index entries returns ROWID values Used with equality on non-unique indexes or range predicate on unique index (<.>, between etc) Index skip scan Skips the leading edge of the index & uses the rest Advantageous if there are few distinct values in the leading column and many distinct values in the non-leading column Full index scan Processes all leaf blocks of an index, but only enough branch blocks to find 1st leaf block. Used when all necessary columns are in index & order by clause matches index struct or if sort merge join is done Fast full index scan Scans all blocks in index used to replace a FTS when all necessary columns are in the index. Using multi-block IO & can going parallel Index joins Hash join of several indexes that together contain all the table columns that are referenced in the query. Won’t eliminate a sort operation Bitmap indexes Uses a bitmap for key values and a mapping function that converts each bit position to a rowid. Can efficiently merge indexes that correspond to several conditions in a WHERE clause
  • 26. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Access path example 1 Table products contains 10K rows & has a primary key on product_id SELECT product_id, name FROM products WHERE products_id IN (‘P123’,’P456’,’P789‘); What plan would you expect for this query? PRODUCTS PRODUCT_PK
  • 27. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Access path example 2 Table products contains 10K rows & has a primary key on product_id SELECT product_id, name FROM products WHERE product_id BETWEEN ‘P123' AND ‘P789'; What plan would you expect for this query? PRODUCTS PRODUCT_PK
  • 28. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Identifying elements of an execution plan - OPERATION OPERATION COLUMN SHOWS ACTUAL STEPS TAKEN TO EXECUTE THE PLAN INCLUDING: • DATA ACCESS METHODS • JOIN METHODS & TYPES • AGGREGATIONS
  • 29. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Join methods Join Methods Explanation Nested Loops joins For every row in the outer table, Oracle accesses all the rows in the inner table Useful when joining small subsets of data and there is an efficient way to access the second table (index look up) Hash Joins The smaller of two tables is scan and resulting rows are used to build 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 predicate Sort Merge joins Consists of two steps: 1. Sort join operation: Both the inputs are sorted on the join key. 2. Merge join operation: The sorted lists are merged together. Useful when the join condition between two tables is an inequality condition
  • 30. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Join types Join Type Explanation Cartesian Joins Joins every row from one data source with every row from the other 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 Outer Joins Returns all rows that satisfy the join condition and also returns all of the rows from the table without the (+) for which no rows from the other table satisfy the join condition
  • 31. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Join method example 1 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 What join method would you expect for this query?
  • 32. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Join method example 1 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; What join method would you expect for this query?
  • 33. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Identifying elements of an execution plan - CARDINALITY CARDINALITY IS THE ESTIMATED NUMBER OF ROWS RETURN BY EACH OPERATION IN THE PLAN AFTER PREDICATES HAVE BEEN APPLIED Determine correct cardinality using a SELECT COUNT(*) from each table applying any WHERE Clause predicates belonging to that table
  • 34. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Checking 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'));
  • 35. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Checking cardinality estimates SELECT * FROM table ( DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'ALLSTATS LAST')); Compare estimated number of rows returned with actual rows returned
  • 36. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Checking cardinality estimates for Parallel Execution SELECT * FROM table ( DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'ALLSTATS LAST')); Note: a lot of the data is zero in the A-rows column because we only show last executed cursor which is the QC. Need to use ALLSTATS ALL to see info on all parallel server cursors
  • 37. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Easiest way to compare the estimated number of rows returned with actual rows returned Check cardinality using SQL Monitor
  • 38. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Identifying elements of an execution plan - CARDINALITY AMOUNT OF DATA TO BE PROCESSED IN BYTES BY EACH OPERATION
  • 39. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Identifying elements of an execution plan - CARDINALITY COST REPRESENTS UNITS OF WORK OR RESOURCES (CPU & IO) USED TO PERFORM EACH OPERATION IN THE PLAN COST IS AN INTERNAL ORACLE MEASUREMENT AND SHOULD BE USED FOR COMPARISION PUROPOSES ONLY
  • 40. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Understanding how the Optimizer works Query Transformation Rewrite query text to allow it to be processed more efficiently Plan Generator Multiple plans are generated for each SQL, using different access paths and join types. Each plan is costed and plan with the lowest cost is used. Cost Estimator Cost is an estimate of the amount of CPU and the number of disk I/Os, used to perform an operation Optimizer Statistics Schema definitions
  • 41. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Best way to influence the Optimizer • Statistics • SQL Plan Management • SQL Patch • SQL Profile* • Hints * Requires Diagnostics Pack
  • 42. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | What are hints? • Hints allow you to influence the Optimizer when it has to choose between several possibilities • A hint is a directive that will be followed when applicable – Hints are only evaluated when they apply to a decision that has to be made • Can influence everything from the Optimizer mode used to each operation in the execution • Automatically means the Cost Based Optimizer will be used – Only exception is the RULE hint but it must be used alone • Use OPTIMIZER_IGNORE_HINTS parameter to test query performance without hints
  • 43. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Hints can influence all aspects of a plan • Most hints have corresponding negative hint preceded by word ‘NO_’ • More information on hints can be found in chapter 3 of SQL Reference Guide  Hints to influence cardinality • DYNAMIC_SAMPLING • CARDINALITY  Hints to influence join methods • USE_NL_WITH_INDEX • USE_HASH  Hints to influence access paths • FULL • INDEX  Hints to influence join order • LEADING • ORDERED
  • 44. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | How to use Optimizer hints • Hints are inserted in a SQL statement in the form of a comment with an additional + sign • They go immediately after the keyword (SELECT, INSERT, etc) SELECT /* this is a comment */ count(*) FROM sales; SELECT /*+ this is a comment */ count(*) FROM sales; Overview Note: Hint syntax is correct but it is not a valid hint so it is treated as comment
  • 45. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | If you can hint it, baseline it • Its not always possible to add hints to third party applications • Hints can be extremely difficult to manage over time • Once added never removed Alternative approach to hints Solution  Use SQL Plan Management (SPM)  Influence the execution plan without adding hints directly to queries  SPM available in both SE and EE, no additional options required
  • 46. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Summary • Learn to work with the Optimizer and not against it • Enjoy the challenge, knowing its only as smart as the folks that feed it information! • More information can be found on the Optimizer blog or Oracle.com – https://blogs.oracle.com/optimizer – https://www.oracle.com/technetwork/database/database-technologies/query- optimization/overview/index.html
  • 47. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | If you have more questions later, feel free to ask

Editor's Notes

  1. Copyright: <a href='https://www.123rf.com/profile_novelo'>novelo / 123RF Stock Photo</a>
  2. Example of features controlled by ofe is hash group by versus sort group by Different transformations also controlled by OFE
  3. In order for the Cost Based Optimizer to accurately determine the cost for an execution plan it must have information about all of the objects (tables and indexes) accessed in the SQL statement, and information about the system on which the SQL statement will be run. This necessary information is commonly referred to as Optimizer statistics. Understanding and managing Optimizer statistics is key to optimal SQL execution
  4. ORA 12012 Error on auto execute of job "SYS"."ORA$AT_OS_OPT_SY_<NN>
  5. Example of features controlled by ofe is hash group by versus sort group by Different transformations also controlled by OFE
  6. Full table reads all rows from a table and filters out those that do not meet the where clause predicates. Does multi block IO. Influenced by Value of init.ora parameter db_multi_block_read_count Parallel degree Lack of indexes Hints Typically selected if no indexes exist or the ones present cant be used Or if the cost is the lowest due to DOP or DBMBRC Rowid of a row specifies the datafile and data block containing the row and the location of the row in that block. Oracle first obtains the rowids either from the WHERE clause or through an index scan of one or more of the table's indexes. Oracle then locates each selected row in the table based on its rowid. With an Index unique scan only one row will be returned. It will be used When a statement contains a UNIQUE or a PRIMARY KEY constraint that guarantees that only a single row is accessed. An index range scan Oracle accesses adjacent index entries and then uses the ROWID values in the index to retrieve the table rows. It can be Bounded or unbounded. Data is returned in the ascending order of index columns. It will be used when a stmt has an equality predicate on non-unique index, or an incompletely specified unique index, or range predicate on unique index. (=, <, >,LIKE if not on leading edge) Uses index range scan descending when an order by descending clause can be satisfied by an index. Normally, in order for an index to be used, the columns defined on the leading edge of the index would be referenced in the query however, If all the other columns are referenced oracle will do an index skip scan to Skip the leading edge of the index and use the rest of it. Advantageous if there are few distinct values in the leading column of the composite index and many distinct values in the non-leading key of the index. A full scan does not read every block in the index structure, contrary to what its name suggests. An index full scan processes all of the leaf blocks of an index, but only enough of the branch blocks to find the first leaf block can be used because all of the columns necessary are in the index And it is cheaper than scanning the table and is used in any of the following situations: An ORDER BY clause has all of the index columns in it and the order is the same as in the index (can contain a subset of the columns in the index). The query requires a sort merge join & all of the columns referenced in the query are in the index. Order of the columns referenced in the query matches the order of the leading index columns. A GROUP BY clause is present in the query, and the columns in the GROUP BY clause are present in the index. A Fast full index scan is an alternative to a full table scan when the index c ontains all the columns that are needed for the query, and at least one column in the index key has the NOT NULL constraint. A fast full scan accesses all of the data in the index itself, without accessing the table. It cannot be used to eliminate a sort operation, because the data is not ordered by the index key. It reads the entire index using multiblock reads, unlike a full index scan, and can be parallelized. An index join is a hash join of several indexes that together contain all the table columns that are referenced in the query. If an index join is used, then no table access is needed, because all the relevant column values can be retrieved from the indexes. An index join cannot be used to eliminate a sort operation. A bitmap join uses a bitmap for key values and a mapping function that converts each bit position to a rowid. Bitmaps can efficiently merge indexes that correspond to several conditions in a WHERE clause, using Boolean operations to resolve AND and OR conditions.
  7. Nested loop joins are useful when small subsets of data are being joined and if the join condition is an efficient way of accessing the second table (index look up), That is the second table is dependent on the outer table (foreign key). For every row in the outer table, Oracle accesses all the rows in the inner table. Consider it Like two embedded for loops. Hash joins are used for joining large data sets. The optimizer uses the smaller of two tables or data sources to build a hash table on the join key in memory. It then scans the larger table, probing the hash table to find the joined rows. Hash joins selected If an equality predicate is present Partition wise join <see next two slides> Sort merge joins are useful when the join condition between two tables is an inequality condition (but not a nonequality) like <, <=, >, or >=. Sort merge joins perform better than nested loop joins for large data sets. The join consists of two steps: Sort join operation: Both the inputs are sorted on the join key. Merge join operation: The sorted lists are merged together.
  8. A Cartesian join is used when one or more of the tables does not have any join conditions to any other tables in the statement. The optimizer joins every row from one data source with every row from the other data source, creating the Cartesian product of the two sets. Only good if the tables involved are Small. Can be a sign of problems with cardinality. An outer join returns all rows that satisfy the join condition and also returns some or all of those rows from the table without the (+) for which no rows from the other satisfy the join condition. Take query: Select * from customers c, orders o WHERE c.credit_limit > 1000 AND c.customer_id = o.customer_id(+) The join preserves the customers rows, including those rows without a corresponding row in orders
  9. SQL Monitor does sampling so the overhead is far less than gather_plan_statisitcs
  10. Example of features controlled by ofe is hash group by versus sort group by Different transformations also controlled by OFE
  11. Hints allow you to influence the optimizer when it has to choose between several possibilities
  12. In this example the hint syntax is correct but the hint is not valid so it will be ignored and treated as a standard comment Statements that have two key words like INSERT as SELECT can have two set of hints specified