Documente Academic
Documente Profesional
Documente Cultură
BASEL BERN BRUGG LAUSANNE ZUERICH DUESSELDORF FRANKFURT A.M. FREIBURG I.BR. HAMBURG MUNICH STUTTGART VIENNA
2014 © Trivadis
1 Adaptive Query Optimization
28 September 2014
@ChrisAntognini
2014 © Trivadis
2 Adaptive Query Optimization
28 September 2014
Introduction
Over the years Oracle has extremely improved the capabilities of the query
optimizer
Most of the improvements fall into one of the following areas
Enhance the quality of the inputs (e.g. objects statistics)
Make the gathering and management of object statistics easier and more efficient
Use new data structures
Implement or enhance query transformations
Improve plan stability
Cope with poor estimations that leads to poor execution plans
2014 © Trivadis
3 Adaptive Query Optimization
28 September 2014
Adaptive Query Optimization Features
Adaptive statistics
SQL plan directives
Automatic reoptimization
- Statistics feedback (evolution of cardinality feedback)
- Performance feedback
Dynamic statistics (evolution of dynamic sampling)
2014 © Trivadis
4 Adaptive Query Optimization
28 September 2014
OPTIMIZER_ADAPTIVE_FEATURES
2014 © Trivadis
5 Adaptive Query Optimization
28 September 2014
OPTIMIZER_ADAPTIVE_REPORTING_ONLY
SELECT *
FROM table(dbms_xplan.display_cursor(format=>'report'))
2014 © Trivadis
6 Adaptive Query Optimization
28 September 2014
Adaptive Plans – Challenge
2014 © Trivadis
7 Adaptive Query Optimization
28 September 2014
Adaptive Plans – Concept
As of 12c, the query optimizer can postpone some decisions until the execution
phase
The idea is to leverage information collected while executing part of an
execution plan to determine how another part should be carried out
The query optimizer uses adaptive plans in three situations:
To switch the join method from a nested loops join to a hash join and vice versa
To switch the PX distribution method from hash to broadcast/round-robin
To disable the access to a dimension for execution plans using the star transformation
2014 © Trivadis
8 Adaptive Query Optimization
28 September 2014
Adaptive Plans – Join Method Switch
A new row source operation is used to partially buffer and count the rows
STATISTICS COLLECTOR
Limitation: if a too large buffer is required, no adaptive plan is used
The execution plan that is actually executed is called the final plan
2014 © Trivadis
9 Adaptive Query Optimization
28 September 2014
adaptive_plan.sql
Adaptive Plans – Join Method Switch Example
-----------------------------------------------
| Id | Operation | Name |
-----------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | HASH JOIN | |
| 2 | NESTED LOOPS | |
| 3 | NESTED LOOPS | |
| 4 | STATISTICS COLLECTOR | |
| 5 | TABLE ACCESS FULL | T1 |
| 6 | INDEX UNIQUE SCAN | T2_PK |
| 7 | TABLE ACCESS BY INDEX ROWID| T2 |
| 8 | TABLE ACCESS FULL | T2 |
-----------------------------------------------
2014 © Trivadis
10 Adaptive Query Optimization
28 September 2014
adaptive_plan.sql
Adaptive Plans – Join Method Switch Example
-----------------------------------------------
| Id | Operation | Name |
-----------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | HASH JOIN | |
| 2 | NESTED LOOPS | |
| 3 | NESTED LOOPS | |
| 4 | STATISTICS COLLECTOR | |
| 5 | TABLE ACCESS FULL | T1 |
| 6 | INDEX UNIQUE SCAN | T2_PK |
| 7 | TABLE ACCESS BY INDEX ROWID| T2 |
| 8 | TABLE ACCESS FULL | T2 |
-----------------------------------------------
2014 © Trivadis
11 Adaptive Query Optimization
28 September 2014
adaptive_plan.sql
Adaptive Plans – Join Method Switch Example
-----------------------------------------------
| Id | Operation | Name |
-----------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | HASH JOIN | |
| 2 | NESTED LOOPS | |
| 3 | NESTED LOOPS | |
| 4 | STATISTICS COLLECTOR | |
| 5 | TABLE ACCESS FULL | T1 |
| 6 | INDEX UNIQUE SCAN | T2_PK |
| 7 | TABLE ACCESS BY INDEX ROWID| T2 |
| 8 | TABLE ACCESS FULL | T2 |
-----------------------------------------------
2014 © Trivadis
12 Adaptive Query Optimization
28 September 2014
Adaptive Plans – Join Method Switch Inflection Point
For both join methods, the cost associated to different cardinalities is estimated
The cardinality of the outer table varies
The cardinality of the inner table remains fixed
2014 © Trivadis
13 Adaptive Query Optimization
28 September 2014
Adaptive Plans – Join Method Switch Inflection Point Example
20,000
2,000
Cost
20
Cardinality
(In Order of Execution)
2014 © Trivadis
14 Adaptive Query Optimization
28 September 2014
Adaptive Plans – Parallel Distribution Method
2014 © Trivadis
15 Adaptive Query Optimization
28 September 2014
hybrid_hash.sql
Adaptive Plans – Parallel Distribution Method Example
------------------------------------------------------------------------
| Operation | Name | TQ |IN-OUT| PQ Distrib |
------------------------------------------------------------------------
| SELECT STATEMENT | | | | |
| SORT AGGREGATE | | | | |
| PX COORDINATOR | | | | |
| PX SEND QC (RANDOM) | :TQ10002 | Q1,02 | P->S | QC (RAND) |
| SORT AGGREGATE | | Q1,02 | PCWP | |
| HASH JOIN | | Q1,02 | PCWP | |
| PX RECEIVE | | Q1,02 | PCWP | |
| PX SEND HYBRID HASH | :TQ10000 | Q1,00 | P->P | HYBRID HASH|
| STATISTICS COLLECTOR | | Q1,00 | PCWC | |
| PX BLOCK ITERATOR | | Q1,00 | PCWC | |
| TABLE ACCESS FULL | T1 | Q1,00 | PCWP | |
| PX RECEIVE | | Q1,02 | PCWP | |
| PX SEND HYBRID HASH | :TQ10001 | Q1,01 | P->P | HYBRID HASH|
| PX BLOCK ITERATOR | | Q1,01 | PCWC | |
| TABLE ACCESS FULL | T2 | Q1,01 | PCWP | |
------------------------------------------------------------------------
2014 © Trivadis
16 Adaptive Query Optimization
28 September 2014
Adaptive Plans – Star Transformation
With the star transformation, the data of each dimension that has a restriction
applied to it might be “joined” to the corresponding bitmap index of the fact
If the number of rowids returned by such a “join” is underestimated, applying the
filter can be detrimental to the performance
With an adaptive plan the access to some dimensions can be disabled
Decision takes place during the first execution only
2014 © Trivadis
17 Adaptive Query Optimization
28 September 2014
Adaptive Plans – Star Transformation Example
-----------------------------------------------------
| Operation | Name |
-----------------------------------------------------
| … | |
| VIEW | VW_ST_5497B905 |
| NESTED LOOPS | |
| BITMAP CONVERSION TO ROWIDS | |
| BITMAP AND | |
| BITMAP MERGE | |
| BITMAP KEY ITERATION | |
| TABLE ACCESS FULL | COLORS |
| BITMAP INDEX RANGE SCAN | CAR_COLOR_IDX |
| STATISTICS COLLECTOR | |
| BITMAP MERGE | |
| BITMAP KEY ITERATION | |
| TABLE ACCESS FULL | MODELS |
| BITMAP INDEX RANGE SCAN| CAR_MODEL_IDX |
| … | |
| TABLE ACCESS BY USER ROWID | CARS |
-----------------------------------------------------
2014 © Trivadis
18 Adaptive Query Optimization
28 September 2014
Adaptive Plans – V$SQL.IS_RESOLVED_ADAPTIVE_PLAN
NULL means that the execution plan associated to the cursor isn’t adaptive
N means that the final execution plan hasn’t been determined
Y means that the final execution plan was determined
Also set if reporting mode is enabled
2014 © Trivadis
19 Adaptive Query Optimization
28 September 2014
Adaptive Statistics – SQL Plan Directives
2014 © Trivadis
20 Adaptive Query Optimization
28 September 2014
Adaptive Statistics – Management of SQL Plan Directives
2014 © Trivadis
21 Adaptive Query Optimization
28 September 2014
Adaptive Statistics – SQL Plan Directives in the Data Dictionary
DBA_SQL_PLAN_DIRECTIVES
DBA_SQL_PLAN_DIR_OBJECTS
SQL> SELECT type, reason, count(*)
2 FROM dba_sql_plan_directives
3 GROUP BY type, reason;
2014 © Trivadis
22 Adaptive Query Optimization
28 September 2014
statistics_feedback1.sql
Adaptive Statistics – Statistics Feedback statistics_feedback2.sql
V$SQL.IS_REOPTIMIZABLE
Y means that the next execution will trigger a reoptimization
N means that no reoptimization is necessary
R means that reoptimization information is available, but reporting mode was enabled
2014 © Trivadis
23 Adaptive Query Optimization
28 September 2014
Adaptive Statistics – Performance Feedback
2014 © Trivadis
24 Adaptive Query Optimization
28 September 2014
dynamic_statistics.sql
Adaptive Statistics – Dynamic Statistics
2014 © Trivadis
25 Adaptive Query Optimization
28 September 2014
Summary
2014 © Trivadis
26 Adaptive Query Optimization
28 September 2014
Questions and answers ...
Christian Antognini
Senior Principal Consultant
christian.antognini@trivadis.com
BASEL BERN BRUGG LAUSANNE ZUERICH DUESSELDORF FRANKFURT A.M. FREIBURG I.BR. HAMBURG MUNICH STUTTGART VIENNA
2013 © Trivadis
2014
27 Adaptive Query Optimization
28 September 2014