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

Your tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
Your tuning arsenal: AWR, ADDM, ASH, Metrics and AdvisorsYour tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
Your tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
John Kanagaraj
 
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
 
Tanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools shortTanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools short
Tanel Poder
 
SQL Tuning, takes 3 to tango
SQL Tuning, takes 3 to tangoSQL Tuning, takes 3 to tango
SQL Tuning, takes 3 to tango
Mauro Pagano
 
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
Tanel Poder
 
Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle
Kyle Hailey
 
Part3 Explain the Explain Plan
Part3 Explain the Explain PlanPart3 Explain the Explain Plan
Part3 Explain the Explain Plan
Maria Colgan
 
Oracle Database SQL Tuning Concept
Oracle Database SQL Tuning ConceptOracle Database SQL Tuning Concept
Oracle Database SQL Tuning Concept
Chien Chung Shen
 
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
 
Standard Edition High Availability (SEHA) - The Why, What & How
Standard Edition High Availability (SEHA) - The Why, What & HowStandard Edition High Availability (SEHA) - The Why, What & How
Standard Edition High Availability (SEHA) - The Why, What & How
Markus Michalewicz
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Aaron Shilo
 
Oracle Real Application Clusters 19c- Best Practices and Internals- EMEA Tour...
Oracle Real Application Clusters 19c- Best Practices and Internals- EMEA Tour...Oracle Real Application Clusters 19c- Best Practices and Internals- EMEA Tour...
Oracle Real Application Clusters 19c- Best Practices and Internals- EMEA Tour...
Sandesh Rao
 
How to find what is making your Oracle database slow
How to find what is making your Oracle database slowHow to find what is making your Oracle database slow
How to find what is making your Oracle database slow
SolarWinds
 
Oracle GoldenGate 21c New Features and Best Practices
Oracle GoldenGate 21c New Features and Best PracticesOracle GoldenGate 21c New Features and Best Practices
Oracle GoldenGate 21c New Features and Best Practices
Bobby Curtis
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning Fundamentals
Enkitec
 
Oracle RAC 19c and Later - Best Practices #OOWLON
Oracle RAC 19c and Later - Best Practices #OOWLONOracle RAC 19c and Later - Best Practices #OOWLON
Oracle RAC 19c and Later - Best Practices #OOWLON
Markus Michalewicz
 
Understanding oracle rac internals part 1 - slides
Understanding oracle rac internals   part 1 - slidesUnderstanding oracle rac internals   part 1 - slides
Understanding oracle rac internals part 1 - slides
Mohamed Farouk
 
Tanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata MigrationsTanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata Migrations
Tanel Poder
 
Christo kutrovsky oracle, memory & linux
Christo kutrovsky   oracle, memory & linuxChristo kutrovsky   oracle, memory & linux
Christo kutrovsky oracle, memory & linux
Kyle Hailey
 
Sga internals
Sga internalsSga internals
Sga internals
sergkosko
 

What's hot (20)

Your tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
Your tuning arsenal: AWR, ADDM, ASH, Metrics and AdvisorsYour tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
Your tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
 
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
 
Tanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools shortTanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools short
 
SQL Tuning, takes 3 to tango
SQL Tuning, takes 3 to tangoSQL Tuning, takes 3 to tango
SQL Tuning, takes 3 to tango
 
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
 
Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle
 
Part3 Explain the Explain Plan
Part3 Explain the Explain PlanPart3 Explain the Explain Plan
Part3 Explain the Explain Plan
 
Oracle Database SQL Tuning Concept
Oracle Database SQL Tuning ConceptOracle Database SQL Tuning Concept
Oracle Database SQL Tuning Concept
 
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
 
Standard Edition High Availability (SEHA) - The Why, What & How
Standard Edition High Availability (SEHA) - The Why, What & HowStandard Edition High Availability (SEHA) - The Why, What & How
Standard Edition High Availability (SEHA) - The Why, What & How
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
 
Oracle Real Application Clusters 19c- Best Practices and Internals- EMEA Tour...
Oracle Real Application Clusters 19c- Best Practices and Internals- EMEA Tour...Oracle Real Application Clusters 19c- Best Practices and Internals- EMEA Tour...
Oracle Real Application Clusters 19c- Best Practices and Internals- EMEA Tour...
 
How to find what is making your Oracle database slow
How to find what is making your Oracle database slowHow to find what is making your Oracle database slow
How to find what is making your Oracle database slow
 
Oracle GoldenGate 21c New Features and Best Practices
Oracle GoldenGate 21c New Features and Best PracticesOracle GoldenGate 21c New Features and Best Practices
Oracle GoldenGate 21c New Features and Best Practices
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning Fundamentals
 
Oracle RAC 19c and Later - Best Practices #OOWLON
Oracle RAC 19c and Later - Best Practices #OOWLONOracle RAC 19c and Later - Best Practices #OOWLON
Oracle RAC 19c and Later - Best Practices #OOWLON
 
Understanding oracle rac internals part 1 - slides
Understanding oracle rac internals   part 1 - slidesUnderstanding oracle rac internals   part 1 - slides
Understanding oracle rac internals part 1 - slides
 
Tanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata MigrationsTanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata Migrations
 
Christo kutrovsky oracle, memory & linux
Christo kutrovsky   oracle, memory & linuxChristo kutrovsky   oracle, memory & linux
Christo kutrovsky oracle, memory & linux
 
Sga internals
Sga internalsSga internals
Sga internals
 

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
 
Presentation v mware roi tco calculator
Presentation   v mware roi tco calculatorPresentation   v mware roi tco calculator
Presentation v mware roi tco calculator
solarisyourep
 
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
 
Modernizing SQL Server the Right Way
Modernizing SQL Server the Right WayModernizing SQL Server the Right Way
Modernizing SQL Server the Right Way
Juan Fabian
 
My Experience Using Oracle SQL Plan Baselines 11g/12c
My Experience Using Oracle SQL Plan Baselines 11g/12cMy Experience Using Oracle SQL Plan Baselines 11g/12c
My Experience Using Oracle SQL Plan Baselines 11g/12c
Nelson Calero
 

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
 
Presentation v mware roi tco calculator
Presentation   v mware roi tco calculatorPresentation   v mware roi tco calculator
Presentation v mware roi tco calculator
 
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
 
Modernizing SQL Server the Right Way
Modernizing SQL Server the Right WayModernizing SQL Server the Right Way
Modernizing SQL Server the Right Way
 
My Experience Using Oracle SQL Plan Baselines 11g/12c
My Experience Using Oracle SQL Plan Baselines 11g/12cMy Experience Using Oracle SQL Plan Baselines 11g/12c
My Experience Using Oracle SQL Plan Baselines 11g/12c
 

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
 
Harnessing the Power of Optimizer Hints
Harnessing the Power of Optimizer HintsHarnessing the Power of Optimizer Hints
Harnessing the Power of Optimizer Hints
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 (14)

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

Recently uploaded

HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
Neo4j
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Neo4j
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
Fwdays
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
operationspcvita
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
Pablo Gómez Abajo
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
ScyllaDB
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
c5vrf27qcz
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
Edge AI and Vision Alliance
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
saastr
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 

Recently uploaded (20)

HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Artificial Intelligence and Electronic Warfare
Artificial Intelligence and Electronic WarfareArtificial Intelligence and Electronic Warfare
Artificial Intelligence and Electronic Warfare
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 

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