SlideShare a Scribd company logo
Master Product Manager
Oracle Database
February 2020
Maria Colgan
Understanding the Oracle Optimizer
@SQLMaria
Part 1
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 pricing of any features or functionality described for Oracle’s
products may change and remains at the sole discretion of Oracle Corporation.
2
Confidential – © 2020 Oracle
3
5
4
3
2
1
SQL Tuning
Harnessing the Power of Optimizer Hints
Explain the Explain Plan
Best Practices for Managing Optimizer Statistics
Understanding the Oracle Optimizer
Agenda
4
How the
Oracle
Optimizer
Operates
5
1979 – 1991
RIP
“ In the beginning
there were rules …”
6
• Rule Based Optimizer (RBO) is a heuristic based Optimizer
- Uses a ranked list of access paths (rules)
- 17 possible access paths
- Lower ranked access paths assumed to operate more efficiently
• Plans chosen based on access paths available and their rank
- If multiple access paths exist, path with the lowest rank is chosen
• Only very simple physical optimizations done automatically
- OR Expansion: multiple OR predicates rewritten as UNION ALL
Oracle Version 6 and earlier
Rule Based Optimizer
SELECT count(*)
FROM emp
WHERE ename || ’’ = ‘SMITH’;
• Only way to influence RBO was
to change the SQL text
• Concatenating an empty string
to the column prevents the
index from being used
Famous tricks to work around RBO
Got an index access but want a full table scan
Dawn of a new era:
“ .. there is cost ..
“
1992
• Database features become more and more complex
• Partitioning
• Parallel execution
• No easy way to extend Rule Based Optimizer to accommodate so
many additional access paths
• Having only one plan per statement regardless of the objects size or
structure was no longer the best approach
Oracle 7 - dawn of a new era
Cost Based Optimizer
Optimizer must evolve to become cost based
• 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 are generated for a statement
• Estimated cost is computed for each plan
• Optimizer selects the plan with the lowest estimated cost
Oracle 7 - dawn of a new era
Cost Based Optimizer
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
• Translates 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
OptimizerTransformations
Subquery Unnesting
*Compares the cost of the best plan with and without the transformation
SELECT C.cust_last_name, C.country_id
FROM customers C
WHERE EXISTS(SELECT 1
FROM sales S
WHERE C.cust_id=S.cust_id
AND S.quantity_sold > 1000);
• A correlated subquery is one that
refers to a column from a table
outside the subquery
• In this case C.cust_id is
referenced in the subquery
• Without subquery unnesting the
correlated subquery must be
evaluated for each row in the
Customers tables
After theTransformation
Subquery Unnesting
*Compares the cost of the best plan with and without the transformation
• Transformation rewrites the
EXISTS subquery to an ANY
subquery
• ANY subquery is no longer
correlated
• ANY subquery returns a set of
CUST_IDs if any match the
predicate will return true
SELECT C.cust_last_name, C.country_id
FROM customers C
WHERE C.cust_id = ANY(SELECT S.cust_id
FROM sales S
WHERE S.quantity_sold > 1000);
After theTransformation
Subquery Unnesting
*Compares the cost of the best plan with and without the transformation
• Transformation allows subquery to
be evaluated as a SEMI join
• Subquery returns a set of
CUST_IDs those
CUST_IDs are joined to the
customers table via a
SEMI Hash Join
• Complex view merging
refers to the merging of
group by and distinct views
• Allows the optimizer to
consider additional join
orders and access paths
• Group-by/distinct
operations can be delayed
until after the joins have
been evaluated
ComplexView Merging
CREATE View cust_prod_totals_v as
SELECT SUM(s.quantity_sold) total, s.cust_id, s.prod_id
FROM sales s
GROUP BY s.cust_id, s.prod_id;
SELECT c.cust_id, c.cust_first_name, c.cust_last_name
FROM customers c,
cust_prod_totals_v v,
products p
WHERE c.country_id = 'US'
AND c.cust_id =v.cust_id
AND v.total > 100
AND v.prod_id = p.prod_id
AND p.prod_name = 'T3 Faux Fur-Trimmed Sweater';
• After transformation
GROUP BY operation
occurs after SALES is joined
to CUSTOMERS and
PRODUCTS
• Number of rows in GROUP
BY greatly reduced after
join
• May not always be best to
delay the GROUP BY or
DISTINCT operation
After theTransformation
ComplexView Merging
SELECT c.cust_id, c.cust_first_name, c.cust_last_name
FROM customers c,
products p,
sales s
WHERE c.country_id = 'US'
AND c.cust_id =s.cust_id
AND s.prod_id = p.prod_id
AND p.prod_name = 'T3 Faux Fur-Trimmed Sweater’
GROUP BY s.cust_id, s.prod_id, s.cust_id, s.prod_id,
p.rowid, c.rowid, c.cust_last_name,
c.cust_first_name, c.cust_id
HAVING SUM(s.quantity_sold) > 100
;
• Without the transformation Optimizer
treats OR predicate as a single unit
• Can’t use index on either column
• Or Expansion transforms queries that
contain OR predicates into the form of
a UNION ALL query of two or more
branches
OR Expansion
SELECT *
FROM products p
WHERE prod_category ='Photo'
OR prod_subcategory ='Camera Media';
• 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
After theTransformation
OR Expansion
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')
;
Transformation allows an index access to be considered for each branch of the UNIONALL
OR Expansion
• Cost-based* transformation designed
to execute star queries more
efficiently
• Relies on bitmap indexes on foreign
key columns to access rows in the fact
table
• Controlled by parameter
STAR_TRANSFORMATION_ENABLED
StarTransformation
Sales
Fact
Table
Products
Dimension
Time
Dimension
Geography
Dimension
Suppliers
Dimension
Customers
Dimension
Star Schema - one or more large fact table
and many smaller dimension tables
*Compares the cost of the best plan with and without the transformation
• Traditionally a star query only defines
predicates on the dimension tables
• No efficient way to access rows in the
fact table
• By rewriting the query new access
paths become available on the fact
table
StarTransformation
*Compares the cost of the best plan with and without the transformation
SELECT c.cust_city, t.cal_quarter_desc,
SUM(s.amount_sold) sales_amt
FROM sales s, times t, customers c,
channels ch
WHERE s.time_id = t.time_id
AND s.cust_id = c.cust_id
AND s.channel_id = ch.channel_id
AND c.cust_state_province = 'CA'
AND ch.channel_desc = 'Internet'
AND t.calendar_quarter_desc IN ('2019-
04', '2020-01')
GROUP BY c.cust_city, t.cal_quarter_desc;
• Converts original query to
include 3 sub-queries on the
fact
After theTransformation
StarTransformation
SELECT c.cust_city, t.cal_quarter_desc,
SUM(s.amount_sold) sales_amt
FROM sales s, times t, customers c,
channels ch
WHERE s.time_id = t.time_id
AND s.cust_id = c.cust_id
AND s.channel_id = ch.channel_id
AND c.cust_state_province = 'CA'
AND ch.channel_desc = 'Internet'
AND t.calendar_quarter_desc IN ('2019-04', '2020-01’)
AND s.time_id IN (SELECT time_id
FROM times
WHERE cal_quarter_desc
IN('2019-01’,’2020-01’))
AND s.cust_id IN (SELECT cust_id
FROM customers
WHERE cust_state_province='CA’)
AND s.channel_id IN (SELECT channel_id
FROM channels
WHERE channel_desc = 'Internet')
GROUP BY c.cust_city, t.cal_quarter_desc;
--------------------------------------------------------------
| Id | Operation | Name |
--------------------------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | HASH GROUP BY | |
|* 2 | HASH JOIN | |
|* 3 | TABLE ACCESS FULL | CUSTOMERS |
|* 4 | HASH JOIN | |
|* 5 | TABLE ACCESS FULL | TIMES |
| 6 | VIEW | VW_ST_B1772830|
| 7 | NESTED LOOPS | |
| 8 | PARTITION RANGE SUBQUERY | |
| 9 | BITMAP CONVERSION TO ROWIDS | |
| 10 | BITMAP AND | |
| 11 | BITMAP MERGE | |
| 12 | BITMAP KEY ITERATION | |
| 13 | BUFFER SORT | |
|* 14 | TABLE ACCESS FULL | CHANNELS |
|* 15 | BITMAP INDEX RANGE SCAN | SALES_CHANNEL_BIX|
| 16 | BITMAP MERGE | |
| 17 | BITMAP KEY ITERATION | |
| 18 | BUFFER SORT | |
|* 19 | TABLE ACCESS FULL | TIMES |
|* 20 | BITMAP INDEX RANGE SCAN | SALES_TIME_BIX |
| 21 | BITMAP MERGE | |
| 22 | BITMAP KEY ITERATION | |
| 23 | BUFFER SORT | |
|* 24 | TABLE ACCESS FULL | CUSTOMERS |
|* 25 | BITMAP INDEX RANGE SCAN | SALES_CUST_BIX |
| 26 | TABLE ACCESS BY USER ROWID | SALES |
--------------------------------------------------------------
After theTransformation
StarTransformation
• Converts original query to include 3
sub-queries on the fact
• Fact table accessed first via bitmap
index and then joins out to dimension
tables
• Result of sub-queries may be saved in
temp tables
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
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
Go to PART 2 – Best Practices for Managing Optimizer Statistics
27
Related Information
• White paper on Cost-Based Query
Transformation in Oracle
http://dl.acm.org/citation.cfm?id=1164215
Join the Conversation
https://twitter.com/SQLMaria
https://blogs.oracle.com/optimizer/
https://sqlmaria.com
https://www.facebook.com/SQLMaria

More Related Content

What's hot

Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsOracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Zohar Elkayam
 
Understanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginnersUnderstanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginners
Carlos Sierra
 
Oracle_Multitenant_19c_-_All_About_Pluggable_D.pdf
Oracle_Multitenant_19c_-_All_About_Pluggable_D.pdfOracle_Multitenant_19c_-_All_About_Pluggable_D.pdf
Oracle_Multitenant_19c_-_All_About_Pluggable_D.pdf
SrirakshaSrinivasan2
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning Fundamentals
Carlos Sierra
 
AWR and ASH Deep Dive
AWR and ASH Deep DiveAWR and ASH Deep Dive
AWR and ASH Deep Dive
Kellyn Pot'Vin-Gorman
 
Survey of some free Tools to enhance your SQL Tuning and Performance Diagnost...
Survey of some free Tools to enhance your SQL Tuning and Performance Diagnost...Survey of some free Tools to enhance your SQL Tuning and Performance Diagnost...
Survey of some free Tools to enhance your SQL Tuning and Performance Diagnost...
Carlos Sierra
 
Ash architecture and advanced usage rmoug2014
Ash architecture and advanced usage rmoug2014Ash architecture and advanced usage rmoug2014
Ash architecture and advanced usage rmoug2014
John Beresniewicz
 
Part5 sql tune
Part5 sql tunePart5 sql tune
Part5 sql tune
Maria Colgan
 
SQL Plan Directives explained
SQL Plan Directives explainedSQL Plan Directives explained
SQL Plan Directives explained
Mauro Pagano
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning Fundamentals
Enkitec
 
SQL Tuning 101
SQL Tuning 101SQL Tuning 101
SQL Tuning 101
Carlos Sierra
 
Oracle Database Performance Tuning Concept
Oracle Database Performance Tuning ConceptOracle Database Performance Tuning Concept
Oracle Database Performance Tuning Concept
Chien Chung Shen
 
Understanding my database through SQL*Plus using the free tool eDB360
Understanding my database through SQL*Plus using the free tool eDB360Understanding my database through SQL*Plus using the free tool eDB360
Understanding my database through SQL*Plus using the free tool eDB360
Carlos Sierra
 
Oracle Performance Tools of the Trade
Oracle Performance Tools of the TradeOracle Performance Tools of the Trade
Oracle Performance Tools of the Trade
Carlos Sierra
 
Same plan different performance
Same plan different performanceSame plan different performance
Same plan different performance
Mauro Pagano
 
Understanding oracle rac internals part 2 - slides
Understanding oracle rac internals   part 2 - slidesUnderstanding oracle rac internals   part 2 - slides
Understanding oracle rac internals part 2 - slides
Mohamed Farouk
 
Chasing the optimizer
Chasing the optimizerChasing the optimizer
Chasing the optimizer
Mauro Pagano
 
Troubleshooting Complex Performance issues - Oracle SEG$ contention
Troubleshooting Complex Performance issues - Oracle SEG$ contentionTroubleshooting Complex Performance issues - Oracle SEG$ contention
Troubleshooting Complex Performance issues - Oracle SEG$ contention
Tanel Poder
 
AIOUG : OTNYathra - Troubleshooting and Diagnosing Oracle Database 12.2 and O...
AIOUG : OTNYathra - Troubleshooting and Diagnosing Oracle Database 12.2 and O...AIOUG : OTNYathra - Troubleshooting and Diagnosing Oracle Database 12.2 and O...
AIOUG : OTNYathra - Troubleshooting and Diagnosing Oracle Database 12.2 and O...
Sandesh Rao
 
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentalsDB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
John Beresniewicz
 

What's hot (20)

Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsOracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
 
Understanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginnersUnderstanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginners
 
Oracle_Multitenant_19c_-_All_About_Pluggable_D.pdf
Oracle_Multitenant_19c_-_All_About_Pluggable_D.pdfOracle_Multitenant_19c_-_All_About_Pluggable_D.pdf
Oracle_Multitenant_19c_-_All_About_Pluggable_D.pdf
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning Fundamentals
 
AWR and ASH Deep Dive
AWR and ASH Deep DiveAWR and ASH Deep Dive
AWR and ASH Deep Dive
 
Survey of some free Tools to enhance your SQL Tuning and Performance Diagnost...
Survey of some free Tools to enhance your SQL Tuning and Performance Diagnost...Survey of some free Tools to enhance your SQL Tuning and Performance Diagnost...
Survey of some free Tools to enhance your SQL Tuning and Performance Diagnost...
 
Ash architecture and advanced usage rmoug2014
Ash architecture and advanced usage rmoug2014Ash architecture and advanced usage rmoug2014
Ash architecture and advanced usage rmoug2014
 
Part5 sql tune
Part5 sql tunePart5 sql tune
Part5 sql tune
 
SQL Plan Directives explained
SQL Plan Directives explainedSQL Plan Directives explained
SQL Plan Directives explained
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning Fundamentals
 
SQL Tuning 101
SQL Tuning 101SQL Tuning 101
SQL Tuning 101
 
Oracle Database Performance Tuning Concept
Oracle Database Performance Tuning ConceptOracle Database Performance Tuning Concept
Oracle Database Performance Tuning Concept
 
Understanding my database through SQL*Plus using the free tool eDB360
Understanding my database through SQL*Plus using the free tool eDB360Understanding my database through SQL*Plus using the free tool eDB360
Understanding my database through SQL*Plus using the free tool eDB360
 
Oracle Performance Tools of the Trade
Oracle Performance Tools of the TradeOracle Performance Tools of the Trade
Oracle Performance Tools of the Trade
 
Same plan different performance
Same plan different performanceSame plan different performance
Same plan different performance
 
Understanding oracle rac internals part 2 - slides
Understanding oracle rac internals   part 2 - slidesUnderstanding oracle rac internals   part 2 - slides
Understanding oracle rac internals part 2 - slides
 
Chasing the optimizer
Chasing the optimizerChasing the optimizer
Chasing the optimizer
 
Troubleshooting Complex Performance issues - Oracle SEG$ contention
Troubleshooting Complex Performance issues - Oracle SEG$ contentionTroubleshooting Complex Performance issues - Oracle SEG$ contention
Troubleshooting Complex Performance issues - Oracle SEG$ contention
 
AIOUG : OTNYathra - Troubleshooting and Diagnosing Oracle Database 12.2 and O...
AIOUG : OTNYathra - Troubleshooting and Diagnosing Oracle Database 12.2 and O...AIOUG : OTNYathra - Troubleshooting and Diagnosing Oracle Database 12.2 and O...
AIOUG : OTNYathra - Troubleshooting and Diagnosing Oracle Database 12.2 and O...
 
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentalsDB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
 

Similar to Part1 of SQL Tuning Workshop - Understanding the Optimizer

Understanding DB2 Optimizer
Understanding DB2 OptimizerUnderstanding DB2 Optimizer
Understanding DB2 Optimizer
terraborealis
 
Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices I
Carlos Oliveira
 
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12cPresentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Ronald Francisco Vargas Quesada
 
Beginners guide to_optimizer
Beginners guide to_optimizerBeginners guide to_optimizer
Beginners guide to_optimizer
Maria Colgan
 
A Deep Dive into HPCM for Planning and Essbase Professionals
A Deep Dive into HPCM for Planning and Essbase ProfessionalsA Deep Dive into HPCM for Planning and Essbase Professionals
A Deep Dive into HPCM for Planning and Essbase Professionals
Alithya
 
Presentation interpreting execution plans for sql statements
Presentation    interpreting execution plans for sql statementsPresentation    interpreting execution plans for sql statements
Presentation interpreting execution plans for sql statements
xKinAnx
 
A Deep Dive into HPCM for Planning and Essbase Professionals
A Deep Dive into HPCM for Planning and Essbase ProfessionalsA Deep Dive into HPCM for Planning and Essbase Professionals
A Deep Dive into HPCM for Planning and Essbase Professionals
Alithya
 
QQ And Advance Query
QQ And Advance QueryQQ And Advance Query
QQ And Advance Query
Kai Liu
 
Oracle query optimizer
Oracle query optimizerOracle query optimizer
Oracle query optimizer
Smitha Padmanabhan
 
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptxFive_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Maria Colgan
 
Presentation v mware roi tco calculator
Presentation   v mware roi tco calculatorPresentation   v mware roi tco calculator
Presentation v mware roi tco calculator
solarisyourep
 
Explain the explain_plan
Explain the explain_planExplain the explain_plan
Explain the explain_plan
Maria Colgan
 
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdfNOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
cookie1969
 
Processes in Query Optimization in (ABMS) Advanced Database Management Systems
Processes in Query Optimization in (ABMS) Advanced Database Management Systems Processes in Query Optimization in (ABMS) Advanced Database Management Systems
Processes in Query Optimization in (ABMS) Advanced Database Management Systems
gamemaker762
 
Ps training mannual ( configuration )
Ps training mannual ( configuration )Ps training mannual ( configuration )
Ps training mannual ( configuration )
Soumya De
 
Revolutionise your Machine Learning Workflow using Scikit-Learn Pipelines
Revolutionise your Machine Learning Workflow using Scikit-Learn PipelinesRevolutionise your Machine Learning Workflow using Scikit-Learn Pipelines
Revolutionise your Machine Learning Workflow using Scikit-Learn Pipelines
Philip Goddard
 
Conversion Detail
Conversion DetailConversion Detail
Conversion Detail
Kiya Aliyar, PMP
 
Software archiecture lecture08
Software archiecture   lecture08Software archiecture   lecture08
Software archiecture lecture08
Luktalja
 
Apex and Virtual Private Database
Apex and Virtual Private DatabaseApex and Virtual Private Database
Apex and Virtual Private Database
Jeffrey Kemp
 
Simplifying the Complexity of Salesforce CPQ: Tips & Best Practices
Simplifying the Complexity of Salesforce CPQ: Tips & Best PracticesSimplifying the Complexity of Salesforce CPQ: Tips & Best Practices
Simplifying the Complexity of Salesforce CPQ: Tips & Best Practices
panayaofficial
 

Similar to Part1 of SQL Tuning Workshop - Understanding the Optimizer (20)

Understanding DB2 Optimizer
Understanding DB2 OptimizerUnderstanding DB2 Optimizer
Understanding DB2 Optimizer
 
Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices I
 
Presentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12cPresentación Oracle Database Migración consideraciones 10g/11g/12c
Presentación Oracle Database Migración consideraciones 10g/11g/12c
 
Beginners guide to_optimizer
Beginners guide to_optimizerBeginners guide to_optimizer
Beginners guide to_optimizer
 
A Deep Dive into HPCM for Planning and Essbase Professionals
A Deep Dive into HPCM for Planning and Essbase ProfessionalsA Deep Dive into HPCM for Planning and Essbase Professionals
A Deep Dive into HPCM for Planning and Essbase Professionals
 
Presentation interpreting execution plans for sql statements
Presentation    interpreting execution plans for sql statementsPresentation    interpreting execution plans for sql statements
Presentation interpreting execution plans for sql statements
 
A Deep Dive into HPCM for Planning and Essbase Professionals
A Deep Dive into HPCM for Planning and Essbase ProfessionalsA Deep Dive into HPCM for Planning and Essbase Professionals
A Deep Dive into HPCM for Planning and Essbase Professionals
 
QQ And Advance Query
QQ And Advance QueryQQ And Advance Query
QQ And Advance Query
 
Oracle query optimizer
Oracle query optimizerOracle query optimizer
Oracle query optimizer
 
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptxFive_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
 
Presentation v mware roi tco calculator
Presentation   v mware roi tco calculatorPresentation   v mware roi tco calculator
Presentation v mware roi tco calculator
 
Explain the explain_plan
Explain the explain_planExplain the explain_plan
Explain the explain_plan
 
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdfNOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
 
Processes in Query Optimization in (ABMS) Advanced Database Management Systems
Processes in Query Optimization in (ABMS) Advanced Database Management Systems Processes in Query Optimization in (ABMS) Advanced Database Management Systems
Processes in Query Optimization in (ABMS) Advanced Database Management Systems
 
Ps training mannual ( configuration )
Ps training mannual ( configuration )Ps training mannual ( configuration )
Ps training mannual ( configuration )
 
Revolutionise your Machine Learning Workflow using Scikit-Learn Pipelines
Revolutionise your Machine Learning Workflow using Scikit-Learn PipelinesRevolutionise your Machine Learning Workflow using Scikit-Learn Pipelines
Revolutionise your Machine Learning Workflow using Scikit-Learn Pipelines
 
Conversion Detail
Conversion DetailConversion Detail
Conversion Detail
 
Software archiecture lecture08
Software archiecture   lecture08Software archiecture   lecture08
Software archiecture lecture08
 
Apex and Virtual Private Database
Apex and Virtual Private DatabaseApex and Virtual Private Database
Apex and Virtual Private Database
 
Simplifying the Complexity of Salesforce CPQ: Tips & Best Practices
Simplifying the Complexity of Salesforce CPQ: Tips & Best PracticesSimplifying the Complexity of Salesforce CPQ: Tips & Best Practices
Simplifying the Complexity of Salesforce CPQ: Tips & Best Practices
 

More from Maria Colgan

Part4 Influencing Execution Plans with Optimizer Hints
Part4 Influencing Execution Plans with Optimizer HintsPart4 Influencing Execution Plans with Optimizer Hints
Part4 Influencing Execution Plans with Optimizer Hints
Maria Colgan
 
Part2 Best Practices for Managing Optimizer Statistics
Part2 Best Practices for Managing Optimizer StatisticsPart2 Best Practices for Managing Optimizer Statistics
Part2 Best Practices for Managing Optimizer Statistics
Maria Colgan
 
Ground Breakers Romania: Oracle Autonomous Database
Ground Breakers Romania: Oracle Autonomous DatabaseGround Breakers Romania: Oracle Autonomous Database
Ground Breakers Romania: Oracle Autonomous Database
Maria Colgan
 
Ground Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_planGround Breakers Romania: Explain the explain_plan
Ground Breakers Romania: Explain the explain_plan
Maria Colgan
 
What to Expect From Oracle database 19c
What to Expect From Oracle database 19cWhat to Expect From Oracle database 19c
What to Expect From Oracle database 19c
Maria Colgan
 
The Changing Role of a DBA in an Autonomous World
The Changing Role of a DBA in an Autonomous WorldThe Changing Role of a DBA in an Autonomous World
The Changing Role of a DBA in an Autonomous World
Maria Colgan
 
Oracle Database in-Memory Overivew
Oracle Database in-Memory OverivewOracle Database in-Memory Overivew
Oracle Database in-Memory Overivew
Maria Colgan
 
Useful PL/SQL Supplied Packages
Useful PL/SQL Supplied PackagesUseful PL/SQL Supplied Packages
Useful PL/SQL Supplied Packages
Maria Colgan
 
JSON and the Oracle Database
JSON and the Oracle DatabaseJSON and the Oracle Database
JSON and the Oracle Database
Maria Colgan
 
Five Tips to Get the Most Out of Your Indexing
Five Tips to Get the Most Out of Your IndexingFive Tips to Get the Most Out of Your Indexing
Five Tips to Get the Most Out of Your Indexing
Maria Colgan
 
Oracle optimizer bootcamp
Oracle optimizer bootcampOracle optimizer bootcamp
Oracle optimizer bootcamp
Maria Colgan
 
What_to_expect_from_oracle_database_12c
What_to_expect_from_oracle_database_12cWhat_to_expect_from_oracle_database_12c
What_to_expect_from_oracle_database_12c
Maria Colgan
 
Oracle database 12c_and_DevOps
Oracle database 12c_and_DevOpsOracle database 12c_and_DevOps
Oracle database 12c_and_DevOps
Maria Colgan
 

More from Maria Colgan (13)

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

Recently uploaded

Password Rotation in 2024 is still Relevant
Password Rotation in 2024 is still RelevantPassword Rotation in 2024 is still Relevant
Password Rotation in 2024 is still Relevant
Bert Blevins
 
Amul milk launches in US: Key details of its new products ...
Amul milk launches in US: Key details of its new products ...Amul milk launches in US: Key details of its new products ...
Amul milk launches in US: Key details of its new products ...
chetankumar9855
 
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
bhumivarma35300
 
Litestack talk at Brighton 2024 (Unleashing the power of SQLite for Ruby apps)
Litestack talk at Brighton 2024 (Unleashing the power of SQLite for Ruby apps)Litestack talk at Brighton 2024 (Unleashing the power of SQLite for Ruby apps)
Litestack talk at Brighton 2024 (Unleashing the power of SQLite for Ruby apps)
Muhammad Ali
 
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyyActive Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
RaminGhanbari2
 
Data Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining DataData Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining Data
Safe Software
 
The Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF GuideThe Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF Guide
Shiv Technolabs
 
The Evolution of Remote Server Management
The Evolution of Remote Server ManagementThe Evolution of Remote Server Management
The Evolution of Remote Server Management
Bert Blevins
 
find out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challengesfind out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challenges
huseindihon
 
“Deploying Large Language Models on a Raspberry Pi,” a Presentation from Usef...
“Deploying Large Language Models on a Raspberry Pi,” a Presentation from Usef...“Deploying Large Language Models on a Raspberry Pi,” a Presentation from Usef...
“Deploying Large Language Models on a Raspberry Pi,” a Presentation from Usef...
Edge AI and Vision Alliance
 
How to build a generative AI solution A step-by-step guide (2).pdf
How to build a generative AI solution A step-by-step guide (2).pdfHow to build a generative AI solution A step-by-step guide (2).pdf
How to build a generative AI solution A step-by-step guide (2).pdf
ChristopherTHyatt
 
Salesforce AI & Einstein Copilot Workshop
Salesforce AI & Einstein Copilot WorkshopSalesforce AI & Einstein Copilot Workshop
Salesforce AI & Einstein Copilot Workshop
CEPTES Software Inc
 
How RPA Help in the Transportation and Logistics Industry.pptx
How RPA Help in the Transportation and Logistics Industry.pptxHow RPA Help in the Transportation and Logistics Industry.pptx
How RPA Help in the Transportation and Logistics Industry.pptx
SynapseIndia
 
Using LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and MilvusUsing LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and Milvus
Zilliz
 
How Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdfHow Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdf
HackersList
 
Best Practices for Effectively Running dbt in Airflow.pdf
Best Practices for Effectively Running dbt in Airflow.pdfBest Practices for Effectively Running dbt in Airflow.pdf
Best Practices for Effectively Running dbt in Airflow.pdf
Tatiana Al-Chueyr
 
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdfWhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
ArgaBisma
 
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Torry Harris
 
Choose our Linux Web Hosting for a seamless and successful online presence
Choose our Linux Web Hosting for a seamless and successful online presenceChoose our Linux Web Hosting for a seamless and successful online presence
Choose our Linux Web Hosting for a seamless and successful online presence
rajancomputerfbd
 
July Patch Tuesday
July Patch TuesdayJuly Patch Tuesday
July Patch Tuesday
Ivanti
 

Recently uploaded (20)

Password Rotation in 2024 is still Relevant
Password Rotation in 2024 is still RelevantPassword Rotation in 2024 is still Relevant
Password Rotation in 2024 is still Relevant
 
Amul milk launches in US: Key details of its new products ...
Amul milk launches in US: Key details of its new products ...Amul milk launches in US: Key details of its new products ...
Amul milk launches in US: Key details of its new products ...
 
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
High Profile Girls call Service Pune 000XX00000 Provide Best And Top Girl Ser...
 
Litestack talk at Brighton 2024 (Unleashing the power of SQLite for Ruby apps)
Litestack talk at Brighton 2024 (Unleashing the power of SQLite for Ruby apps)Litestack talk at Brighton 2024 (Unleashing the power of SQLite for Ruby apps)
Litestack talk at Brighton 2024 (Unleashing the power of SQLite for Ruby apps)
 
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyyActive Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
Active Inference is a veryyyyyyyyyyyyyyyyyyyyyyyy
 
Data Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining DataData Integration Basics: Merging & Joining Data
Data Integration Basics: Merging & Joining Data
 
The Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF GuideThe Role of IoT in Australian Mobile App Development - PDF Guide
The Role of IoT in Australian Mobile App Development - PDF Guide
 
The Evolution of Remote Server Management
The Evolution of Remote Server ManagementThe Evolution of Remote Server Management
The Evolution of Remote Server Management
 
find out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challengesfind out more about the role of autonomous vehicles in facing global challenges
find out more about the role of autonomous vehicles in facing global challenges
 
“Deploying Large Language Models on a Raspberry Pi,” a Presentation from Usef...
“Deploying Large Language Models on a Raspberry Pi,” a Presentation from Usef...“Deploying Large Language Models on a Raspberry Pi,” a Presentation from Usef...
“Deploying Large Language Models on a Raspberry Pi,” a Presentation from Usef...
 
How to build a generative AI solution A step-by-step guide (2).pdf
How to build a generative AI solution A step-by-step guide (2).pdfHow to build a generative AI solution A step-by-step guide (2).pdf
How to build a generative AI solution A step-by-step guide (2).pdf
 
Salesforce AI & Einstein Copilot Workshop
Salesforce AI & Einstein Copilot WorkshopSalesforce AI & Einstein Copilot Workshop
Salesforce AI & Einstein Copilot Workshop
 
How RPA Help in the Transportation and Logistics Industry.pptx
How RPA Help in the Transportation and Logistics Industry.pptxHow RPA Help in the Transportation and Logistics Industry.pptx
How RPA Help in the Transportation and Logistics Industry.pptx
 
Using LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and MilvusUsing LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and Milvus
 
How Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdfHow Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdf
 
Best Practices for Effectively Running dbt in Airflow.pdf
Best Practices for Effectively Running dbt in Airflow.pdfBest Practices for Effectively Running dbt in Airflow.pdf
Best Practices for Effectively Running dbt in Airflow.pdf
 
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdfWhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
WhatsApp Image 2024-03-27 at 08.19.52_bfd93109.pdf
 
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
 
Choose our Linux Web Hosting for a seamless and successful online presence
Choose our Linux Web Hosting for a seamless and successful online presenceChoose our Linux Web Hosting for a seamless and successful online presence
Choose our Linux Web Hosting for a seamless and successful online presence
 
July Patch Tuesday
July Patch TuesdayJuly Patch Tuesday
July Patch Tuesday
 

Part1 of SQL Tuning Workshop - Understanding the Optimizer

  • 1. Master Product Manager Oracle Database February 2020 Maria Colgan Understanding the Oracle Optimizer @SQLMaria Part 1
  • 2. 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 pricing of any features or functionality described for Oracle’s products may change and remains at the sole discretion of Oracle Corporation. 2 Confidential – © 2020 Oracle
  • 3. 3 5 4 3 2 1 SQL Tuning Harnessing the Power of Optimizer Hints Explain the Explain Plan Best Practices for Managing Optimizer Statistics Understanding the Oracle Optimizer Agenda
  • 5. 5 1979 – 1991 RIP “ In the beginning there were rules …”
  • 6. 6 • Rule Based Optimizer (RBO) is a heuristic based Optimizer - Uses a ranked list of access paths (rules) - 17 possible access paths - Lower ranked access paths assumed to operate more efficiently • Plans chosen based on access paths available and their rank - If multiple access paths exist, path with the lowest rank is chosen • Only very simple physical optimizations done automatically - OR Expansion: multiple OR predicates rewritten as UNION ALL Oracle Version 6 and earlier Rule Based Optimizer
  • 7. SELECT count(*) FROM emp WHERE ename || ’’ = ‘SMITH’; • Only way to influence RBO was to change the SQL text • Concatenating an empty string to the column prevents the index from being used Famous tricks to work around RBO Got an index access but want a full table scan
  • 8. Dawn of a new era: “ .. there is cost .. “ 1992
  • 9. • Database features become more and more complex • Partitioning • Parallel execution • No easy way to extend Rule Based Optimizer to accommodate so many additional access paths • Having only one plan per statement regardless of the objects size or structure was no longer the best approach Oracle 7 - dawn of a new era Cost Based Optimizer Optimizer must evolve to become cost based
  • 10. • 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 are generated for a statement • Estimated cost is computed for each plan • Optimizer selects the plan with the lowest estimated cost Oracle 7 - dawn of a new era Cost Based Optimizer
  • 11. 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
  • 12. • Translates 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 OptimizerTransformations
  • 13. Subquery Unnesting *Compares the cost of the best plan with and without the transformation SELECT C.cust_last_name, C.country_id FROM customers C WHERE EXISTS(SELECT 1 FROM sales S WHERE C.cust_id=S.cust_id AND S.quantity_sold > 1000); • A correlated subquery is one that refers to a column from a table outside the subquery • In this case C.cust_id is referenced in the subquery • Without subquery unnesting the correlated subquery must be evaluated for each row in the Customers tables
  • 14. After theTransformation Subquery Unnesting *Compares the cost of the best plan with and without the transformation • Transformation rewrites the EXISTS subquery to an ANY subquery • ANY subquery is no longer correlated • ANY subquery returns a set of CUST_IDs if any match the predicate will return true SELECT C.cust_last_name, C.country_id FROM customers C WHERE C.cust_id = ANY(SELECT S.cust_id FROM sales S WHERE S.quantity_sold > 1000);
  • 15. After theTransformation Subquery Unnesting *Compares the cost of the best plan with and without the transformation • Transformation allows subquery to be evaluated as a SEMI join • Subquery returns a set of CUST_IDs those CUST_IDs are joined to the customers table via a SEMI Hash Join
  • 16. • Complex view merging refers to the merging of group by and distinct views • Allows the optimizer to consider additional join orders and access paths • Group-by/distinct operations can be delayed until after the joins have been evaluated ComplexView Merging CREATE View cust_prod_totals_v as SELECT SUM(s.quantity_sold) total, s.cust_id, s.prod_id FROM sales s GROUP BY s.cust_id, s.prod_id; SELECT c.cust_id, c.cust_first_name, c.cust_last_name FROM customers c, cust_prod_totals_v v, products p WHERE c.country_id = 'US' AND c.cust_id =v.cust_id AND v.total > 100 AND v.prod_id = p.prod_id AND p.prod_name = 'T3 Faux Fur-Trimmed Sweater';
  • 17. • After transformation GROUP BY operation occurs after SALES is joined to CUSTOMERS and PRODUCTS • Number of rows in GROUP BY greatly reduced after join • May not always be best to delay the GROUP BY or DISTINCT operation After theTransformation ComplexView Merging SELECT c.cust_id, c.cust_first_name, c.cust_last_name FROM customers c, products p, sales s WHERE c.country_id = 'US' AND c.cust_id =s.cust_id AND s.prod_id = p.prod_id AND p.prod_name = 'T3 Faux Fur-Trimmed Sweater’ GROUP BY s.cust_id, s.prod_id, s.cust_id, s.prod_id, p.rowid, c.rowid, c.cust_last_name, c.cust_first_name, c.cust_id HAVING SUM(s.quantity_sold) > 100 ;
  • 18. • Without the transformation Optimizer treats OR predicate as a single unit • Can’t use index on either column • Or Expansion transforms queries that contain OR predicates into the form of a UNION ALL query of two or more branches OR Expansion SELECT * FROM products p WHERE prod_category ='Photo' OR prod_subcategory ='Camera Media';
  • 19. • 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 After theTransformation OR Expansion 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') ;
  • 20. Transformation allows an index access to be considered for each branch of the UNIONALL OR Expansion
  • 21. • Cost-based* transformation designed to execute star queries more efficiently • Relies on bitmap indexes on foreign key columns to access rows in the fact table • Controlled by parameter STAR_TRANSFORMATION_ENABLED StarTransformation Sales Fact Table Products Dimension Time Dimension Geography Dimension Suppliers Dimension Customers Dimension Star Schema - one or more large fact table and many smaller dimension tables *Compares the cost of the best plan with and without the transformation
  • 22. • Traditionally a star query only defines predicates on the dimension tables • No efficient way to access rows in the fact table • By rewriting the query new access paths become available on the fact table StarTransformation *Compares the cost of the best plan with and without the transformation SELECT c.cust_city, t.cal_quarter_desc, SUM(s.amount_sold) sales_amt FROM sales s, times t, customers c, channels ch WHERE s.time_id = t.time_id AND s.cust_id = c.cust_id AND s.channel_id = ch.channel_id AND c.cust_state_province = 'CA' AND ch.channel_desc = 'Internet' AND t.calendar_quarter_desc IN ('2019- 04', '2020-01') GROUP BY c.cust_city, t.cal_quarter_desc;
  • 23. • Converts original query to include 3 sub-queries on the fact After theTransformation StarTransformation SELECT c.cust_city, t.cal_quarter_desc, SUM(s.amount_sold) sales_amt FROM sales s, times t, customers c, channels ch WHERE s.time_id = t.time_id AND s.cust_id = c.cust_id AND s.channel_id = ch.channel_id AND c.cust_state_province = 'CA' AND ch.channel_desc = 'Internet' AND t.calendar_quarter_desc IN ('2019-04', '2020-01’) AND s.time_id IN (SELECT time_id FROM times WHERE cal_quarter_desc IN('2019-01’,’2020-01’)) AND s.cust_id IN (SELECT cust_id FROM customers WHERE cust_state_province='CA’) AND s.channel_id IN (SELECT channel_id FROM channels WHERE channel_desc = 'Internet') GROUP BY c.cust_city, t.cal_quarter_desc;
  • 24. -------------------------------------------------------------- | Id | Operation | Name | -------------------------------------------------------------- | 0 | SELECT STATEMENT | | | 1 | HASH GROUP BY | | |* 2 | HASH JOIN | | |* 3 | TABLE ACCESS FULL | CUSTOMERS | |* 4 | HASH JOIN | | |* 5 | TABLE ACCESS FULL | TIMES | | 6 | VIEW | VW_ST_B1772830| | 7 | NESTED LOOPS | | | 8 | PARTITION RANGE SUBQUERY | | | 9 | BITMAP CONVERSION TO ROWIDS | | | 10 | BITMAP AND | | | 11 | BITMAP MERGE | | | 12 | BITMAP KEY ITERATION | | | 13 | BUFFER SORT | | |* 14 | TABLE ACCESS FULL | CHANNELS | |* 15 | BITMAP INDEX RANGE SCAN | SALES_CHANNEL_BIX| | 16 | BITMAP MERGE | | | 17 | BITMAP KEY ITERATION | | | 18 | BUFFER SORT | | |* 19 | TABLE ACCESS FULL | TIMES | |* 20 | BITMAP INDEX RANGE SCAN | SALES_TIME_BIX | | 21 | BITMAP MERGE | | | 22 | BITMAP KEY ITERATION | | | 23 | BUFFER SORT | | |* 24 | TABLE ACCESS FULL | CUSTOMERS | |* 25 | BITMAP INDEX RANGE SCAN | SALES_CUST_BIX | | 26 | TABLE ACCESS BY USER ROWID | SALES | -------------------------------------------------------------- After theTransformation StarTransformation • Converts original query to include 3 sub-queries on the fact • Fact table accessed first via bitmap index and then joins out to dimension tables • Result of sub-queries may be saved in temp tables
  • 25. 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
  • 26. 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 Go to PART 2 – Best Practices for Managing Optimizer Statistics
  • 27. 27 Related Information • White paper on Cost-Based Query Transformation in Oracle http://dl.acm.org/citation.cfm?id=1164215 Join the Conversation https://twitter.com/SQLMaria https://blogs.oracle.com/optimizer/ https://sqlmaria.com https://www.facebook.com/SQLMaria