New Optimizer Features in Oracle 12c - Sage...

98
www.sagecomputing.com.au [email protected] New Optimizer Features in Oracle 12c Penny Cookson SAGE Computing Services SAGE Computing Services Customised Oracle Training Workshops and Consulting

Transcript of New Optimizer Features in Oracle 12c - Sage...

www.sagecomputing.com.au

[email protected]

New Optimizer Features

in Oracle 12c

Penny Cookson SAGE Computing Services

SAGE Computing Services

Customised Oracle Training Workshops and Consulting

SAGE Computing Services

Customised Oracle Training Workshops and Consulting

Penny Cookson

Managing Director and Principal Consultant

Working with Oracle products since 1987

Oracle Magazine Educator of the Year 2004

www.sagecomputing.com.au

[email protected]

SAGE Computing Services

Customised Oracle Training Workshops and Consulting

A long time ago in

a distant galaxy

And finally….

Oracle 12c

is here!

the evil emperor

promised to create

a new version of

his weapon

Agenda

Adaptive Optimisation

Adaptive Plans

Dynamic Sampling

Cardinality Feedback

SQL Plan directives

Other Stuff

VARCHAR2

Optimizer Statistics

Better histograms

Bulk Loads

Automatic creation of column

statistics groups

Session-private statistics for

global temporary tables

Reporting Mode

Reporting on Past Operations

X X X X X X X X X X X Dynamic Statistics

Stopping the worst

stuff ups (1st time)

X X X X X X X X X X X Statistics Feedback

Plan A – looking goodgoing to

work

Plan A

looking

good

Now witness the firepower of

this fully ARMED and

OPERATIONAL battle station!

You know that moment when you realise

that Plan A is not going to work

And you really need to try Plan B

That’s Adaptive Optimisation

During optimization – not re-optimization

Join methods (Hash and NL) and Parallel distribution methods

Adaptive Optimization – Adaptive Plans

Sub Plan (B)

Sub Plan (A)

Hash

Nested

Loop

You will find that it is you who

are mistaken, about a great

many things

Adaptive Optimization – Adaptive Plans

Sub Plan (B)

Sub Plan (A)

Hash

Nested

Loop Start

reading

rows

<threshold

>=threshold

Collector gathers statistics during execution

During execution you

realise you have

underestimated the

power of the Hash Side

Adaptive Optimization – Adaptive Plans

Sub Plan (B)

Sub Plan (A)

Hash

Nested

Loop Read

rows

<threshold

>=threshold

Next time it just chooses the same plan Everything is

proceeding as I

have foreseen

Organisations

Bookings

_Large Events

Resources

name = 'Australian Medical Systems'

description = 'Buffet Lunch'

comments = 'TEST'

This IS the join we are

looking for

Adaptive Plans - Example

This is the default plan for the

join to BOOKINGS_LARGE

You have

learned

much

young

optimizer

What is the Threshold – 10053 trace

Join order[4]: ORGANISATIONS[O]#0 EVENTS_LARGE[E]#2 BOOKINGS_LARGE[B]#3

RESOURCES[R]#1

………………………………………………………………………………………………………………………………………………………………………….

Outer table: ORGANISATIONS Alias: O

resc: 3.00 card 1.00 bytes: 26 deg: 1 resp: 3.00

Inner table: EVENTS_LARGE Alias: E

resc: 273.01 card: 75243.00 bytes: 17 deg: 1 resp: 273.01

using dmeth: 2 #groups: 1

Cost per ptn: 0.21 #ptns: 1

hash_area: 203 (max=25600) buildfrag: 1 probefrag: 267 ppasses: 1

Hash join: Resc: 276.22 Resp: 276.22 [multiMatchCost=0.00]

HA Join

HA cost: 276.22

resc: 276.22 resc_io: 275.00 resc_cpu: 47387417

resp: 276.22 resp_io: 275.00 resp_cpu: 47387417

Best:: JoinMethod: Hash

Cost: 276.22 Degree: 1 Resp: 276.22 Card: 12540.50 Bytes: 43

How many

Events are

we expecting

What is the Threshold – 10053 trace

***************

Now joining: BOOKINGS_LARGE[B]#3

***************

Outer table: EVENTS_LARGE Alias: E

resc: 276.22 card 12540.50 bytes: 43 deg: 1 resp: 276.22

Inner table: BOOKINGS_LARGE Alias: B

resc: 9695.37 card: 5767168.00 bytes: 19 deg: 1 resp: 9695.37

