SlideShare a Scribd company logo
1 of 62
Master Product Manager
MissionCritical Database Technologies
February 2020
Maria Colgan
Harnessing the Power of Optimizer Hints
@SQLMaria
PART 4
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.
Safe harbor statement
2
This session will not instantly make you an Optimizer hint expert!
Adding hints won’t magically improve every query you encounter
Expectations
Harnessing the power of Optimizer hints
Optimizer hints should only be used with extreme care
4
4
3
2
1 What are hints?
How to use Optimizer hints
Useful Optimizer hints to know
Why are Optimizer hints ignored?
Program Agenda
5
6
• Hints allow you to influence the Optimizer when it has to choose between several
possibilities
• A hint is a directive that will be followed when applicable
• Can influence everything fromOptimizer mode to every operation in plan
• Automatically means the Cost Based Optimizer will be used
• Only exception is the RULE hint, but it must be used alone
Overview
What are hints?
Example - directions to the mall
What are hints?
Should I walk or drive to the mall?
• Best plan would be to walk
Should I go up 4th, 5th, or 6th street?
• Best plan would be to go up 4th street
Should I go in the front or the back door of the mall?
• Best plan would be to go in the back door
Telling me the cheapest parking is at 5th and Mission garage is irrelevant
since I decided to walk
Hints only evaluated when they apply to a decision that has to be made
What are hints?
Optimizer Hints
8
Two different classes of hints
Non-Optimizer Hints
HINTS
The hint mechanism is not exclusively used by the Optimizer
Several other functional areas use hints too
• Direct path load can be controlled by APPEND hint
• Parallel statement queuing can be controlled by STATEMENT_QUEUING hint
• Data management in buffer cache can be influenced by CACHE hint
• What SQL statements get monitored by SQL Monitor can be controlled by MONITOR
hint
• Use of In-Memory column store can be controlled by INMEMORY hint
• Take advantage of new fast ingest for IoT controlled by MEMOPTIMIZE_WRITE
Overview
Not all hints influence the Optimizer
SELECT /*+ GATHER_PLAN_STATISTICS*/
p.prod_name,
SUM(s.quantity_sold)
FROM Products p, Sales s
WHERE s.prod_id = p.prod_id
GROUP BY p.prod_name;
GATHER_PLAN_STATISTICS hint
Checking cardinality estimates
SELECT * FROM table(
DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'ALLSTATS LAST’));
GATHER_PLAN_STATISTICS hint
Checking cardinality estimates
• Compare estimated rows returned for each operation in plan to actual
rows returned
• A-Time allows you to see where the time is spent
SELECT /*+ MONITOR*/ CUST_LASTNAME, SUM(AMOUNT_SOLD)
FROM Customers c, Sales s
WHERE s.cust_id = c.cust_id …
MONITOR hint
Checking cardinality estimates
• Compare estimated rows
returned for each operation
in plan to actual rows
returned
• Timeline allows you to see
where the time is spent
Optimizer Hints
13
Two different classes of hints
Non-Optimizer Hints
HINTS
Inside the Oracle Optimizer
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
Statistics
Schema definitions
First thing the Optimizer does is try to transform (rewrite) your statement
• The goal is to allow additional access or join methods and join orders to be used
Some transformations are always done but some are cost-based
Hints can be used to influence the transformations the Optimizer does
Overview
Hints influencing QueryTransformations
• REWRITE
• STAR_TRANSFORMATION
• UNNEST
• NO_QUERY_TRANSFORMATION
• MERGE
• USE_CONCAT
Most hints have corresponding negative hint preceded by word ‘NO_’
More information on hints can be found in chapter 3 of SQL Reference Guide &
chapter 19 of the SQLTuning Guide
Overview
Hints can also influence all aspects of a plan
Hints to influence cardinality
DYNAMIC_SAMPLING
CARDINALITY
Hints to influence join methods
USE_NL_WITH_INDEX
USE_HASH
Hints to influence access paths
FULL
INDEX
Hints to influence join order
LEADING
ORDERED
Single-table - hints that are specified on one table or view
• FULL , INDEX or USE_NL
Multi-table - hint that can be specified on one or more tables or views
• LEADING or ORDERED
Query block - hints that operate on single query blocks
• STAR_TRANSFORMATION or UNNEST
Statement – hints that apply to the entire SQL statement
• ALL_ROWS or OPTIMIZER_FEATURES_ENABLE
Overview
Hints Classifications
18
4
3
2
1 What are hints?
How to use Optimizer hints
Useful Optimizer hints to know
Why are Optimizer hints ignored?
Program Agenda
5
6
Hints are inserted in a SQL statement in the form of a comment with an additional +
sign
They go immediately after the keyword (SELECT, INSERT, etc)
SELECT /* This is a comment */ count(*) FROM Sales;
SELECT /*+ This is a hint */ count(*) FROM Sales;
Overview
How to use Optimizer hints
Hint syntax is correct, but it is not a valid hint so is treated as comment
Hints and comments can be combined
But best practice is to keep comment and hints in different blocks
• Comments can be put anywhere in a SQL statement not just after keyword
SELECT /*+ FULL(s) and a comment*/ count(*) FROM Sales s;
SELECT /*+ This_is_a_comment FULL(s) */ count(*) FROM Sales s;
Overview
How to use Optimizer hints
SELECT /*+ FULL(s)*/ count(*) FROM Sales s /* comment */;
Which one of the following hints will trigger the pk_emp index to be used in this
query?
SELECT /*+ index(scott.emp pk_emp)*/ * FROM emp e;
SELECT /*+ index(emp pk_emp)*/ * FROM emp e;
SELECT /*+ index(pk_emp)*/ * FROM emp e;
Correctly identifying the object in the hint
How to use Optimizer hints
None of them
If you use a table alias in the query than you must specify the table alias name in the
hint
Otherwise the hint is not valid
SELECT /*+ index(e pk_emp)*/ * FROM emp e;
Correctly identifying the object in the hint
How to use Optimizer hints
SELECT /*+ FULL(e) FULL(d) */ e.last_name, e.dept_id
FROM employees e
WHERE e.dept_id in (SELECT d.dept_id
FROM departments d
WHERE d.location_id=51);
Hints only apply to the query block in which they appear
How to use Optimizer hints
The dept table only appears in
the sub-query, which is treated
as separate query block.
Hint has no effect.
SELECT /*+ FULL(e) */ e.last_name, e.dept_id
FROM employees e
WHERE e.dept_id in (SELECT /*+ FULL(d) */ d.dept_id
FROM departments d
WHERE d.location_id=51);
Hints only apply to the query block in which they appear
How to use Optimizer hints
Only exception are statement level hints
The hint on dept now has an
effect as it appears in the correct
query block, the sub-query
Oracle automatically names each query block in a SQL statement
• sel$1, ins$2, upd$3, del$4, cri$5, mrg$6, set$7, misc$8
• Displayed using ‘+alias’ format parameter in DBMS_XPLAN procedures
Query block names can be used to specify which block a hint applies to
• /*+ FULL(@SEL$2 D) */
The QB_NAME hint can be used to explicitly labels each query block
• /*+ QB_NAME(my_name_for_block) */
Query block names
How to use Optimizer hints
SELECT /*+ FULL(e) FULL(@MY_SUBQ d) */
e.last_name, e.dept_id
FROM employees e
WHERE e.dept_id in (SELECT /*+ QB_NAME(MY_SUBQ) */
d.dept_id
FROM departments d
WHERE d.location_id=51);
Query block names
How to use Optimizer hints
Any valid hint will be used
Can check if a hint is valid in hint section of 10053 trace
How do I know if my hints are used or not?
How to use Optimizer hints
ERR indicates if there is
an error with hint
USED indicates the hint was used during the
evaluation of the part of the plan it pertains to
Doesn’t mean the final plan will reflect it
Example showing how hints are used
How to use Optimizer hints
SQL Statement
SELECT c.cust_name, sum (s.amount_sold)
FROM customers c, sales s
WHERE c.cust_id = s.cust_id
AND c.cust_city = ‘Los Angeles’
AND c.cust_province = ‘CA’
AND s.time_id = ‘09-SEP-18’
GROUP BY c.cust_name;
Default plan is a hash join between sales and customers
Example showing how hints are used
How to use Optimizer hints
But we want the query to use a nested loops join
Hinted SQL statement
SELECT /*+ USE_NL(s) */
c.cust_name, sum (s.amount_sold)
FROM customers c, sales s
WHERE c.cust_id = s.cust_id
AND c.cust_city = ‘Los Angeles’
AND c.cust_province = ‘CA’
AND s.time_id = ‘09-SEP-18’
GROUP BY c.cust_name;
Example showing how hints are used
How to use Optimizer hints
Even with the hint we still get a hash join plan
Example showing how hints are used
How to use Optimizer hints
Why did it not use the hint?
Let's look in the 10053 trace file
Hint is valid and was used
Why did it not change the plan?
We only hinted the join method we didn’t hint the join order
Hint only valid when sales is on right side
Hint considered ONLY when join order was customers, sales
Example showing how hints are used
How to use Optimizer hints
33
Example showing how hints are used
How to use Optimizer hints
New Unused hint info under
the plan in 19c with
DBMS_XPLAN.DISPLAY_CURSOR
1
4
3
535
535
64
256
138
9630
9630
Hinted SQL statement with both join method and join order hints
SELECT /*+ ORDERED USE_NL(s) */
c.cust_name, sum (s.amount_sold)
FROM customers c, sales s
WHERE c.cust_id = s.cust_id
AND c.cust_city = ‘Los Angeles’
AND c.cust_province = ‘CA’
AND s.time_id = ‘09-SEP-17’
GROUP BY c.cust_name;
Example showing how hints are used
How to use Optimizer hints
Hinted plan
Example showing how hints are used
How to use Optimizer hints
Partial hints can’t guarantee the same plan every time
Only way to guarantee the same plan every time is with a full outline
A full outline is a complete set of hints for all aspects of a plan
Full outline for a plan can be displayed using ‘+outline’ option with FORMAT
parameter in DBMS_XPLAN.DISPLAY_CURSOR
SELECT * FROM
TABLE(DBMS_XPLAN.display_cursor(format=>’+outline'));
Guaranteeing the same plan every time
How to use Optimizer hints
Guaranteeing the same plan every time
How to use Optimizer hints
Full outline for the plan
Guaranteeing the same plan every time
How to use Optimizer hints
Cut and paste full
outline for the plan
Easier to maintain a full outline using SQL Plan Management
39
4
3
2
1 What are hints?
How to use Optimizer hints
Useful Optimizer hints to know
Why are Optimizer hints ignored?
Program Agenda
5
6
The following hints control the Optimizer mode
• ALL_ROWS (default mode)
• FIRST_ROWS(n)
• RULE
FIRST_ROWS(n) choose plan that returns the first n rows most efficiently
• Use of old FIRST_ROWS hint is not recommended
- Not fully cost based
RULE hint reverts back to Rule Based Optimizer (RBO)
• Not recommended as RBO is de-supported and severely limits plan options
Changing the Optimizer mode
45% of the rows in the employee table have department_id = 50
Default plan is a full table scan
SELECT e.emp_id, e.last_name, e.salary
FROM employees e
WHERE e.dept_id = 50;
Default Optimizer mode example
Changing the Optimizer mode
SELECT /*+ FIRST_ROWS(10) */
e.emp_id, e.last_name, e.salary
FROM employees e
WHERE e.dept_id = 50;
Plan changed because the assumption is you are going to stop
fetching after first 10 rows
FIRST_ROWS(n) hint example
Changing the Optimizer mode
RULE hint specifies that the Rule Based Optimizer (RBO) be used
The RULE hint is not applicable if
• Other hints are specified in the stmt
• One or more tables are partitioned
• One or more IOTs are used
• One or more Materialized views exist
• A SAMPLE clauses is specified
• A spreadsheet clause is specified
RULE hint
Changing the Optimizer mode
• Parallel execution is used
• Grouping sets are used
• Group outer-join is used
• A create table with a parallel clause
• A left or full outer join (ANSI) is specified
• Flashback cursor (AS OF) is used
• ……….
RULE hint ignored when partitioned table is used
SELECT /*+ RULE */ count(*)
FROM sales s;
Rule hint
Changing the Optimizer mode
RULE hint is ignored because SALES is a
partitioned table therefore CBO is used
RULE hint prevents bitmap index being used and triggers full scan
SELECT /*+ RULE */ count(*)
FROM non_partitioned_sales s;
RULE hint
Changing the Optimizer mode
• Allows init.ora Optimizer parameters to be changed for a specific query
• Useful way to prevent setting non-default parameter value system-wide
• Only the following Optimizer influencing init.ora parameters can be set:
OPT_PARAM hint
Changing initialization parameter for a query
- OPTIMIZER_DYNAMIC_SAMPLING
- OPTIMIZER_INDEX_CACHING
- OPTIMIZER_INDEX_COST_ADJ
- OPTIMIZER_USE_PENDING_STATISTICS
- OPTIMIZER_USE_INVISIBLE_INDEXES
- OPTIMIZER_SECURE_VIEW_MERGING
- Optimizer related underscore parameters
- STAR_TRANSFORMATION_ENABLED
- PARALLEL_DEGREE_POLICY
- PARALLEL_DEGREE_LIMIT
- OPTIMIZER_ADAPTIVE_PLANS
- OPTIMIZER_ADAPTIVE_REPORTING_ONLY
- OPTIMIZER_ADAPTIVE_STATISTICS
- OPTIMIZER_INMEMORY_AWARE
OPT_PARAM hint example
Changing initialization parameter for a query
Cardinality under-estimated due
to complex expression
Extended statistics would help
OPT_PARAM hint example
Changing initialization parameter for a query
Create extended statistics &
re-gather statistics as pending
statistics
OPT_PARAM hint enables
pending statistics for only this
statement
OPTIMIZER_FEATURES_ENABLE parameter allows you to switch between
optimizer versions
Setting it to previous database version reverts the Optimizer to that version
• Disables any functionality that was not present in that version
Easy way to work around unexpected behavior in a new release
Hint allows you to revert the Optimizer for just a single statement
This parameter gets it own hint
Changing Optimizer features enable
Example
Changing Optimizer features enable
Example
Changing Optimizer features enable
Hash GROUP BY introduced in
10g not an option for 9.2
Optimizer, so traditional sort
based GROUP BY selected
52
4
3
2
1 What are hints?
How to use Optimizer hints
Useful Optimizer hints to know
Why are Optimizer hints ignored?
Program Agenda
5
6
Which one of the following hints will trigger the pk_emp index to be used
in this query?
SELECT /*+ IND(e pk_emp)*/ * FROM employees e;
SELECT /*+ INDEX(e emp_pk)*/ * FROM employees e;
SELECT /*+ INDEX(e pk_emp)*/ * FROM employees e;
Syntax and Spelling
Why are Optimizer hints ignored?
Specifying an index hint on a table with no indexes
SELECT /*+ INDEX(p) */ Count(*)
FROM my_promotions p
WHERE promo_category = 'TV'
AND promo_begin_date = '05-OCT-17’;
Invalid hint
Why are Optimizer hints ignored?
Invalid hint because no indexes
exist on the table
Specifying a hash join hint for non-equality join
SELECT /*+ USE_HASH(e s) */ e.first_name, e.last_name
FROM employees e, salary_grade s
WHERE e.salary BETWEEN s.low_sal AND s.high_sal;
Illegal hint
Why are Optimizer hints ignored?
Illegal hint because a hash join
can’t be used for a non-equality
join predicate
Specifying a parallel hint for an index range scan
SELECT /*+ index(e empno_pk_ind) parallel(e 8) */
e.empno, e.name
FROM employees e
WHERE e.empno < 7700;
Invalid hint combinations
Why are Optimizer hints ignored?
Invalid hint combination because an index
range scan can’t be parallelized on
non-partitioned table
If two hints contradict each other, they will both be ignored
SELECT /*+ full(e) index(e empno_fk_ind) */
e.empno, e.name
FROM employees e
WHERE e.empno < 7700;
Invalid hint combinations
Why are Optimizer hints ignored?
Conflicting hints you can’t do a
full table scan and index lookup
on same table
Ordered hint dictates the join order as the order of tables in FROM clause
SELECT /*+ ordered */ e1.last_name,
j.job_title, e1.salary, v.avg_salary
FROM employees e1, jobs j
( SELECT e2.dept_id, avg(e2.salary) avg_salary
FROM employees e2, departments d
WHERE d.location=1700 AND e2.depet_id=d.dept_id
GROUP BY e2.dept_id)v
WHERE e1.job_id = j.job_id
AND e1.dept_id = v.dept_id
AND e1.salary > a.avg_salary
ORDER BY e1.last_name;
Hint becomes invalid due to transformation
Why are Optimizer hints ignored?
Expected join order
1. Employees
2. Jobs
3. V
Actual join order used
Hint becomes invalid due to transformation
Why are Optimizer hints ignored?
1
3
2
View merging occurred
Order of tables in FROM clause
(e1,j,v) lost
Optimizer picks join order with
lowest cost
4
NO_MERGE hint prevents transformation from taking place
SELECT /*+ no_merge(v) ordered */
e1.last_name, j.job_title, e1.salary, v.avg_salary
FROM employees e1, jobs j
( SELECT e2.dept_id, avg(e2.salary) avg_salary
FROM employees e2, departments d
WHERE d.location=1700 AND e2.depet_id=d.dept_id
GROUP BY e2.dept_id)v
WHERE e1.job_id = j.job_id
AND e1.dept_id = v.dept_id
AND e1.salary > a.avg_salary
ORDER BY e1.last_name;
Hint becomes invalid due to transformation
Why are Optimizer hints ignored?
Preserves FROM clause order
Actual join order used
Hint becomes invalid due to transformation
Why are Optimizer hints ignored?
InlineView v
1
2
3
Optimizer hints should only be used with extreme caution
• Exhaust all other tuning mechanisms first
- Statistics, SQLTuning Advisor, etc.
To guarantee the same plan every time supply a complete outline
• Easier to do this with SQL Plan Management
Hints live forever!
• Use _OPTIMIZER_IGNORE_HINTS parameter to test query performance
without hints
Summary

More Related Content

What's hot

Explaining the explain_plan
Explaining the explain_planExplaining the explain_plan
Explaining the explain_planarief12H
 
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
 
Presentation top tips for getting optimal sql execution
Presentation    top tips for getting optimal sql executionPresentation    top tips for getting optimal sql execution
Presentation top tips for getting optimal sql executionxKinAnx
 
Advanced functions in PL SQL
Advanced functions in PL SQLAdvanced functions in PL SQL
Advanced functions in PL SQLHosein Zare
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007paulguerin
 
SQL Optimization With Trace Data And Dbms Xplan V6
SQL Optimization With Trace Data And Dbms Xplan V6SQL Optimization With Trace Data And Dbms Xplan V6
SQL Optimization With Trace Data And Dbms Xplan V6Mahesh Vallampati
 
Sydney Oracle Meetup - indexes
Sydney Oracle Meetup - indexesSydney Oracle Meetup - indexes
Sydney Oracle Meetup - indexespaulguerin
 
How mysql choose the execution plan
How mysql choose the execution planHow mysql choose the execution plan
How mysql choose the execution plan辛鹤 李
 
OBIEE 12c Advanced Analytic Functions
OBIEE 12c Advanced Analytic FunctionsOBIEE 12c Advanced Analytic Functions
OBIEE 12c Advanced Analytic FunctionsMichael Perhats
 
Online Statistics Gathering for ETL
Online Statistics Gathering for ETLOnline Statistics Gathering for ETL
Online Statistics Gathering for ETLAndrej Pashchenko
 
Sap Abap Reports
Sap Abap ReportsSap Abap Reports
Sap Abap Reportsvbpc
 
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
SQL Performance Solutions: Refactor Mercilessly, Index WiselySQL Performance Solutions: Refactor Mercilessly, Index Wisely
SQL Performance Solutions: Refactor Mercilessly, Index WiselyEnkitec
 

What's hot (20)

SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
 
Oracle SQL Advanced
Oracle SQL AdvancedOracle SQL Advanced
Oracle SQL Advanced
 
Explaining the explain_plan
Explaining the explain_planExplaining the explain_plan
Explaining the explain_plan
 
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
 
Presentation top tips for getting optimal sql execution
Presentation    top tips for getting optimal sql executionPresentation    top tips for getting optimal sql execution
Presentation top tips for getting optimal sql execution
 
Advanced functions in PL SQL
Advanced functions in PL SQLAdvanced functions in PL SQL
Advanced functions in PL SQL
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 
Project Report on SAP
Project Report on SAPProject Report on SAP
Project Report on SAP
 
SQL Optimization With Trace Data And Dbms Xplan V6
SQL Optimization With Trace Data And Dbms Xplan V6SQL Optimization With Trace Data And Dbms Xplan V6
SQL Optimization With Trace Data And Dbms Xplan V6
 
Sydney Oracle Meetup - indexes
Sydney Oracle Meetup - indexesSydney Oracle Meetup - indexes
Sydney Oracle Meetup - indexes
 
Do You Know The 11g Plan?
Do You Know The 11g Plan?Do You Know The 11g Plan?
Do You Know The 11g Plan?
 
advanced sql(database)
advanced sql(database)advanced sql(database)
advanced sql(database)
 
How mysql choose the execution plan
How mysql choose the execution planHow mysql choose the execution plan
How mysql choose the execution plan
 
Abap reports
Abap reportsAbap reports
Abap reports
 
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
Views, Triggers, Functions, Stored Procedures,  Indexing and JoinsViews, Triggers, Functions, Stored Procedures,  Indexing and Joins
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
 
OBIEE 12c Advanced Analytic Functions
OBIEE 12c Advanced Analytic FunctionsOBIEE 12c Advanced Analytic Functions
OBIEE 12c Advanced Analytic Functions
 
Online Statistics Gathering for ETL
Online Statistics Gathering for ETLOnline Statistics Gathering for ETL
Online Statistics Gathering for ETL
 
Sap Abap Reports
Sap Abap ReportsSap Abap Reports
Sap Abap Reports
 
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
SQL Performance Solutions: Refactor Mercilessly, Index WiselySQL Performance Solutions: Refactor Mercilessly, Index Wisely
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
 
11i Logs
11i Logs11i Logs
11i Logs
 

Similar to Part4 Influencing Execution Plans with Optimizer Hints

Admin Guiding Query Plans
Admin Guiding Query PlansAdmin Guiding Query Plans
Admin Guiding Query Plansrsnarayanan
 
Oracle Sql Tuning
Oracle Sql TuningOracle Sql Tuning
Oracle Sql TuningChris Adkin
 
Beginners guide to_optimizer
Beginners guide to_optimizerBeginners guide to_optimizer
Beginners guide to_optimizerMaria Colgan
 
Performance tuning
Performance tuningPerformance tuning
Performance tuningami111
 
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 ICarlos Oliveira
 
How to analyze and tune sql queries for better performance webinar
How to analyze and tune sql queries for better performance webinarHow to analyze and tune sql queries for better performance webinar
How to analyze and tune sql queries for better performance webinaroysteing
 
05_DP_300T00A_Optimize.pptx
05_DP_300T00A_Optimize.pptx05_DP_300T00A_Optimize.pptx
05_DP_300T00A_Optimize.pptxKareemBullard1
 
QQ and Advance query
QQ and Advance queryQQ and Advance query
QQ and Advance queryKai Liu
 
How to analyze and tune sql queries for better performance percona15
How to analyze and tune sql queries for better performance percona15How to analyze and tune sql queries for better performance percona15
How to analyze and tune sql queries for better performance percona15oysteing
 
Db performance optimization with indexing
Db performance optimization with indexingDb performance optimization with indexing
Db performance optimization with indexingRajeev Kumar
 
Enhancement technique how to use validations
Enhancement technique how to use validationsEnhancement technique how to use validations
Enhancement technique how to use validationsUgeshkumarnetha Dasari
 
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 statementsxKinAnx
 
Web Cloud Computing SQL Server - Ferrara University
Web Cloud Computing SQL Server  -  Ferrara UniversityWeb Cloud Computing SQL Server  -  Ferrara University
Web Cloud Computing SQL Server - Ferrara Universityantimo musone
 

Similar to Part4 Influencing Execution Plans with Optimizer Hints (20)

Admin Guiding Query Plans
Admin Guiding Query PlansAdmin Guiding Query Plans
Admin Guiding Query Plans
 
Oracle Sql Tuning
Oracle Sql TuningOracle Sql Tuning
Oracle Sql Tuning
 
Optimizer hint
Optimizer hintOptimizer hint
Optimizer hint
 
Optimizer Hints
Optimizer HintsOptimizer Hints
Optimizer Hints
 
Beginners guide to_optimizer
Beginners guide to_optimizerBeginners guide to_optimizer
Beginners guide to_optimizer
 
Performance tuning
Performance tuningPerformance tuning
Performance tuning
 
Oracle query optimizer
Oracle query optimizerOracle query optimizer
Oracle query 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
 
How to analyze and tune sql queries for better performance webinar
How to analyze and tune sql queries for better performance webinarHow to analyze and tune sql queries for better performance webinar
How to analyze and tune sql queries for better performance webinar
 
semantics_documentationsbn
semantics_documentationsbnsemantics_documentationsbn
semantics_documentationsbn
 
Teradata sql-tuning-top-10
Teradata sql-tuning-top-10Teradata sql-tuning-top-10
Teradata sql-tuning-top-10
 
05_DP_300T00A_Optimize.pptx
05_DP_300T00A_Optimize.pptx05_DP_300T00A_Optimize.pptx
05_DP_300T00A_Optimize.pptx
 
QQ and Advance query
QQ and Advance queryQQ and Advance query
QQ and Advance query
 
How to analyze and tune sql queries for better performance percona15
How to analyze and tune sql queries for better performance percona15How to analyze and tune sql queries for better performance percona15
How to analyze and tune sql queries for better performance percona15
 
Cbo100053
Cbo100053Cbo100053
Cbo100053
 
Db performance optimization with indexing
Db performance optimization with indexingDb performance optimization with indexing
Db performance optimization with indexing
 
Enhancement technique how to use validations
Enhancement technique how to use validationsEnhancement technique how to use validations
Enhancement technique how to use validations
 
Session 14 validation_steps_sap
Session 14 validation_steps_sapSession 14 validation_steps_sap
Session 14 validation_steps_sap
 
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
 
Web Cloud Computing SQL Server - Ferrara University
Web Cloud Computing SQL Server  -  Ferrara UniversityWeb Cloud Computing SQL Server  -  Ferrara University
Web Cloud Computing SQL Server - Ferrara University
 

More from Maria Colgan

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.pptxMaria Colgan
 
Ground Breakers Romania: Oracle Autonomous Database
Ground Breakers Romania: Oracle Autonomous DatabaseGround Breakers Romania: Oracle Autonomous Database
Ground Breakers Romania: Oracle Autonomous DatabaseMaria 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 19cMaria 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 WorldMaria Colgan
 
Oracle Database in-Memory Overivew
Oracle Database in-Memory OverivewOracle Database in-Memory Overivew
Oracle Database in-Memory OverivewMaria Colgan
 
JSON and the Oracle Database
JSON and the Oracle DatabaseJSON and the Oracle Database
JSON and the Oracle DatabaseMaria 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 IndexingMaria Colgan
 
Harnessing the Power of Optimizer Hints
Harnessing the Power of Optimizer HintsHarnessing the Power of Optimizer Hints
Harnessing the Power of Optimizer HintsMaria Colgan
 
Oracle optimizer bootcamp
Oracle optimizer bootcampOracle optimizer bootcamp
Oracle optimizer bootcampMaria 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_12cMaria Colgan
 
Oracle database 12c_and_DevOps
Oracle database 12c_and_DevOpsOracle database 12c_and_DevOps
Oracle database 12c_and_DevOpsMaria Colgan
 

More from Maria Colgan (11)

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
 
Ground Breakers Romania: Oracle Autonomous Database
Ground Breakers Romania: Oracle Autonomous DatabaseGround Breakers Romania: Oracle Autonomous Database
Ground Breakers Romania: Oracle Autonomous Database
 
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
 
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

Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 

Recently uploaded (20)

Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 

Part4 Influencing Execution Plans with Optimizer Hints

  • 1. Master Product Manager MissionCritical Database Technologies February 2020 Maria Colgan Harnessing the Power of Optimizer Hints @SQLMaria PART 4
  • 2. 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. Safe harbor statement 2
  • 3. This session will not instantly make you an Optimizer hint expert! Adding hints won’t magically improve every query you encounter Expectations Harnessing the power of Optimizer hints Optimizer hints should only be used with extreme care
  • 4. 4 4 3 2 1 What are hints? How to use Optimizer hints Useful Optimizer hints to know Why are Optimizer hints ignored? Program Agenda 5 6
  • 5. • Hints allow you to influence the Optimizer when it has to choose between several possibilities • A hint is a directive that will be followed when applicable • Can influence everything fromOptimizer mode to every operation in plan • Automatically means the Cost Based Optimizer will be used • Only exception is the RULE hint, but it must be used alone Overview What are hints?
  • 6. Example - directions to the mall What are hints?
  • 7. Should I walk or drive to the mall? • Best plan would be to walk Should I go up 4th, 5th, or 6th street? • Best plan would be to go up 4th street Should I go in the front or the back door of the mall? • Best plan would be to go in the back door Telling me the cheapest parking is at 5th and Mission garage is irrelevant since I decided to walk Hints only evaluated when they apply to a decision that has to be made What are hints?
  • 8. Optimizer Hints 8 Two different classes of hints Non-Optimizer Hints HINTS
  • 9. The hint mechanism is not exclusively used by the Optimizer Several other functional areas use hints too • Direct path load can be controlled by APPEND hint • Parallel statement queuing can be controlled by STATEMENT_QUEUING hint • Data management in buffer cache can be influenced by CACHE hint • What SQL statements get monitored by SQL Monitor can be controlled by MONITOR hint • Use of In-Memory column store can be controlled by INMEMORY hint • Take advantage of new fast ingest for IoT controlled by MEMOPTIMIZE_WRITE Overview Not all hints influence the Optimizer
  • 10. SELECT /*+ GATHER_PLAN_STATISTICS*/ p.prod_name, SUM(s.quantity_sold) FROM Products p, Sales s WHERE s.prod_id = p.prod_id GROUP BY p.prod_name; GATHER_PLAN_STATISTICS hint Checking cardinality estimates
  • 11. SELECT * FROM table( DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'ALLSTATS LAST’)); GATHER_PLAN_STATISTICS hint Checking cardinality estimates • Compare estimated rows returned for each operation in plan to actual rows returned • A-Time allows you to see where the time is spent
  • 12. SELECT /*+ MONITOR*/ CUST_LASTNAME, SUM(AMOUNT_SOLD) FROM Customers c, Sales s WHERE s.cust_id = c.cust_id … MONITOR hint Checking cardinality estimates • Compare estimated rows returned for each operation in plan to actual rows returned • Timeline allows you to see where the time is spent
  • 13. Optimizer Hints 13 Two different classes of hints Non-Optimizer Hints HINTS
  • 14. Inside the Oracle Optimizer 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 Statistics Schema definitions
  • 15. First thing the Optimizer does is try to transform (rewrite) your statement • The goal is to allow additional access or join methods and join orders to be used Some transformations are always done but some are cost-based Hints can be used to influence the transformations the Optimizer does Overview Hints influencing QueryTransformations • REWRITE • STAR_TRANSFORMATION • UNNEST • NO_QUERY_TRANSFORMATION • MERGE • USE_CONCAT
  • 16. Most hints have corresponding negative hint preceded by word ‘NO_’ More information on hints can be found in chapter 3 of SQL Reference Guide & chapter 19 of the SQLTuning Guide Overview Hints can also influence all aspects of a plan Hints to influence cardinality DYNAMIC_SAMPLING CARDINALITY Hints to influence join methods USE_NL_WITH_INDEX USE_HASH Hints to influence access paths FULL INDEX Hints to influence join order LEADING ORDERED
  • 17. Single-table - hints that are specified on one table or view • FULL , INDEX or USE_NL Multi-table - hint that can be specified on one or more tables or views • LEADING or ORDERED Query block - hints that operate on single query blocks • STAR_TRANSFORMATION or UNNEST Statement – hints that apply to the entire SQL statement • ALL_ROWS or OPTIMIZER_FEATURES_ENABLE Overview Hints Classifications
  • 18. 18 4 3 2 1 What are hints? How to use Optimizer hints Useful Optimizer hints to know Why are Optimizer hints ignored? Program Agenda 5 6
  • 19. Hints are inserted in a SQL statement in the form of a comment with an additional + sign They go immediately after the keyword (SELECT, INSERT, etc) SELECT /* This is a comment */ count(*) FROM Sales; SELECT /*+ This is a hint */ count(*) FROM Sales; Overview How to use Optimizer hints Hint syntax is correct, but it is not a valid hint so is treated as comment
  • 20. Hints and comments can be combined But best practice is to keep comment and hints in different blocks • Comments can be put anywhere in a SQL statement not just after keyword SELECT /*+ FULL(s) and a comment*/ count(*) FROM Sales s; SELECT /*+ This_is_a_comment FULL(s) */ count(*) FROM Sales s; Overview How to use Optimizer hints SELECT /*+ FULL(s)*/ count(*) FROM Sales s /* comment */;
  • 21. Which one of the following hints will trigger the pk_emp index to be used in this query? SELECT /*+ index(scott.emp pk_emp)*/ * FROM emp e; SELECT /*+ index(emp pk_emp)*/ * FROM emp e; SELECT /*+ index(pk_emp)*/ * FROM emp e; Correctly identifying the object in the hint How to use Optimizer hints None of them
  • 22. If you use a table alias in the query than you must specify the table alias name in the hint Otherwise the hint is not valid SELECT /*+ index(e pk_emp)*/ * FROM emp e; Correctly identifying the object in the hint How to use Optimizer hints
  • 23. SELECT /*+ FULL(e) FULL(d) */ e.last_name, e.dept_id FROM employees e WHERE e.dept_id in (SELECT d.dept_id FROM departments d WHERE d.location_id=51); Hints only apply to the query block in which they appear How to use Optimizer hints The dept table only appears in the sub-query, which is treated as separate query block. Hint has no effect.
  • 24. SELECT /*+ FULL(e) */ e.last_name, e.dept_id FROM employees e WHERE e.dept_id in (SELECT /*+ FULL(d) */ d.dept_id FROM departments d WHERE d.location_id=51); Hints only apply to the query block in which they appear How to use Optimizer hints Only exception are statement level hints The hint on dept now has an effect as it appears in the correct query block, the sub-query
  • 25. Oracle automatically names each query block in a SQL statement • sel$1, ins$2, upd$3, del$4, cri$5, mrg$6, set$7, misc$8 • Displayed using ‘+alias’ format parameter in DBMS_XPLAN procedures Query block names can be used to specify which block a hint applies to • /*+ FULL(@SEL$2 D) */ The QB_NAME hint can be used to explicitly labels each query block • /*+ QB_NAME(my_name_for_block) */ Query block names How to use Optimizer hints
  • 26. SELECT /*+ FULL(e) FULL(@MY_SUBQ d) */ e.last_name, e.dept_id FROM employees e WHERE e.dept_id in (SELECT /*+ QB_NAME(MY_SUBQ) */ d.dept_id FROM departments d WHERE d.location_id=51); Query block names How to use Optimizer hints
  • 27. Any valid hint will be used Can check if a hint is valid in hint section of 10053 trace How do I know if my hints are used or not? How to use Optimizer hints ERR indicates if there is an error with hint USED indicates the hint was used during the evaluation of the part of the plan it pertains to Doesn’t mean the final plan will reflect it
  • 28. Example showing how hints are used How to use Optimizer hints SQL Statement SELECT c.cust_name, sum (s.amount_sold) FROM customers c, sales s WHERE c.cust_id = s.cust_id AND c.cust_city = ‘Los Angeles’ AND c.cust_province = ‘CA’ AND s.time_id = ‘09-SEP-18’ GROUP BY c.cust_name;
  • 29. Default plan is a hash join between sales and customers Example showing how hints are used How to use Optimizer hints But we want the query to use a nested loops join
  • 30. Hinted SQL statement SELECT /*+ USE_NL(s) */ c.cust_name, sum (s.amount_sold) FROM customers c, sales s WHERE c.cust_id = s.cust_id AND c.cust_city = ‘Los Angeles’ AND c.cust_province = ‘CA’ AND s.time_id = ‘09-SEP-18’ GROUP BY c.cust_name; Example showing how hints are used How to use Optimizer hints
  • 31. Even with the hint we still get a hash join plan Example showing how hints are used How to use Optimizer hints Why did it not use the hint?
  • 32. Let's look in the 10053 trace file Hint is valid and was used Why did it not change the plan? We only hinted the join method we didn’t hint the join order Hint only valid when sales is on right side Hint considered ONLY when join order was customers, sales Example showing how hints are used How to use Optimizer hints
  • 33. 33 Example showing how hints are used How to use Optimizer hints New Unused hint info under the plan in 19c with DBMS_XPLAN.DISPLAY_CURSOR 1 4 3 535 535 64 256 138 9630 9630
  • 34. Hinted SQL statement with both join method and join order hints SELECT /*+ ORDERED USE_NL(s) */ c.cust_name, sum (s.amount_sold) FROM customers c, sales s WHERE c.cust_id = s.cust_id AND c.cust_city = ‘Los Angeles’ AND c.cust_province = ‘CA’ AND s.time_id = ‘09-SEP-17’ GROUP BY c.cust_name; Example showing how hints are used How to use Optimizer hints
  • 35. Hinted plan Example showing how hints are used How to use Optimizer hints
  • 36. Partial hints can’t guarantee the same plan every time Only way to guarantee the same plan every time is with a full outline A full outline is a complete set of hints for all aspects of a plan Full outline for a plan can be displayed using ‘+outline’ option with FORMAT parameter in DBMS_XPLAN.DISPLAY_CURSOR SELECT * FROM TABLE(DBMS_XPLAN.display_cursor(format=>’+outline')); Guaranteeing the same plan every time How to use Optimizer hints
  • 37. Guaranteeing the same plan every time How to use Optimizer hints Full outline for the plan
  • 38. Guaranteeing the same plan every time How to use Optimizer hints Cut and paste full outline for the plan Easier to maintain a full outline using SQL Plan Management
  • 39. 39 4 3 2 1 What are hints? How to use Optimizer hints Useful Optimizer hints to know Why are Optimizer hints ignored? Program Agenda 5 6
  • 40. The following hints control the Optimizer mode • ALL_ROWS (default mode) • FIRST_ROWS(n) • RULE FIRST_ROWS(n) choose plan that returns the first n rows most efficiently • Use of old FIRST_ROWS hint is not recommended - Not fully cost based RULE hint reverts back to Rule Based Optimizer (RBO) • Not recommended as RBO is de-supported and severely limits plan options Changing the Optimizer mode
  • 41. 45% of the rows in the employee table have department_id = 50 Default plan is a full table scan SELECT e.emp_id, e.last_name, e.salary FROM employees e WHERE e.dept_id = 50; Default Optimizer mode example Changing the Optimizer mode
  • 42. SELECT /*+ FIRST_ROWS(10) */ e.emp_id, e.last_name, e.salary FROM employees e WHERE e.dept_id = 50; Plan changed because the assumption is you are going to stop fetching after first 10 rows FIRST_ROWS(n) hint example Changing the Optimizer mode
  • 43. RULE hint specifies that the Rule Based Optimizer (RBO) be used The RULE hint is not applicable if • Other hints are specified in the stmt • One or more tables are partitioned • One or more IOTs are used • One or more Materialized views exist • A SAMPLE clauses is specified • A spreadsheet clause is specified RULE hint Changing the Optimizer mode • Parallel execution is used • Grouping sets are used • Group outer-join is used • A create table with a parallel clause • A left or full outer join (ANSI) is specified • Flashback cursor (AS OF) is used • ……….
  • 44. RULE hint ignored when partitioned table is used SELECT /*+ RULE */ count(*) FROM sales s; Rule hint Changing the Optimizer mode RULE hint is ignored because SALES is a partitioned table therefore CBO is used
  • 45. RULE hint prevents bitmap index being used and triggers full scan SELECT /*+ RULE */ count(*) FROM non_partitioned_sales s; RULE hint Changing the Optimizer mode
  • 46. • Allows init.ora Optimizer parameters to be changed for a specific query • Useful way to prevent setting non-default parameter value system-wide • Only the following Optimizer influencing init.ora parameters can be set: OPT_PARAM hint Changing initialization parameter for a query - OPTIMIZER_DYNAMIC_SAMPLING - OPTIMIZER_INDEX_CACHING - OPTIMIZER_INDEX_COST_ADJ - OPTIMIZER_USE_PENDING_STATISTICS - OPTIMIZER_USE_INVISIBLE_INDEXES - OPTIMIZER_SECURE_VIEW_MERGING - Optimizer related underscore parameters - STAR_TRANSFORMATION_ENABLED - PARALLEL_DEGREE_POLICY - PARALLEL_DEGREE_LIMIT - OPTIMIZER_ADAPTIVE_PLANS - OPTIMIZER_ADAPTIVE_REPORTING_ONLY - OPTIMIZER_ADAPTIVE_STATISTICS - OPTIMIZER_INMEMORY_AWARE
  • 47. OPT_PARAM hint example Changing initialization parameter for a query Cardinality under-estimated due to complex expression Extended statistics would help
  • 48. OPT_PARAM hint example Changing initialization parameter for a query Create extended statistics & re-gather statistics as pending statistics OPT_PARAM hint enables pending statistics for only this statement
  • 49. OPTIMIZER_FEATURES_ENABLE parameter allows you to switch between optimizer versions Setting it to previous database version reverts the Optimizer to that version • Disables any functionality that was not present in that version Easy way to work around unexpected behavior in a new release Hint allows you to revert the Optimizer for just a single statement This parameter gets it own hint Changing Optimizer features enable
  • 51. Example Changing Optimizer features enable Hash GROUP BY introduced in 10g not an option for 9.2 Optimizer, so traditional sort based GROUP BY selected
  • 52. 52 4 3 2 1 What are hints? How to use Optimizer hints Useful Optimizer hints to know Why are Optimizer hints ignored? Program Agenda 5 6
  • 53. Which one of the following hints will trigger the pk_emp index to be used in this query? SELECT /*+ IND(e pk_emp)*/ * FROM employees e; SELECT /*+ INDEX(e emp_pk)*/ * FROM employees e; SELECT /*+ INDEX(e pk_emp)*/ * FROM employees e; Syntax and Spelling Why are Optimizer hints ignored?
  • 54. Specifying an index hint on a table with no indexes SELECT /*+ INDEX(p) */ Count(*) FROM my_promotions p WHERE promo_category = 'TV' AND promo_begin_date = '05-OCT-17’; Invalid hint Why are Optimizer hints ignored? Invalid hint because no indexes exist on the table
  • 55. Specifying a hash join hint for non-equality join SELECT /*+ USE_HASH(e s) */ e.first_name, e.last_name FROM employees e, salary_grade s WHERE e.salary BETWEEN s.low_sal AND s.high_sal; Illegal hint Why are Optimizer hints ignored? Illegal hint because a hash join can’t be used for a non-equality join predicate
  • 56. Specifying a parallel hint for an index range scan SELECT /*+ index(e empno_pk_ind) parallel(e 8) */ e.empno, e.name FROM employees e WHERE e.empno < 7700; Invalid hint combinations Why are Optimizer hints ignored? Invalid hint combination because an index range scan can’t be parallelized on non-partitioned table
  • 57. If two hints contradict each other, they will both be ignored SELECT /*+ full(e) index(e empno_fk_ind) */ e.empno, e.name FROM employees e WHERE e.empno < 7700; Invalid hint combinations Why are Optimizer hints ignored? Conflicting hints you can’t do a full table scan and index lookup on same table
  • 58. Ordered hint dictates the join order as the order of tables in FROM clause SELECT /*+ ordered */ e1.last_name, j.job_title, e1.salary, v.avg_salary FROM employees e1, jobs j ( SELECT e2.dept_id, avg(e2.salary) avg_salary FROM employees e2, departments d WHERE d.location=1700 AND e2.depet_id=d.dept_id GROUP BY e2.dept_id)v WHERE e1.job_id = j.job_id AND e1.dept_id = v.dept_id AND e1.salary > a.avg_salary ORDER BY e1.last_name; Hint becomes invalid due to transformation Why are Optimizer hints ignored? Expected join order 1. Employees 2. Jobs 3. V
  • 59. Actual join order used Hint becomes invalid due to transformation Why are Optimizer hints ignored? 1 3 2 View merging occurred Order of tables in FROM clause (e1,j,v) lost Optimizer picks join order with lowest cost 4
  • 60. NO_MERGE hint prevents transformation from taking place SELECT /*+ no_merge(v) ordered */ e1.last_name, j.job_title, e1.salary, v.avg_salary FROM employees e1, jobs j ( SELECT e2.dept_id, avg(e2.salary) avg_salary FROM employees e2, departments d WHERE d.location=1700 AND e2.depet_id=d.dept_id GROUP BY e2.dept_id)v WHERE e1.job_id = j.job_id AND e1.dept_id = v.dept_id AND e1.salary > a.avg_salary ORDER BY e1.last_name; Hint becomes invalid due to transformation Why are Optimizer hints ignored? Preserves FROM clause order
  • 61. Actual join order used Hint becomes invalid due to transformation Why are Optimizer hints ignored? InlineView v 1 2 3
  • 62. Optimizer hints should only be used with extreme caution • Exhaust all other tuning mechanisms first - Statistics, SQLTuning Advisor, etc. To guarantee the same plan every time supply a complete outline • Easier to do this with SQL Plan Management Hints live forever! • Use _OPTIMIZER_IGNORE_HINTS parameter to test query performance without hints Summary

Editor's Notes

  1. This is a Safe Harbor Front slide, one of two Safe Harbor Statement slides included in this template. One of the Safe Harbor slides must be used if your presentation covers material affected by Oracle’s Revenue Recognition Policy To learn more about this policy, e-mail: Revrec-americasiebc_us@oracle.com For internal communication, Safe Harbor Statements are not required. However, there is an applicable disclaimer (Exhibit E) that should be used, found in the Oracle Revenue Recognition Policy for Future Product Communications. Copy and paste this link into a web browser, to find out more information.   http://my.oracle.com/site/fin/gfo/GlobalProcesses/cnt452504.pdf For all external communications such as press release, roadmaps, PowerPoint presentations, Safe Harbor Statements are required. You can refer to the link mentioned above to find out additional information/disclaimers required depending on your audience.
  2. Copyright: <a href='https://www.123rf.com/profile_3dmask'>3dmask / 123RF Stock Photo</a>
  3. Hints allow you to influence the optimizer when it has to choose between several possibilities
  4. Oracle Autonomous Database is actually a family of products with each member of the family optimized by workload. The first member of the family to become available in March of this year was the Autonomous Data Warehouse (ADW), which has been optimized for analytic workloads, such as data warehouse, data marts or as part of a data lake. The second member of the family that’s just become available is Autonomous Transaction Processing (ATP). ATP is optimized for transaction processing or mixed workload environments and makes an excellent platform for new application development.
  5. Oracle Autonomous Database is actually a family of products with each member of the family optimized by workload. The first member of the family to become available in March of this year was the Autonomous Data Warehouse (ADW), which has been optimized for analytic workloads, such as data warehouse, data marts or as part of a data lake. The second member of the family that’s just become available is Autonomous Transaction Processing (ATP). ATP is optimized for transaction processing or mixed workload environments and makes an excellent platform for new application development.
  6. Use_CONCAT is OR-Expansion to a Union ALL MERGE is for view merging UNNEST is for sub-query unnesting
  7. USE_NL only allows you to specify the table that should be used on the right-hand side of the NL join.
  8. In this example the hint syntax is correct but the hint is not valid so it will be ignored and treated as a standard comment Statements that have two key words like INSERT as SELECT can have two set of hints specified
  9. PARALLEL and ALL_ROWS are examples of a statement hint.
  10. While processing a SQL statement the parser checks the syntax of the hint. If there is an error with your hint it is considered a comment. There is no error raised. A correctly specified hint on the wrong object won’t be caught either.
  11. Specifying an index hint on a table that has no indexes
  12. Index range scan on a non-partitioned table can’t be parallelized
  13. Index range scan on a non-partitioned table can’t be parallelized