SlideShare a Scribd company logo
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 (20)

PDF
Analyzing and Interpreting AWR
pasalapudi
 
PDF
Understanding oracle rac internals part 2 - slides
Mohamed Farouk
 
PPTX
Understanding SQL Trace, TKPROF and Execution Plan for beginners
Carlos Sierra
 
PDF
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Aaron Shilo
 
PDF
SQL Monitoring in Oracle Database 12c
Tanel Poder
 
PPTX
Oracle sql high performance tuning
Guy Harrison
 
PDF
Same plan different performance
Mauro Pagano
 
PDF
Deep review of LMS process
Riyaj Shamsudeen
 
PDF
Tanel Poder - Scripts and Tools short
Tanel Poder
 
PDF
Oracle Performance Tuning Fundamentals
Enkitec
 
PPSX
Oracle Performance Tools of the Trade
Carlos Sierra
 
PDF
Oracle RAC 19c: Best Practices and Secret Internals
Anil Nair
 
PDF
AWR & ASH Analysis
aioughydchapter
 
PPT
Your tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
John Kanagaraj
 
PPTX
Part5 sql tune
Maria Colgan
 
PPT
Ash masters : advanced ash analytics on Oracle
Kyle Hailey
 
PDF
A deep dive about VIP,HAIP, and SCAN
Riyaj Shamsudeen
 
PDF
Redo internals ppt
Riyaj Shamsudeen
 
PPTX
Oracle AWR Data mining
Yury Velikanov
 
PDF
Troubleshooting Complex Performance issues - Oracle SEG$ contention
Tanel Poder
 
Analyzing and Interpreting AWR
pasalapudi
 
Understanding oracle rac internals part 2 - slides
Mohamed Farouk
 
Understanding SQL Trace, TKPROF and Execution Plan for beginners
Carlos Sierra
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Aaron Shilo
 
SQL Monitoring in Oracle Database 12c
Tanel Poder
 
Oracle sql high performance tuning
Guy Harrison
 
Same plan different performance
Mauro Pagano
 
Deep review of LMS process
Riyaj Shamsudeen
 
Tanel Poder - Scripts and Tools short
Tanel Poder
 
Oracle Performance Tuning Fundamentals
Enkitec
 
Oracle Performance Tools of the Trade
Carlos Sierra
 
Oracle RAC 19c: Best Practices and Secret Internals
Anil Nair
 
AWR & ASH Analysis
aioughydchapter
 
Your tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
John Kanagaraj
 
Part5 sql tune
Maria Colgan
 
Ash masters : advanced ash analytics on Oracle
Kyle Hailey
 
A deep dive about VIP,HAIP, and SCAN
Riyaj Shamsudeen
 
Redo internals ppt
Riyaj Shamsudeen
 
Oracle AWR Data mining
Yury Velikanov
 
Troubleshooting Complex Performance issues - Oracle SEG$ contention
Tanel Poder
 

Similar to Beginners guide to_optimizer (20)

PPTX
Explain the explain_plan
Maria Colgan
 
PDF
How to analyze and tune sql queries for better performance
oysteing
 
PPTX
05_DP_300T00A_Optimize.pptx
KareemBullard1
 
PPTX
Sql and PL/SQL Best Practices I
Carlos Oliveira
 
PDF
SQL for Analytics.pdfSQL for Analytics.pdf
namtunguyen6
 
PPTX
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Ronald Francisco Vargas Quesada
 
PPTX
Admin Guiding Query Plans
rsnarayanan
 
PDF
A Deep Dive into HPCM for Planning and Essbase Professionals
Alithya
 
PDF
Con7091 sql tuning for expert db as-oow17_oct2_1507314871265001m0x4
asifanw
 
PPTX
SQL Server 2008 Development for Programmers
Adam Hutson
 
PPTX
Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2
SolarWinds
 
PPTX
Web Cloud Computing SQL Server - Ferrara University
antimo musone
 
PDF
How to Analyze and Tune MySQL Queries for Better Performance
oysteing
 
PDF
How to Analyze and Tune MySQL Queries for Better Performance
oysteing
 
PPTX
Ssis Best Practices Israel Bi U Ser Group Itay Braun
sqlserver.co.il
 
PDF
Upcoming changes in MySQL 5.7
Morgan Tocker
 
PPTX
State ofdolphin short
Mandy Ang
 
PPTX
MySQL 8.0 in a nutshell
OracleMySQL
 
PDF
Optimizer overviewoow2014
Mysql User Camp
 
PDF
18. Madhur Hemnani - Result Orientated Innovation with Oracle HR Analytics
Cedar Consulting
 
Explain the explain_plan
Maria Colgan
 
How to analyze and tune sql queries for better performance
oysteing
 
