You are on page 1of 64

Safe Harbor Statement

The following is intended to outline our general product direction. It is intended for
information purposes only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality, and should not be relied upon
in making purchasing decisions. The development, release, and timing of any features or
functionality described for Oracle’s products remains at the sole discretion of Oracle.

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 2


The Optimizer in Oracle Database 12c
Upgrading Without Pain

Nigel Bayliss
Optimizer Product Manager
@vldbb
http://blogs.oracle.com/optimizer

Data Warehousing
Product Management Team

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |


The Optimizer in Oracle Database 12c
Upgrading With Minimal Pain

Nigel Bayliss
Optimizer Product Manager
@vldbb
http://blogs.oracle.com/optimizer

Data Warehousing
Product Management Team

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |


Upgrading to Oracle Database 12c Without Pain
• From:
–Oracle Database 11g Release 2 or...
–Oracle Database 12c Release 1
• To:
–Oracle Database 12c Release 2
• With a word about Oracle Database 12c Release 1

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 5


Upgrading to Oracle Database 12c Without Pain
• I will cover adaptive optimizer features but...
• ...less about...
–What adaptive features do (it doesn't matter in this context)
• ...more about...
–When to care
–The DBA's perspective (adaption and metadata)
–Controlling adaptive features
–Prioritizing stability
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 6
Agenda

1 Stepping up to Oracle Database 12c


2 Enhancements in Oracle Database 12c Release 2
3 Upgrade Approach
4 A word about Oracle Database 12c Release 1
5 From test to production

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 7


Agenda

1 Stepping up to Oracle Database 12c


2 Enhancements in Oracle Database 12c Release 2
3 Upgrade Approach
4 A word about Oracle Database 12c Release 1
5 From test to production

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 8


The Goal of the Oracle Optimizer
• To find a good SQL execution plan for every query
–Oracle must figure out how to execute queries
–Better execution plans yield better performance
• The optimizer uses cardinality estimates to find the best
execution plans
–"How many rows?"

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 9


The Cost-Based Optimizer Uses Statistics
• The database gathers and
stores a rich set of statistics
about your data Row Count

Min/Max

#Distinct
Data values
Distribution
...and more...
Correlation

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 10


The Cost-Based Optimizer Uses Statistics
• Histograms for data skew

Santa Row Count


Dolls
Sold Min/Max

#Distinct
Data values
December
Distribution
...and more...
Correlation

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 11


The Cost-Based Optimizer Uses Statistics
• Column Group Statistics
for correlation
Row Count
...
WHERE month = 'NOVEMBER' Min/Max
AND zodiac = 'SCORPIO'
#Distinct
Data values
Month
Distribution
...and more...
Zodiac Correlation

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 12


But Statistics Are Not Always Enough
• SQL statements can be REALLY complex
• Schemas can be REALLY complex
• Data can be REALLY complex

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 13


The Goal of the Adaptive Optimizer
• Account for complexity
–Identify cardinality misestimates
–Obtain better cardinality estimates
–Use better estimates to improve SQL execution plans
–Improve optimizer statistics
•Histograms
•Column group statistics

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 14


New in Oracle Database 12c Release 1
The Adaptive Optimizer

Optimizer Adaptive Features

Learn from previous executions and


Change SQL execution plans at runtime
choose better SQL execution plans

12c Optimizer

Adaptive
Adaptive Statistics
Plans

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 15


Oracle Database 11g
11g Optimizer
Cardinality
Feedback

Answers the question,


"How are columns
used in queries?"
The Data
Column Usage Learn from
This metadata is Statistics Dictionary
persisted to the previous
data dictionary Gather Statistics executions (on a
FOR ALL COLUMNS query-by-query
SIZE AUTO
(the default) basis)
Better statistics for
skewed datasets Histograms

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 16


Dynamic Sampling In Oracle Database 11g
11g Optimizer
Cardinality
Feedback

Dynamic
Sampling

Column Usage
Statistics

Sample data to
account for missing
or inadequate
Histograms statistics

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 17


The Oracle Optimizer in Oracle Database 12c
From Oracle Database 12c 12c Optimizer Statistics feedback for
Release 1 single table cardinality
and joins
Adaptive
Plans Dynamic
Statistics
(Adaptive)
Column Usage SQL Plan
Adapt at Statistics Directives
runtime
The lessons learned
from previous
executions
are SHARED
between queries

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 18


Oracle Optimizer Adaptive Features
From Oracle Database 12c 12c Optimizer Statistics feedback for
Release 1 single table cardinality
and joins
Adaptive
Plans Dynamic
Statistics
(Adaptive)
Column Usage SQL Plan
Statistics Directives

Gather Statistics
FOR ALL COLUMNS
SIZE AUTO Column group
statistics created
automatically
Histograms

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 19


Viewing Optimizer Metadata
• DBA_SQL_PLAN_DIRECTIVES
• HISTOGRAM column in DBA_TAB_COLUMNS
• DBA_STAT_EXTENSIONS
• SELECT DBMS_STATS.REPORT_COL_USAGE('SH,'EMP')
FROM DUAL;
–SYS.COL_USAGE$

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 20


Agenda

1 Stepping up to Oracle Database 12c


2 Enhancements in Oracle Database 12c Release 2
3 Upgrade Approach
4 A word about Oracle Database 12c Release 1
5 From test to production

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 21


Oracle Database 12c Release 1
Controlling adaptive features
OPTIMIZER_ADAPTIVE_FEATURES

Optimizer Adaptive Features

Change plans at runtime Learn from previous executions

12c Optimizer

Adaptive Adaptive
Plans Statistics

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 22


A Wide Spectrum of Oracle Databases

Highly responsive Long running


Time critical - High complexity
Strict SLAs Large datasets
Most Databases
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 23
Oracle Database 12c Release 1
Adaptive features controlled with OPTIMIZER_ADAPTIVE_FEATURES

Zero Fully
Adaptive Adaptive

Oracle Database 12c Release 1 Default

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 24


Oracle Database 12c Release 2
A new default

Oracle Database 12c Release 2 Default

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 25


Oracle Database 12c Release 2
A new default and finer control

Minimal Default Fully


Adaptive Adaptive Adaptive

Oracle Database 12c Release 2 Default

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 26


From Oracle Database 12c Release 2
Finer control of adaptive features – new database parameters
OPTIMIZER_ADAPTIVE_FEATURES Obsolete

Optimizer Adaptive Features

OPTIMIZER_ADAPTIVE_PLANS OPTIMIZER_ADAPTIVE_STATISTICS

Change plans at runtime Learn from previous executions

12c Optimizer

Adaptive Adaptive
Plans Statistics

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 27


From Oracle Database 12c Release 2
New default behavior

Optimizer Adaptive Features

OPTIMIZER_ADAPTIVE_PLANS (TRUE) OPTIMIZER_ADAPTIVE_STATISTICS (FALSE)

Change plans at runtime Learn from previous executions

12c Optimizer

Adaptive Adaptive
Plans Statistics

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 28


Oracle Database 12c Release 2
A new default and finer control

Minimal Default Fully


Adaptive Adaptive Adaptive

Oracle Database 12c Release 2 Default

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 29


Oracle Optimizer Adaptive Features – Fully Adaptive
Oracle Database 12c 12c Optimizer Statistics feedback for
Release 2 single table cardinality
and joins
Adaptive
Plans Dynamic
Statistics
(Adaptive)
Column Usage SQL Plan
Statistics Directives

Gather Statistics
FOR ALL COLUMNS
SIZE AUTO Column group
statistics created
automatically
Histograms

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 30


Oracle Optimizer Adaptive Features – New Defaults
Oracle Database 12c 12c Optimizer Statistics feedback
Release 2 for single table
cardinality ONLY

Adaptive
Plans
Dynamic
Statistics

Column Usage SQL Plan


Statistics Directives

Gather Statistics
FOR ALL COLUMNS
SIZE AUTO

Histograms

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 31


Oracle Optimizer Adaptive Features – New Defaults
Oracle Database 12c 12c Optimizer Statistics feedback
Release 2 for single table
cardinality ONLY

Adaptive
Plans
Dynamic
Statistics

Column Usage SQL Plan


Statistics Directives
DBMS_STATS preference:
Gather Statistics
FOR ALL COLUMNS AUTO_STAT_EXTENSIONS
SIZE AUTO Default is OFF

Histograms

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 32


Oracle Optimizer Adaptive Features
Oracle Database 12c 12c Optimizer Statistics feedback
Release 2 for single table
cardinality ONLY

Dynamic
Statistics

Column Usage SQL Plan


Statistics Directives

Gather Statistics
FOR ALL COLUMNS
SIZE AUTO

Histograms

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 33


Oracle Database 12c Release 2
• Adaptive features…
–Defaults cover the widest range of workloads
–Finer control: easy to prioritize stability without turning
"everything off"
• In the context of upgrades…
–The defaults are closer to the behavior of Oracle
Database 11g

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 34


Agenda

1 Stepping up to Oracle Database 12c


2 Enhancements in Oracle Database 12c Release 2
3 Upgrade Approach
4 A word about Oracle Database 12c Release 1
5 From test to production

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 35


You Have Hundreds of Databases
• You expect Oracle to provide defaults that deliver excellent
performance for most systems
–"KISS" - and use the defaults in Oracle Database 12c Release 2
• The defaults have been chosen on the basis of what works best
for widest range of workloads

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 36


Default Adaptive Features

Minimal Default Fully


Adaptive Adaptive Adaptive

Use the defaults


OPTIMIZER_ADAPTIVE_PLANS (TRUE)
OPTIMIZER_ADAPTIVE_STATISTICS (FALSE)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 37
Traditionally, Oracle has always recognized that DBAs
need more control in complex and critical environments

If you want more control...

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 38


Minimal Adaptive

Minimal Default Fully


Adaptive Adaptive Adaptive

Set OPTIMIZER_ADAPTIVE_PLANS=FALSE

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 39


Fully Adaptive

Minimal Default Fully


Adaptive Adaptive Adaptive

Set OPTIMIZER_ADAPTIVE_STATISTICS=TRUE
Optionally, set DBMS_STATS preference
AUTO_STAT_EXTENSIONS=ON
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 40
Adaptive Statistics
OPTIMIZER_ADAPTIVE_STATISTICS=TRUE

• High added value where…


–Queries are longer-running
–Queries are complex
–Schema is complex
–Data is complex
• Ad-hoc query environments
–SQL plan directives allow the optimizer to learn from queries
and share this information with others
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 41
New in Oracle Database 12c Release 1
• New histogram types
–HYBRID and TOP-N – gather all your statistics
–Use SET_TABLE_PREFS to control what histograms you have?
• Global temporary tables – session private statistics
–DBMS_STATS.SET_DATABASE_PREFS (...
GLOBAL_TEMP_TABLE_STATS = >'SHARED') ....?
• SQL plan management evolution is
automated in Oracle Database 12c
–Do you want to use auto evolution?

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 42


New in Oracle Database 12c Release 2
• More compact incremental statistics for partitioned tables
from Oracle Database 12c Release 2 Partitioned
Table

Gather
SYSAUX Tablespace SALES
P1 has a synopsis Statistics
in pre-12.2 format
P1

P2
Synopsis for
P2 in compact
12.2 format

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 43


Remember: You Have Fine Control of Optimizer Statistics
• You can test new statistics without deleting the old
–DBMS_STATS.SET_GLOBAL_PREFS('PUBLISH', 'false');
DBMS_STATS.GATHER…
optimizer_use_pending_statistics = TRUE
DBMS_STATS.PUBLISH_PENDING_STATS
• You roll out changes to statistics back to a point-in-time
–DBMS_STATS.RESTORE_SCHEMA_STATS

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 44


You have a critical environment where any
performance regression is unacceptable...

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 45


What if You Could Do This?
Start With Oracle Database 11g
Good query
execution
plans =
Good query
performance
Oracle Database 12c Upgrade
Use same
Evolve over time... execution
3. Use SQL Plan Detect
4. Evolve and verify 3. Use SQL Plan
Baselines Baselinesplans =
better Good query
execution plans = performance
Better query performance

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 46


Use SQL Plan Management
Capture SQL plan baselines in pre-upgrade database
Oracle Database 11g
SQL Plan Baselines

Good
1. Capture
Execution
Plans

Oracle Database 12c


SQL Plan Baselines 2. Upgrade
OR Copy
3. Use SQL Plan
4. Evolve Baselines
Better No
Performance Performance
Regressions

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 47


SQL Plan Management – Even Post-Upgrade
Use optimizer_features_enable (OFE) to capture in post-upgrade database
Oracle Database 12c
optimizer_features_enable='11.2.0.4' SQL Plan Baselines

Good
1. Capture
Execution
Plans

optimizer_features_enable='12.2.0.1'

3. Evolve 2. Use SQL Plan Baselines

Better No
Performance Performance
Regressions

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 48


SQL Plan Management
Taking control of change

• Use a known set of SQL execution plans, but without


setting the system in stone
• Reduce the risk of change
–Capture SQL plan baselines and control SQL execution plans
–"Change"
–Alternative SQL execution plans are validated automatically
–Better SQL execution plans can be accepted automatically

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 49


Upgrading from Oracle Database 12c Release 1

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 50


Upgrading from Oracle Database 12c Release 1
• You want to continue with everything on, then keep things as they are
– Set OPTIMIZER_ADAPTIVE_STATISTICS=TRUE
– Set DBMS_STATS preference AUTO_STAT_EXTENSIONS to ON to allow column group
statistics to be created in response to SQL plan directives
• If adaptive features are disabled, consider using adaptive plans:
– Default OPTIMIZER_ADAPTIVE_PLANS (the default is TRUE)
– Default OPTIMIZER_ADAPTIVE_STATISTICS (the default is FALSE)
• Remember SQL plan management!

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 51


Agenda

1 Stepping up to Oracle Database 12c


2 Enhancements in Oracle Database 12c Release 2
3 Upgrade Approach
4 A word about Oracle Database 12c Release 1
5 From test to production

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 52


Oracle Database 12c Release 1 Production - Options
• You should probably keep things as they are
–OPTIMIZER_ADAPTIVE_FEATURES
–OPTIMIZER_FEATURES_ENABLE
• Defaults can be expected to work well for most
systems
• No requirement to re-test

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 53


Oracle Database 12c Release 1 - Options
• If you want the new adaptive parameters in Oracle
Database 12c Release 1 request patch for bug# 22652097
• To control auto column group creation using DBMS_STATS
preference AUTO_STAT_EXTENSIONS, apply patch for
bug# 21171382
• Testing will be required because plans may change

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 54


Agenda

1 Stepping up to Oracle Database 12c


2 Enhancements in Oracle Database 12c Release 2
3 Upgrade Approach
4 A word about Oracle Database 12c Release 1
5 From test to production

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 55


From Test to Production
• The optimizer generates metadata, for example:
– DBA_SQL_PLAN_DIRECTIVES
– SYS.COL_USAGE$
• Metadata gives rise to additional statistics, for example:
– Histograms
– DBA_STAT_EXTENSIONS
• Testing on realistic data can generate useful metadata and statistics
• You might want to transport metadata and statistics from one database to
another

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 56


Migrating Oracle Optimizer Metadata and Statistics
Keep It Simple - Data Pump!

Column Usage SQL Plan


Statistics Directives expdp content=metadata_only
include=statistics

impdp table_exists_action=skip
include=statistics
remap_schema=s1:s2

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 57


Migrating Oracle Optimizer Metadata and Statistics
Other Recommended Methods
Export/Import SQL Plan Directives
DBMS_SPD.PACK_STGTAB_DIRECTIVE

Column Usage SQL Plan


Statistics Directives
DBMS_STATS.TRANSFER_STATS (DB Link)

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 58


Summary
• Oracle Database 12c Release 2 Adaptive Features
–Have been improved
–Have finer controls
–By default, are closer to the behavior of Oracle Database 11g
–Most systems should use the new default settings for adaptive features
• For complex systems, choose adaptive features to match your
priorities and/or workload
• SQL plan management helps you manage change
• Optimizer metadata can be migrated from test to production
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 59
More Information
•Oracle Optimizer Blog
–http://blogs.oracle.com/optimizer
•Upgrade Blog
–http://blogs.oracle.com/UPGRADE
•OTN
–http://tinyurl.com/zamvpzv

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 60


More Information
•Future white papers...
–Optimizer with Oracle Database 12c Release 2
–Understanding Optimizer Statistics with Oracle Database 12c Release 2
–Best Practices for Gathering Optimizer Statistics with
Oracle Database 12c Release 2
–SQL Plan Management with Oracle Database 12c Release 2

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 61


Safe Harbor Statement
The preceding 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, and timing of any features or
functionality described for Oracle’s products remains at the sole discretion of Oracle.

Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 62


Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 63

You might also like