2013-Db-Christian Antognini-Query Optimizer 12c Was Ist Neu - Praesentation
2013-Db-Christian Antognini-Query Optimizer 12c Was Ist Neu - Praesentation
BASEL BERN BRUGG LAUSANNE ZUERICH DUESSELDORF FRANKFURT A.M. FREIBURG I.BR. HAMBURG MUNICH STUTTGART VIENNA
2013 © Trivadis
1 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
@ChrisAntognini
2013 © Trivadis
2 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
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 faster
Implement or enhance query transformations
Improve plan stability
Cope with poor estimations that leads to poor execution plans
2013 © Trivadis
3 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
AGENDA
2013 © Trivadis
4 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Adaptive Query Optimization
2013 © Trivadis
5 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Adaptive Plans – Challenge
2013 © Trivadis
6 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Adaptive Plans – Concept (1)
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 two situations:
To switch the join method from a nested loops join to a hash join
To switch the PX distribution method from hash to broadcast
2013 © Trivadis
7 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Adaptive Plans – Concept (2)
A subplan is chosen during the first execution based on the number of rows
actually processed
The query optimizer computes an inflection point
A new row source operation is used to partially buffer and count the rows
STATISTICS COLLECTOR
The selection of the final plan is only performed during the first execution
V$SQL.IS_RESOLVED_ADAPTIVE_PLAN
2013 © Trivadis
8 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
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 |
-----------------------------------------------
2013 © Trivadis
9 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
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 |
-----------------------------------------------
2013 © Trivadis
10 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
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 |
-----------------------------------------------
2013 © Trivadis
11 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Cardinality Feedback
With cardinality feedback, the query optimizer can take advantage of actual
cardinalities obtained by a previous execution
Not used for every SQL statement
Monitoring is only performed for the first execution of a given child cursor
When you rerun a statement you might get a different execution plan
Used only for single-table cardinalities
2013 © Trivadis
12 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
statistics_feedback1.sql
Statistics Feedback statistics_feedback2.sql
V$SQL.IS_REOPTIMIZABLE
2013 © Trivadis
13 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Dynamic Statistics
2013 © Trivadis
15 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
SQL Plan Directives (1)
2013 © Trivadis
16 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
statistics_feedback2.sql
SQL Plan Directives (2)
They are not tied to a specific SQL statement, but to a specific column
They instruct the database engine to automatically create extended statistics
If creating extended statistics is not supported/possible, they instruct the query
optimizer to use dynamic sampling
2013 © Trivadis
17 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
OPTIMIZER_ADAPTIVE_FEATURES Initialization Parameter
2013 © Trivadis
18 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
OPTIMIZER_ADAPTIVE_REPORTING_ONLY Initialization
Parameter
Enables or disables reporting mode for adaptive query optimization features
Useful to assess how execution plans would change
Use DBMS_XPLAN to get detail information about the analysis
Might fail with an ORA-1001 (bug 17270605)
SELECT *
FROM table(dbms_xplan.display_cursor(format=>'report'))
2013 © Trivadis
19 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Object Statistics
2013 © Trivadis
20 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Frequency Histograms
Precise estimations
Limited number of
distinct values (254)
2013 © Trivadis
21 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Height-Balanced Histograms
Only used when a frequency histogram cannot be build because of the limited
number of buckets
Precision highly dependent on data skewing
It is all about popular values (i.e. values associated to several buckets)
Sometimes misleading
When histograms are re-gathered they can lead to instability in estimations
2013 © Trivadis
22 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Histograms – 12c New Features
2013 © Trivadis
23 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Top-Frequency Histograms
Similar to frequency histograms, but only the top-n values are stored
Conditions
Number of distinct values larger than n
Top-n values account for at least x percent of the rows
- x = 100 – 100 / n
ESTIMATE_PERCENT must be set to AUTO_SAMPLE_SIZE
2013 © Trivadis
24 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Hybrid Histograms
2013 © Trivadis
25 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Hybrid Histograms – Example (1)
2013 © Trivadis
26 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Hybrid Histograms – Example (2)
Source: Oracle Database 12c – SQL Tuning Guide, Oracle Corporation, Mai 2013
2013 © Trivadis
27 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Hybrid Histograms – Example (3)
Source: Oracle Database 12c – SQL Tuning Guide, Oracle Corporation, Mai 2013
2013 © Trivadis
28 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Hybrid Histograms – Example (4)
Source: Oracle Database 12c – SQL Tuning Guide, Oracle Corporation, Mai 2013
2013 © Trivadis
29 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Session-Specific Object Statistics for Global Temporary Table
2013 © Trivadis
30 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
online_statistics.sql
Online Object Statistics Gathering for Bulk Load Operations
Table and column statistics are automatically gathered during the following
operations:
CTAS statement
Direct-path insert into an empty table (except with APPEND_VALUES hint)
2013 © Trivadis
31 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Enhancements to Incremental Statistics
2013 © Trivadis
32 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
DBMS_STATS Reporting Mode
A new set of functions can be used to list the objects that would be processed
during the gathering of object statistics
REPORT_GATHER_AUTO_STATS
REPORT_GATHER_DATABASE_STATS
REPORT_GATHER_DICTIONARY_STATS
REPORT_GATHER_FIXED_OBJ_STATS
REPORT_GATHER_SCHEMA_STATS
REPORT_GATHER_TABLE_STATS
2013 © Trivadis
33 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
DBMS_STATS Reporting Mode – Parameters
2013 © Trivadis
34 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Logging of Management Operations
2013 © Trivadis
35 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Example: Which Parameters Were Used by the Default Gathering
Job During the Last 24 Hours?
SQL> SELECT x.*
2 FROM dba_optstat_operations o,
3 XMLTable('/params/param'
4 PASSING XMLType(notes)
5 COLUMNS name VARCHAR2(20) PATH '@name',
6 value VARCHAR2(30) PATH '@val') x
7 WHERE start_time > systimestamp - INTERVAL '1' DAY
8 AND operation = 'gather_database_stats (auto)';
NAME VALUE
---------------- --------------------------
block_sample FALSE
cascade NULL
…
2013 © Trivadis
36 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
optstat_reports.sql
Reports on Management Operations
Two functions are provided to avoid manual queries against the log
Example 1: List operations that took place between two timestamps
dbms_stats.report_stats_operations(detail_level => 'typical',
format => 'text',
since => sysdate-1,
until => sysdate)
2013 © Trivadis
37 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Plan Stability
2013 © Trivadis
38 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
SQL Plan Management Evolve Advisor
2013 © Trivadis
39 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
spm_evolve_advisor_log.sql
SQL Plan Management Evolve Advisor – Reporting
2013 © Trivadis
40 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
SQL Plan Management and Unreproducible Execution Plans
To avoid such a problem, from 12c onward the execution plan is stored in the
SQL Management Base
For reporting purposes only
2013 © Trivadis
41 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Indexing
2013 © Trivadis
42 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Multiple Indexes on the Same Columns
2013 © Trivadis
43 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
multiple_indexes.sql
Support for Multiple Indexes on the Same Columns
2013 © Trivadis
44 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Partial Indexes
For performance purposes it’s sometimes not necessary to index all data stored
in a table
For example, it might be enough to index data of the last day or week and to
leave older data un-indexed
Through 11.2 some kind of partial indexes are supported by implementing
particular tricks
Making index partitions unusable
2013 © Trivadis
45 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Partial Indexes for Partitioned Tables
2013 © Trivadis
46 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
partial_index.sql
Partial Indexes for Partitioned Tables – Example
2013 © Trivadis
47 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Optimization Techniques
2013 © Trivadis
48 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Execution of UNION and UNION ALL
2013 © Trivadis
49 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
concurrent_union.sql
Concurrent Execution of UNION [ALL] concurrent_union_remote.sql
Considered when parallel processing is enabled and considered for all branches
According to the documentation branches with remote queries are supported
I was not able to observe it!
2013 © Trivadis
50 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
concurrent_union.sql
The (NO_)PQ_CONCURRENT_UNION Hints
2013 © Trivadis
51 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Scalar Subquery Unnesting Does Not Processes
Correlated Subqueries in the SELECT Clause
------------------------------------
| Id | Operation | Name |
------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | SORT AGGREGATE | |
|* 2 | INDEX UNIQUE SCAN| T2_PK |
| 3 | TABLE ACCESS FULL | T1 |
------------------------------------
2 - access("T2"."ID"=:B1)
2013 © Trivadis
52 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Scalar Subquery Unnesting Processes Correlated Subqueries in
the SELECT Clause
-------------------------------------
------------------------------------
| Id | Operation ||Name
Name ||
------------------------------------
-------------------------------------
| 0 | SELECT STATEMENT || ||
| 1 | HASH
SORT GROUP
AGGREGATE
BY || ||
|* 2 |
| INDEX
NESTEDUNIQUE
LOOPS SCAN|
OUTER|T2_PK ||
| 3 | TABLE
TABLEACCESS
ACCESSFULL
FULL|
| T1
T1 ||
------------------------------------
|* 4 | INDEX UNIQUE SCAN| T2_PK |
-------------------------------------
2 - access("T2"."ID"=:B1)
4 - access("T2"."ID"(+)="T1"."ID")
2013 © Trivadis
53 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Miscellaneous
2013 © Trivadis
57 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
expand_sql_text.sql
Expand Text of Queries Referencing Views
SQL> BEGIN
2 :in := q'[SELECT name, owner, category, enabled
3 FROM dba_outlines
4 WHERE enabled = 'ENABLED']';
5 dbms_utility.expand_sql_text(input_sql_text => :in,
6 output_sql_text => :out);
7 END;
8 /
QUERY
------------------------------------------------------------
SELECT "A1"."NAME" "NAME","A1"."OWNER" "OWNER","A1"."CATE...
2013 © Trivadis
58 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Summary
2013 © Trivadis
59 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013
Questions and answers ...
Christian Antognini
Senior Principal Consultant
[email protected]
BASEL BERN BRUGG LAUSANNE ZUERICH DUESSELDORF FRANKFURT A.M. FREIBURG I.BR. HAMBURG MUNICH STUTTGART VIENNA
2013 © Trivadis
60 The Query Optimizer in Oracle Database 12c - What's New?
November 21, 2013