………………………………………………………………………………………………………………………………………………………………………….

…………………………………………… works out the best join order ……………………………………….

………………………………………………………………………………………………………………………………………………………………………….

*********************************

Number of join permutations tried: 20

*********************************

………………………………………………………………………………………………………………………………………………………………………….

Searching for inflection point (join #1)

What if there are less events than we

expect?

Searching for inflection point (join #2) between 0.00 and 12540.50

AP: Computing costs for inflection point at min value 0.00

DP: Using binary search for inflection point search

DP: Costing Nested Loops Join for inflection point at card 0.00

………………………………………………………………………………………………………………………………………………………………………….

DP: Costing Hash Join for inflection point at card 0.00

………………………………………………………………………………………………………………………………………………………………………….

DP: Costing Nested Loops Join for inflection point at card 12540.50

………………………………………………………………………………………………………………………………………………………………………….

DP: Costing Hash Join for inflection point at card 12540.50

………………………………………………………………………………………………………………………………………………………………………….

DP: Costing Nested Loops Join for inflection point at card 9695.37

………………………………………………………………………………………………………………………………………………………………………….

DP: Costing Hash Join for inflection point at card 9695.37

………………………………………………………………………………………………………………………………………………………………………….

………………………………………………………………………………………………………………………………………………………………………….

DP: Found point of inflection for NLJ vs. HJ: card = 164.56

What if there are less Events than we

expect?

If more than 164 rows you

have a hash join you will do

BEGIN

dbms_spd.flush_sql_plan_directive;

END;

/

SELECT d.directive_id, d.type, d.state, d.reason, d.created,

o.object_name, o.subobject_name, o.notes, o.owner

FROM dba_sql_plan_directives d, dba_sql_plan_dir_objects o

WHERE o.directive_id = d.directive_id

AND o.owner NOT IN ('XDB','SYS','SYSTEM')

ORDER BY directive_id desc;

Is the information

saved?

Adaptive Optimisation

OPTIMIZER_FEATURES_ENABLE >= 12.1.0.1

OPTIMIZER_ADAPTIVE_FEATURES = true

OPTIMIZER_ADAPTIVE_REPORTING_ONLY = false

Always in motion is

the Oracle version

Adaptive Optimisation

SELECT

DBMS_XPLAN.DISPLAY_CURSOR ('89xrxq7gghfdm',0, FORMAT=>'+REPORT')

FROM dual;

Statistics Feedback

Storm Trooper Conference

Base it on our existing statistics

How many chairs do

we need in the main

room?

Storm Trooper Conference

This should be enough

Oh dear

who knew Connor McVader was presenting

Storm Trooper Conference

Base it on our existing statistics and the

feedback from this morning

How many chairs do

we need in the main

room?

That’s better, but what happens when they shut the

place down and we start again tomorrow?

No problem we have obeyed the prime directive plan

xxxxxxxx

1010 is a minority value

Statistics are not up to date

When will statistics

feedback be used?

Find the SQL_ID

Execute again

but it uses the same plan

Cardinality feedback not used

With a complex condition

Execute again

After executing the same statement again

Statistics Feedback –

works with Joins too now

it does

And persistent it is

Dynamic Statistics

Dynamic stats considered when:

OPTIMIZER_DYNAMIC_SAMPLING = default (2)

DYNAMIC_SAMPLING hint

Parallel

No/old statistics

Expressions

Complex predicates (OR or AND on 1 table)

Joins/Group By

(12) Shareable

(12) Automatic dynamic sampling for level 11

Table has no statistics Default value (2) for

OPTIMIZER_DYNAMIC_SAMPLING

Access the table that has no

statistics

Table that has

really bad

statistics

Access the table that has bad statistics

Statistics are up to

date, but we have a

complex condition

But no dynamic

sampling

Statistics are up to date, but we

have a complex condition

Dynamic Statistics

When should we use them?

Stats not right

Stable table Volatile table

Get a new DBA TP or DWH

database?

Unknown conditions

Dynamic stats at

session/task level

TP

Dynamic stats at

DB level

DWH

Get a new DBA

TP DWH

TP or DWH

database?

Shareable Dynamic Statistics

Adaptive Optimisation Summary

During Execution

Adaptive Plans

NL or Hash

Parallel

distribution

Dynamic

statistics Adaptive Statistics

SQL Plan

directives

Cardinality

Adaptive Optimisation Summary

Re-optimisation

Statistics

feedback

SQL Plan

directives

Next hard

parse

Only SQL Plan Management

deals in absolutes

Dynamic

Statistics

Adaptive Optimisation Summary

Feature Bind Vars

/Literals

Conditions When

Adaptive Plan Literals and

bind variables

Join method, Parallelisation During

Statistics

Feedback

Literals but

works with

Adaptive

cursors for

bind variables

Complex conditions

joins

Re-Opt but stores as SPD

Adaptive cursor Bind variables Skewed data and histogram During - /*+BIND_AWARE */

Or Re-opt

Dynamic

Statistics

Literals and

Bind Variables

Dependent on level

no stats, old stats, complex

conditions

During

Optimisation Enhancements

Adaptive optimisation is good for the multi table

dependencies that statistics cannot help with

Need for some statistics feedback can be removed

by correct statistics – watch the plan directives

Dynamic stats can help with weird query

combinations and volatile tables

Monitor SQL Plan

Directives you must –

Help you improve your

statistics they will

Improved Statistics

Size matters not. Look at me.

Judge me by my size, do you?

Male Jedis always say that.

It matters to the optimizer

11g -Histograms 11g -Histograms

Frequency Histogram

7 distinct values

7 buckets

11g -Histograms

Height Balanced Histogram

11g -Histograms

7 distinct values

6 buckets

12g -Histograms

Top Frequency Histogram

7 distinct values

5 buckets

Top 5 values occurs >= (1 – 1/5)*100 pct = 80%

estimate_percent = AUTO_SAMPLE_SIZE

Hybrid Histogram

12g -Histograms

2 4 6 3 Repeat Count

7 distinct values

5 buckets

Top 5 values occurs < (1 – 1/5)*100 pct = 80%

3

12g -Histograms

Height balanced histograms

stop using you should

Estimate_percent = integer

do not use

Extended Statistics

Introduced in 11g for multi column

data dependencies

Data Dependency

SELECT count(*)

FROM org

WHERE state = ‘WA’ and postcode = 6000

11g – Create Statistics on a

Column Group

dbms_stats.create_extended_stats

('TRAIN',‘ORG','(STATE, POSTCODE)');

12c – Create Statistics on a

Column Group

12c – Create Statistics on a

Column Group

12c – Create Statistics on a

Column Group

12c – Use Statistics on a

Column Group

Running Statistics in Reporting Mode

SET LINES 100 PAGES 0

SET LONG 100000

COLUMN REPORT FORMAT A200

VARIABLE stats_report CLOB;

BEGIN

:stats_report :=

dbms_stats.report_gather_schema_stats

(ownname=>'TRAIN',

detail_level=>'ALL',

format=>'HTML');

END;

Running Statistics in Reporting Mode

Reporting on Past Statistics

SET LINES 100 PAGES 0

SET LONG 100000

COLUMN REPORT FORMAT A200

VARIABLE stats_report CLOB;

BEGIN

:stats_report := dbms_stats.report_stats_operations

(since=>sysdate-2,

until=>sysdate,

detail_level=>'ALL',

format=>'HTML');

END;

Global Temporary Tables - Oracle 11.2

Oracle 11.2 – New Session

Oracle 11.2 – Original Session (100322 rows)

Those were

not the

statistics we

were looking

for

Global Temporary Tables - Oracle 11.2

R2-D2, you

know better

than to trust

a strange

computer

Session specific statistics for global

temporary tables

Session specific statistics

on global temporary

tables

Consider gathering

statistics you should

when Global Temporary

Tables you are working

with

Online Statistics Gathering

for Direct Load

Oracle 11g

Load and then gather stats

Extra full scan

Statistics automatically gathered

Oracle 12c

But no histogram

Create a table and

index, then populate it

Table/column

statistics are created

Index stats not

created

Create a table and

insert one row

We have the statistics

from the CREATE

Deceive you your eyes

can; do not trust them

Now append a whole

load of rows

It still thinks the table

is empty

Increase in Limit for VARCHAR2

ID COMMENTS

13 XXXX XXXX

14 XXXX XXXX

15 XXXX XXXX

ID COMMENTS

13 XXXX XXXX XXXXXXX XXXXXXX XX

14 XXXX XXXX XXXXXXX XXXXXXX XX

15 XXXX XXXX XXXXXXX XXXXXXX XX

VARCHAR2(4000) VARCHAR2(32767)

DBA Morning Tea You can't stop change

any more than you

can stop the suns

from setting

The optimizer is

much smarter

But what about

stability?

Master Yoda

says I should be

mindful of the

future

But not at the

expense of the

moment

Questions?