Professional Documents
Culture Documents
Oracle Database Optimizer Harnessing The Power of Optimizer Hints
Oracle Database Optimizer Harnessing The Power of Optimizer Hints
Program Agenda
What are hints? How to use Optimizer hints Useful Optimizer hints to know Why are Optimizer hints ignored? If you can hint it, baseline it Managing an existing hinted application
between several possibilities A hint is a directive that will be followed when applicable
Can influence everything from the Optimizer mode used to each
HINTS
10
MONITOR hint
11
Compare estimated rows returned for each operation in plan to actual rows returned
12 Copyright 2012, Oracle and/or its affiliates. All rights reserved.
Compare estimated rows returned for each operation in plan to actual rows returned
13 Copyright 2012, Oracle and/or its affiliates. All rights reserved.
14
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
15
Some transformations are always done but some are cost-based Hints can be used to influence the transformations the Optimizer does NO_QUERY_TRANSFORMATION MERGE USE_CONCAT REWRITE
STAR_TRANSFORMATION UNNEST
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
17 Copyright 2012, Oracle and/or its affiliates. All rights reserved.
Hints Classifications
Overview
Single-table - hints that are specified on one table or view
FULL , INDEX or USE_NL
18
Program Agenda
What are hints? How to use Optimizer hints Useful Optimizer hints to know Why are Optimizer hints ignored? If you can hint it, baseline it Managing an existing hinted application
19
additional + sign
They go immediately after the keyword (SELECT, INSERT, etc)
Hint syntax is correct but it is not a valid hint so it is ignored & treated as comment
20 Copyright 2012, Oracle and/or its affiliates. All rights reserved.
21
used in this query? Select /*+ index(scott.emp pk_emp) */ * From emp e; Select /*+ index(emp pk_emp) */ Select /*+ index(pk_emp) */ * From emp e; * From emp e;
None of them
22 Copyright 2012, Oracle and/or its affiliates. All rights reserved.
23
24
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) */
26
27
USED indicates the hint was used during the evaluation of the part of the plan it pertains to Doesnt mean the final plan will reflect it
29
31
Hint is valid and was used Why did it not change the plan? We only hinted the join method we didnt hint the join order Hint only valid when sales is on right side Hint considered when join order was customer, sales but not when it
34
35
36
37
Program Agenda
What are hints? How to use Optimizer hints Useful Optimizer hints to know Why are Optimizer hints ignored? If you can hint it, baseline it Managing an existing hinted application
39
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
41
Plan changed because the assumption is you are going to stop fetching after first 10 rows
42 Copyright 2012, Oracle and/or its affiliates. All rights reserved.
43
RULE hint is ignored because SALES is a partitioned table has to use CBO
44
RULE hint is used but prevents bitmap index from being used thus triggers a full table scan
45
specific query
Useful way to prevent setting non-default parameter value system-wide Only the following Optimizer influencing init.ora parameters can be set:
OPTIMIZER_DYNAMIC_SAMPLING OPTIMIZER_INDEX_CACHING OPTIMIZER_INDEX_COST_ADJ Optimizer related underscore parameters
46 Copyright 2012, Oracle and/or its affiliates. All rights reserved.
OPTIMIZER_USE_PENDING_STATISTICS
47
Extended statistics created & statistics re-gathered as pending statistics OPT_PARAM hint enables pending statistics for only this statement
48
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
49
50
Hash GROUP BY introduced in 10g not an option for 9.2 Optimizer so traditional sort based GROUP BY selected
51
Program Agenda
What are hints? How to use Optimizer hints Useful Optimizer hints to know Why are Optimizer hints ignored? If you can hint it, baseline it
52
Select /*+ index(e emp_pk) */ * From emp e; Select /*+ index(e pk_emp) */ * From emp e;
53
54
Illegal hint because a hash join cant be used for a non-equality join predicate
55
Invalid hint combination because an index range scan cant be parallelized on nonpartitioned table
56
Conflicting hints you cant do a full table scan and index lookup on same table
57
58
4 3 1 2
59
60
1 2 3
Inline View v
61
Program Agenda
What are hints? How to use Optimizer hints Useful Optimizer hints to know Why are Optimizer hints ignored? If you can hint it, baseline it Managing an existing hinted application
62
Solution
Use SQL Plan Management (SPM) Influence the execution plan without adding hints directly to queries SPM available in EE, no additional options required
63
Plan Acceptable
Execute
Users
HJ HJ
64
Users
NL NL
GB NL NL
Plan baseline
GB HJ HJ
65
Plan Acceptable
Execute
Users
HJ HJ
GB NL NL
Plan baseline
GB HJ HJ
66
Current Plan
67 Copyright 2012, Oracle and/or its affiliates. All rights reserved.
Desired Plan
68
69
70
71
72
73
74
75
Note section also confirms SQL plan baseline used for statement
77
Program Agenda
What are hints? How to use Optimizer hints Useful Optimizer hints to know Why are Optimizer hints ignored? If you can hint it, baseline it Managing an existing hinted application
78
Establish baseline
Execute workload
3
Set
_OPTIMIZER_IGNORE_HINTS Execute workload
80
Summary
Optimizer hints should only be used with extreme caution
Exhaust all other tuning mechanisms first Statistics, SQL Tuning Advisor, etc
without hints
81
82
83