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)

PPTX
What to Expect From Oracle database 19c
Maria Colgan
 
PDF
Performance Stability, Tips and Tricks and Underscores
Jitendra Singh
 
PDF
MySQL 8.0 Optimizer Guide
Morgan Tocker
 
PPTX
RESTful Services for your Oracle Autonomous Database
Jeff Smith
 
PPSX
Oracle Performance Tools of the Trade
Carlos Sierra
 
PDF
Scaling paypal workloads with oracle rac ss
Anil Nair
 
PDF
Oracle statistics by example
Mauro Pagano
 
PDF
The evolution of Apache Calcite and its Community
Julian Hyde
 
PPTX
AWR and ASH Deep Dive
Kellyn Pot'Vin-Gorman
 
PDF
Introduction To Liquibase
Knoldus Inc.
 
PDF
Zero Data Loss Recovery Applianceのご紹介
オラクルエンジニア通信
 
PPTX
Oracle Database Performance Tuning Basics
nitin anjankar
 
PPT
Oracle archi ppt
Hitesh Kumar Markam
 
PPTX
SQL Tuning, takes 3 to tango
Mauro Pagano
 
PPT
SAS Proc SQL
guest2160992
 
PPT
Sga internals
sergkosko
 
PPTX
New lessons in connection management
Toon Koppelaars
 
PDF
Oracle RAC 19c: Best Practices and Secret Internals
Anil Nair
 
PPTX
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Maria Colgan
 
PDF
Oracle db performance tuning
Simon Huang
 
What to Expect From Oracle database 19c
Maria Colgan
 
Performance Stability, Tips and Tricks and Underscores
Jitendra Singh
 
MySQL 8.0 Optimizer Guide
Morgan Tocker
 
RESTful Services for your Oracle Autonomous Database
Jeff Smith
 
Oracle Performance Tools of the Trade
Carlos Sierra
 
Scaling paypal workloads with oracle rac ss
Anil Nair
 
Oracle statistics by example
Mauro Pagano
 
The evolution of Apache Calcite and its Community
Julian Hyde
 
AWR and ASH Deep Dive
Kellyn Pot'Vin-Gorman
 
Introduction To Liquibase
Knoldus Inc.
 
Zero Data Loss Recovery Applianceのご紹介
オラクルエンジニア通信
 
Oracle Database Performance Tuning Basics
nitin anjankar
 
Oracle archi ppt
Hitesh Kumar Markam
 
SQL Tuning, takes 3 to tango
Mauro Pagano
 
SAS Proc SQL
guest2160992
 
Sga internals
sergkosko
 
New lessons in connection management
Toon Koppelaars
 
Oracle RAC 19c: Best Practices and Secret Internals
Anil Nair
 
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Maria Colgan
 
Oracle db performance tuning
Simon Huang
 

Similar to Beginners guide to_optimizer (20)

PDF
Managing Statistics for Optimal Query Performance
Karen Morton
 
PPTX
Oracle Query Optimizer - An Introduction
adryanbub
 
PPTX
Oracle performance tuning for java developers
Saeed Shahsavan
 
PPTX
Processes in Query Optimization in (ABMS) Advanced Database Management Systems
gamemaker762
 
PPTX
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Ronald Francisco Vargas Quesada
 
PDF
31063115_1679409488310Developer_Tuning_Tips_-_UTOUG_Mar_2023.pdf
TricantinoLopezPerez
 
PDF
Setting up the Oracle Optimizer for Proof of Concept Testing
Nigel Bayliss
 
PDF
Presentation interpreting execution plans for sql statements
xKinAnx
 
PPTX
Oracle 12c Optimizer IOUG
Connor McDonald
 
PPTX
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Guatemala User Group
 
PPT
PASS Summit 2010 Keynote David DeWitt
GraySystemsLab
 
PPTX
DB
Samchu Li
 
PPTX
Melbourne Groundbreakers Tour - Upgrading without risk
Connor McDonald
 
PPTX
Sangam 18 - The New Optimizer in Oracle 12c
Connor McDonald
 
PDF
Hailey_Database_Performance_Made_Easy_through_Graphics.pdf
cookie1969
 
PPTX
Oracle 12c Optimizer Overview - 2014
Connor McDonald
 
PPTX
Oracle performance tuning_sfsf
Mao Geng
 
PPTX
Explain the explain_plan
Maria Colgan
 
PPTX
MySQL Optimizer Overview
Olav Sandstå
 
PPTX
Sql and PL/SQL Best Practices I
Carlos Oliveira
 
Managing Statistics for Optimal Query Performance
Karen Morton
 
Oracle Query Optimizer - An Introduction
adryanbub
 
Oracle performance tuning for java developers
Saeed Shahsavan
 
Processes in Query Optimization in (ABMS) Advanced Database Management Systems
gamemaker762
 
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Ronald Francisco Vargas Quesada
 
31063115_1679409488310Developer_Tuning_Tips_-_UTOUG_Mar_2023.pdf
TricantinoLopezPerez
 
Setting up the Oracle Optimizer for Proof of Concept Testing
Nigel Bayliss
 
Presentation interpreting execution plans for sql statements
xKinAnx
 
Oracle 12c Optimizer IOUG
Connor McDonald
 
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Guatemala User Group
 
PASS Summit 2010 Keynote David DeWitt
GraySystemsLab
 
Melbourne Groundbreakers Tour - Upgrading without risk
Connor McDonald
 
Sangam 18 - The New Optimizer in Oracle 12c
Connor McDonald
 
Hailey_Database_Performance_Made_Easy_through_Graphics.pdf
cookie1969
 
Oracle 12c Optimizer Overview - 2014
Connor McDonald
 
Oracle performance tuning_sfsf
Mao Geng
 
Explain the explain_plan
Maria Colgan
 
MySQL Optimizer Overview
Olav Sandstå
 
Sql and PL/SQL Best Practices I
Carlos Oliveira
 
Ad

More from Maria Colgan (14)

PPTX
Part4 Influencing Execution Plans with Optimizer Hints
Maria Colgan
 
PPTX
Part2 Best Practices for Managing Optimizer Statistics
Maria Colgan
 
PPTX
Part1 of SQL Tuning Workshop - Understanding the Optimizer
Maria Colgan
 
PPTX
Ground Breakers Romania: Oracle Autonomous Database
Maria Colgan
 
PPTX
Ground Breakers Romania: Explain the explain_plan
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
 
Part4 Influencing Execution Plans with Optimizer Hints
Maria Colgan
 
Part2 Best Practices for Managing Optimizer Statistics
Maria Colgan
 
Part1 of SQL Tuning Workshop - Understanding the Optimizer
Maria Colgan
 
Ground Breakers Romania: Oracle Autonomous Database
Maria Colgan
 
Ground Breakers Romania: Explain the explain_plan
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
Understanding AI Optimization AIO, LLMO, and GEO
CoDigital
 
PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
PDF
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
PDF
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
PDF
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PPTX
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
PDF
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
PPTX
Smart Factory Monitoring IIoT in Machine and Production Operations.pptx
Rejig Digital
 
PDF
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
PDF
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
PDF
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
PDF
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
PPTX
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
Understanding AI Optimization AIO, LLMO, and GEO
CoDigital
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
Smart Factory Monitoring IIoT in Machine and Production Operations.pptx
Rejig Digital
 
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 

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