05_DP_300T00A_Optimize.pptx
KareemBullard1
 
Sql and PL/SQL Best Practices I
Carlos Oliveira
 
SQL for Analytics.pdfSQL for Analytics.pdf
namtunguyen6
 
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Ronald Francisco Vargas Quesada
 
Admin Guiding Query Plans
rsnarayanan
 
A Deep Dive into HPCM for Planning and Essbase Professionals
Alithya
 
Con7091 sql tuning for expert db as-oow17_oct2_1507314871265001m0x4
asifanw
 
SQL Server 2008 Development for Programmers
Adam Hutson
 
Stop the Chaos! Get Real Oracle Performance by Query Tuning Part 2
SolarWinds
 
Web Cloud Computing SQL Server - Ferrara University
antimo musone
 
How to Analyze and Tune MySQL Queries for Better Performance
oysteing
 
How to Analyze and Tune MySQL Queries for Better Performance
oysteing
 
Ssis Best Practices Israel Bi U Ser Group Itay Braun
sqlserver.co.il
 
Upcoming changes in MySQL 5.7
Morgan Tocker
 
State ofdolphin short
Mandy Ang
 
MySQL 8.0 in a nutshell
OracleMySQL
 
Optimizer overviewoow2014
Mysql User Camp
 
18. Madhur Hemnani - Result Orientated Innovation with Oracle HR Analytics
Cedar Consulting
 
Ad

More from Maria Colgan (16)

PPTX
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Maria Colgan
 
PPTX
Part4 Influencing Execution Plans with Optimizer Hints
Maria Colgan
 
PPTX
Part3 Explain the Explain Plan
Maria Colgan
 
PPTX
Part2 Best Practices for Managing Optimizer Statistics
Maria Colgan
 
PPTX
Ground Breakers Romania: Oracle Autonomous Database
Maria Colgan
 
PPTX
Ground Breakers Romania: Explain the explain_plan
Maria Colgan
 
PPTX
What to Expect From Oracle database 19c
Maria Colgan
 
PPTX
The Changing Role of a DBA in an Autonomous World
Maria Colgan
 
PPTX
Oracle Database in-Memory Overivew
Maria Colgan
 
PPTX
Useful PL/SQL Supplied Packages
Maria Colgan
 
PPTX
JSON and the Oracle Database
Maria Colgan
 
PPTX
Five Tips to Get the Most Out of Your Indexing
Maria Colgan
 
PDF
Harnessing the Power of Optimizer Hints
Maria Colgan
 
PPTX
Oracle optimizer bootcamp
Maria Colgan
 
PPTX
What_to_expect_from_oracle_database_12c
Maria Colgan
 
PPTX
Oracle database 12c_and_DevOps
Maria Colgan
 
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Maria Colgan
 
Part4 Influencing Execution Plans with Optimizer Hints
Maria Colgan
 
Part3 Explain the Explain Plan
Maria Colgan
 
Part2 Best Practices for Managing Optimizer Statistics
Maria Colgan
 
Ground Breakers Romania: Oracle Autonomous Database
Maria Colgan
 
Ground Breakers Romania: Explain the explain_plan
Maria Colgan
 
What to Expect From Oracle database 19c
Maria Colgan
 
The Changing Role of a DBA in an Autonomous World
Maria Colgan
 
Oracle Database in-Memory Overivew
Maria Colgan
 
Useful PL/SQL Supplied Packages
Maria Colgan
 
JSON and the Oracle Database
Maria Colgan
 
Five Tips to Get the Most Out of Your Indexing
Maria Colgan
 
Harnessing the Power of Optimizer Hints
Maria Colgan
 
Oracle optimizer bootcamp
Maria Colgan
 
What_to_expect_from_oracle_database_12c
Maria Colgan
 
Oracle database 12c_and_DevOps
Maria Colgan
 
Ad

Recently uploaded (20)

PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 

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

  • #2: Copyright: <a href='https://www.123rf.com/profile_novelo'>novelo / 123RF Stock Photo</a>
  • #13: Example of features controlled by ofe is hash group by versus sort group by Different transformations also controlled by OFE
  • #14: 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
  • #17: ORA 12012 Error on auto execute of job "SYS"."ORA$AT_OS_OPT_SY_<NN>
  • #20: Example of features controlled by ofe is hash group by versus sort group by Different transformations also controlled by OFE
  • #26: 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.
  • #30: 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.
  • #31: 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
  • #38: SQL Monitor does sampling so the overhead is far less than gather_plan_statisitcs
  • #41: Example of features controlled by ofe is hash group by versus sort group by Different transformations also controlled by OFE
  • #43: Hints allow you to influence the optimizer when it has to choose between several possibilities
  • #45: 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