3. Topics
•
•
•
•
Plan
Flexibility
and
Plan
Stability
Adap-ve
Cursor
Sharing
(ACS)
SQL
Plan
Management
(SPM)
Combining
ACS
and
SPM
Enkitec
(c)
2014
3
4. Plan
Flexibility
• Cost-‐based
Op-mizer
(CBO)
– Introduced
in
Oracle
7
– Computes
an
Op-mal
Plan
based
on
• Heuris-cs
• Query
Predicates
• Schema
Object
Sta-s-cs
• Several
Others
Enkitec
(c)
2014
4
5. Flexibility:
Good
or
Evil?
• CBO
Mission
– Compute
an
Op-mal
Plan
for
a
given
SQL
and
its
Data
• Plans
have
many
and
complex
Dependencies
• CBO
Performance
– Successful
most
of
the
-mes
• Produces
a
sub-‐op-mal
Plan
some&mes
Enkitec
(c)
2014
5
6. DBA
Recurrent
Bad
Dream
• A
business
cri-cal
process
experiences
an
occasional
slow
down
– Same
SQL
usually
runs
very
quickly
– Developers
claim
they
haven’t
changed
anything
– Users
claim
they
do
the
same
every
-me
– Business
is
sick
and
-red
of
repor-ng
this
to…
YOU!
Enkitec
(c)
2014
6
7. Plan
Performs
Inconsistently?
(1)
•
•
•
•
•
•
Concurrency
with
some
other
Process
State
of
Buffer
Cache
RAC
Node
Affinity
CPU
Starva-on
Sudden
Changes
on
Data
Volume
Inconsistent
Performance
through
Database
Link
Enkitec
(c)
2014
7
8. Plan
Performs
Inconsistently?
(2)
•
•
•
•
•
Downgraded
Parallel
Execu-on
Crossing
Small-‐Table
Threshold
Resource
Manager
Direc-ve
Plan
Flips
Many
Others…
Enkitec
(c)
2014
8
9. WHY
a
Plan
Flips?
(1)
•
•
•
•
•
Bind
Variable
Peeking
with
Histograms
Outdated
Schema
Object
(SO)
Sta-s-cs
Missing
SO
Sta-s-cs
Fresh
SO
Sta-s-cs
New
Empty
Par--ons
Enkitec
(c)
2014
9
10. WHY
a
Plan
Flips?
(2)
• Incomplete
set
of
CBO
Hints
– Either
on
SQL
Text
or
SQL
Profile
• Dropped,
Invalid,
Invisible
or
new
Indexes
• Index
or
Table
rebuild
• Two
or
more
Plans
with
similar
Cost
Enkitec
(c)
2014
10
11. WHY
a
Plan
Flips?
(3)
•
•
•
•
Database
Parameters
Changes
CBO
System
Sta-s-cs
Changes
Asynchronous
SO
Sta-s-cs
Gathering
Many
Others…
Enkitec
(c)
2014
11
12. How
to
Mi-gate
Plan
Flipping
• Sound
CBO
Sta-s-cs
• Default
CBO
Parameters
• Regulate
Global
Changes
Enkitec
(c)
2014
12
15. Cursor
Sharing
• Use
of
Bind
Variables
instead
of
Literals
– AND
p.prod_category
LIKE
'Sooware%’
– AND
p.prod_category
LIKE
:b5
• Goal:
Reduce
Hard
Parsing
– Improve
Scalability
• Reduce
CPU
u-liza-on
• Reduce
Shared
Memory
footprint
Enkitec
(c)
2014
15
16. Cursor
Sharing
Shortcomings
• Flipping
Plans
– Exacerbated
by
Histograms
on
Skewed
Data
– AND
c.cust_marital_status
=
:b2
• :b2
:=
‘married’
• :b2
:=
‘widow’
– Plan
is
computed
at
hard
parse
• Plan
becomes
a
long
term
moving
target
Enkitec
(c)
2014
16
17. Adap-ve
Cursor
Sharing
(ACS)
• 11g+
• Mul-ple
Op-mal
Plans
per
SQL
– As
per
“Selec-vity”
of
Predicates
• :b2
:=
‘married’
(close
to
60%
maybe)
• :b2
:=
‘widow’
(this
one
is
more
selec-ve)
Enkitec
(c)
2014
17
18. ACS
Challenges
• Minimize
Resources
Impact
– Monitor
only
a
subset
of
SQL
Statements
– Ac-vate
ACS
only
for
a
subset
of
the
monitored
SQL
– Share
Execu-ons
Plans
through
a
“Selec-vity
Profile”
Bind
Aware
ACS
Bind
Sensi-ve
All
Statements
Enkitec
(c)
2014
18
19. Becoming
Bind
Sensi-ve
1. SQL
has
Range
Predicates
on
Bind
Variables
– AND
c.cust_year_of_birth
BETWEEN
:b3
AND
:b4
– AND
p.prod_category
LIKE
:b5
2. SQL
has
Equality
Predicates
on
Bind
Variables
and
Column
has
a
Histogram
– AND
c.cust_marital_status
=
:b2
– AND
TO_CHAR(s.-me_id,
'YYYY')
=
:b6
Enkitec
(c)
2014
19
20. Becoming
Bind
Aware
1. Rows
Processed
change
substan-ally
between
Execu-ons
– Between
a
few
rows
to
millions
2. Rows
Processed
oscillate
significantly
between
Execu-ons
– Between
a
few
rows
and
a
few
thousand
– Between
a
few
thousand
and
millions
Enkitec
(c)
2014
20
23. Rows
Processed
•
•
•
•
v$sql_cs_sta-s-cs.rows_processed
Updated
only
at
hard
parse
A
measure
of
amount
of
work
on
Execu-on
Plan
Three
sizes:
S/M/L
– 0:
Small
(less
than
1K
rows)
– 1:
Medium
(between
1k
and
1m
rows)
– 2:
Large
(more
than
1m
rows)
Enkitec
(c)
2014
23
24. ACS
Buckets
• v$sql_cs_histogram.bucket_id
– 0:
Small
– 1:
Medium
– 2:
Large
• v$sql_cs_histogram.count
– Incremented
with
each
Execu-on
as
per
• v$sql_cs_sta-s-cs.rows_processed
Enkitec
(c)
2014
24
25. Rows
Processed
and
ACS
Buckets
IF
v$sql_cs_statistics.rows_processed
<
1K
THEN
v$sql_cs_histogram.count(0)++
ELSIF
v$sql_cs_statistics.rows_processed
<
1M
THEN
v$sql_cs_histogram.count(1)++
ELSE
v$sql_cs_histogram.count(2)++
END
IF
Enkitec
(c)
2014
25
26. Becoming
Bind
Aware
1. Small
and
Large
buckets
have
a
value
– bucket_id.count(0)
>
0
AND
bucket_id.count(2)
>
0
2. Two
adjacent
buckets
have
same
non-‐zero
value
– bucket_id.count(0)
=
bucket_id.count(1)
>
0
– bucket_id.count(1)
=
bucket_id.count(2)
>
0
Enkitec
(c)
2014
26
27. Rows
Processed
per
Execu-on
•
•
•
•
•
•
•
10(0)…
50(0)…
3,000,000(2)…
BA
30(0)…
3,000(1)…
BA
2,000,000(2)…
1(0)…
BA
0(0)…
10,000(1)…
BA
3,000(1)…
2,000(1)…
200(0)…
300(0)…
BA
10…
100…
500…
2,000…
3,000…
5,000…
BA
rows_processed(bucket_id)…
Bind
Aware(BA)
Enkitec
(c)
2014
27
28. WHY
becoming
BA
is
important?
• Mul-ple
Op-mal
Plans
are
created
aoer
Cursor
becomes
Bind
Aware
Enkitec
(c)
2014
28
29. Sample
Query
(1)
SELECT
p.prod_subcategory_desc
subcatagory,
SUM(amount_sold)
amount_sold
FROM
sh.customers
c,
sh.products
p,
sh.sales
s
WHERE
c.cust_gender
=
'M'
AND
c.cust_marital_status
=
'single'
AND
c.cust_year_of_birth
BETWEEN
1913
AND
1990
AND
p.prod_category
LIKE
'Software%'
AND
TO_CHAR(s.time_id,
'YYYY')
=
'2001'
AND
s.cust_id
=
c.cust_id
AND
s.prod_id
=
p.prod_id
GROUP
BY
p.prod_subcategory_desc
ORDER
BY
p.prod_subcategory_desc;
Enkitec
(c)
2014
29
30. Sample
Query
(2)
• Based
on
Sample
Schema
SH
– With
CBO
Histograms
in
all
Columns
• Not
a
requirement
for
this
ACS
test
•
•
•
•
3
Tables
with
Filter
Predicates
2
Joins
6
possible
Join
Orders
Several
possible
Execu-on
Plans
Enkitec
(c)
2014
30
31. Demo
1
• 5
Execu-ons
of
Sample
Query
using
Literals
– Different
values
for
each
Execu-on
• Sequence
1,
2,
3,
4
and
5
– Each
Execu-on
performs
a
Hard
Parse
– Each
Execu-on
computes
a
“new”
Plan
– Each
seems
to
be
an
“Op-mal”
Plan
Enkitec
(c)
2014
31
32. Demo
2
• 5
Execu-ons
of
Sample
Query
using
Binds
– Different
values
for
each
Execu-on
• Sequence
1,
2,
3,
4
and
5
– Each
Execu-on
performs
a
Hard
Parse
• Forced
with
a
Cursor
Flush
before
the
Execu-on
– Each
computes
a
“new”
Op-mal
Plan
• Almost
same
as
“with
Literals”
Enkitec
(c)
2014
32
34. Demo
3
• 5
Execu-ons
of
Sample
Query
using
Binds
– Different
values
for
each
Execu-on
• Sequence
1,
2,
3,
4
and
5
– No
Cursor
Flush
between
Execu-ons
– First
Execu-on
computes
a
“new”
Op-mal
Plan
– All
Execu-ons
use
same
Plan…
Enkitec
(c)
2014
34
35. Demo
3
Results
Query
Rows
Processed
ACS
Bucket
Op,mal
Plan
ACS
Aware
Executed
1
1,483,124
2
2048551027
N
2048551027
2
1,272,154
2
3022804314
N
2048551027
3
1,014,876
2
2326939410
N
2048551027
4
716,168
1
2163719564
N
2048551027
5
530
0
2163719564
N
2048551027
Enkitec
(c)
2014
35
36. Demo
4
• 5
Execu-ons
of
Sample
Query
using
Binds
– Different
values
for
each
Execu-on
• Sequence
5,
4,
3,
2
and
1
– No
Cursor
Flush
between
Execu-ons
– Cursor
becomes
Bind
Aware
aoer
2nd
Execu-on
– All
Execu-ons
used
an
Op-mal
Plan
Enkitec
(c)
2014
36
37. Demo
4
Results
Query
Rows
Processed
ACS
Bucket
Op,mal
Plan
Bind
Aware
Executed
5
530
0
2163719564
N
2163719564
4
716,168
1
2163719564
N
2163719564
3
1,014,876
2
2326939410
Y
2326939410
2
1,272,154
2
3022804314
Y
3022804314
1
1,483,124
2
2048551027
Y
2048551027
Enkitec
(c)
2014
37
38. Demo
5
• 5
Execu-ons
of
Sample
Query
using
Binds
– Different
values
for
each
Execu-on
• Sequence
5,
1,
2,
3
and
4
– No
Cursor
Flush
between
Execu-ons
– Cursor
becomes
Bind
Aware
aoer
2nd
Execu-on
– All
but
one
Execu-ons
used
an
Op-mal
Plan
Enkitec
(c)
2014
38
39. Demo
5
Results
Query
Rows
Processed
ACS
Bucket
Op,mal
Plan
Bind
Aware
Executed
5
530
0
2163719564
N
2163719564
1
1,483,124
2
2048551027
N
2163719564
2
1,272,154
2
3022804314
Y
3022804314
3
1,014,876
2
2326939410
Y
2326939410
4
716,168
1
2163719564
Y
2163719564
Enkitec
(c)
2014
39
40. Controlling
ACS
with
CBO
Hint
• /*+
BIND_AWARE
*/
– Bypasses
the
monitoring
phase
of
a
Bind
Sensi-ve
SQL
• /*+
NO_BIND_AWARE
*/
– Turns
off
ACS
for
given
SQL
Enkitec
(c)
2014
40
42. ACS
Plan
Selec-on
• On
every
Execu-on
of
Bind
Aware
Cursor
– Compute
Selec-vity
of
each
qualifying
Predicate
– Search
Selec-vity
within
Range
of
values
on
ACS
Selec-vity
Profile
– If
within
Range,
lookup
Child
Number
and
use
its
Plan
– Else,
Hard
Parse
and
Execute
newly
computed
Plan
• If
same
as
exis-ng
Plan,
then
update
Selec-vity
Profile
• Else,
create
Selec-vity
Profile
for
new
Child
Number
Enkitec
(c)
2014
42
45. ACS
Summary
• ACS
is
capable
of
producing
mul-ple
Op-mal
Execu-on
Plans
per
SQL
• During
ramp-‐up
sub
Op-mal
Plans
may
happen
• ACS
Metadata
resides
in
Memory
(not
Persistent)
• ACS
provides
desirable
Plan
Flexibility
• ACS
does
not
address
the
Plan
Stability
concern
Enkitec
(c)
2014
45
46. SQL
Plan
Management
• 11g+
• Mul-ple
Persistent
Op-mal
Execu-on
Plans
per
SQL
• Only
“accepted”
and
“enabled”
Plans
are
Executed
• New
Plans
are
acknowledged
but
not
Executed
• Goal:
Plan
Stability
with
controlled
Flexibility
Enkitec
(c)
2014
46
47. SQL
Plan
Baseline
• A
set
of
Plans
available
to
the
CBO
for
a
given
SQL
– Iden-fied
by
SQL
Handle
and
Signature
• Hash
func-on
on
SQL
Text
• dbms_sqltune.sqltext_to_signature
– View
dba_sql_plan_baselines
• enabled
=
YES
• accepted
=
YES
• reproduced
=
YES
Enkitec
(c)
2014
47
48. Plan
History
• Content
of
dba_sql_plan_baselines
• Includes
SQL
Plan
Baseline
• Includes
Pending
Plans
– accepted
=
NO
and
last_verified
is
NULL
• Includes
Rejected
Plans
– accepted
=
NO
and
last_verified
is
not
NULL
• Includes
Disabled
Plans
Enkitec
(c)
2014
48
49. Methods
to
Create
a
Plan
Baseline
1. Capture
2. Load
a. Cursor
Cache
(CUR)
b. SQL
Tuning
Set
(STS)
c. Stored
Outline
3. Migra-on
1. Capture
2. Load
a.
b.
c.
d.
e.
CUR
STS
AWR
SPA
TRC
3. Migra-on
Enkitec
(c)
2014
49
50. Capturing
a
SQL
Plan
Baseline
(1)
• Set
op-mizer_use_sql_plan_baselines
to
TRUE
(default)
• Set
op-mizer_capture_sql_plan_baselines
to
TRUE
(default
is
FALSE)
– Set
this
parameter
at
SESSION
level
• Execute
SQL
2
-mes
• Set
op-mizer_capture_sql_plan_baselines
to
FALSE
Enkitec
(c)
2014
50
52. Loading
SQL
Plan
Baseline
from
Cache
• dbms_spm.load_plans_from_cursor_cache
– Inputs
• sql_id
• plan_hash_value
(opt)
– Outputs
• Number
of
Plans
loaded
Enkitec
(c)
2014
52
53. Demo
6
• Load
Plans
for
Query
out
of
Demo
5
– demo5.sql
– load_plans.sql
• Execute
Demo
5
again
and
review
Plan
History
– demo5.sql
– list_plans.sql
• Execute
demo5.sql
twice
and
verify
results
Enkitec
(c)
2014
53
54. Demo
6
Results
(aoer
1st
demo5.sql)
CHILD
EXECUTIONS
BUFFER_GETS
PLAN_HASH_VALUE
SHAR
SENS
AWRE
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐
-‐-‐-‐-‐
-‐-‐-‐-‐
0
3
9090
3022804314
Y
Y
N
CHILD
BUCKET_ID
COUNT
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
0
0
0
0
1
1
0
2
2
Note
-‐-‐-‐-‐-‐
-‐
SQL
plan
baseline
SQL_PLAN_652hmt7yxthdwd624c0cd
used
for
this
statement
CREATED
PLAN_NAME
ENA
ACC
REP
FIX
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐
-‐-‐-‐
-‐-‐-‐
-‐-‐-‐
28-‐DEC-‐13
02.03.43.000000
PM
SQL_PLAN_652hmt7yxthdw501f1424
YES
YES
YES
NO
28-‐DEC-‐13
02.03.43.000000
PM
SQL_PLAN_652hmt7yxthdwb6c39290
YES
YES
YES
NO
28-‐DEC-‐13
02.03.43.000000
PM
SQL_PLAN_652hmt7yxthdwd624c0cd
YES
YES
YES
NO
28-‐DEC-‐13
02.03.50.000000
PM
SQL_PLAN_652hmt7yxthdwc82a6b0c
YES
NO
YES
NO
Enkitec
(c)
2014
54
55. Demo
6
Results
(aoer
4rd
demo5.sql)
CHILD
EXECUTIONS
BUFFER_GETS
PLAN_HASH_VALUE
SHAR
SENS
AWRE
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐
-‐-‐-‐-‐
-‐-‐-‐-‐
0
2
4274
2163719564
N
Y
N
1
1
3030
3022804314
Y
Y
Y
2
1
3030
2326939410
Y
Y
Y
3
1
1587
2163719564
Y
Y
Y
Note
-‐-‐-‐-‐-‐
-‐
SQL
plan
baseline
SQL_PLAN_652hmt7yxthdwb6c39290
used
for
this
statement
CREATED
PLAN_NAME
ENA
ACC
REP
FIX
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐
-‐-‐-‐
-‐-‐-‐
-‐-‐-‐
28-‐DEC-‐13
02.03.43.000000
PM
SQL_PLAN_652hmt7yxthdw501f1424
YES
YES
YES
NO
28-‐DEC-‐13
02.03.43.000000
PM
SQL_PLAN_652hmt7yxthdwb6c39290
YES
YES
YES
NO
28-‐DEC-‐13
02.03.43.000000
PM
SQL_PLAN_652hmt7yxthdwd624c0cd
YES
YES
YES
NO
28-‐DEC-‐13
02.03.50.000000
PM
SQL_PLAN_652hmt7yxthdwc82a6b0c
YES
NO
YES
NO
Enkitec
(c)
2014
55
56. Demo
7
• Drop
SQL
Plan
Baseline
– drop_plans.sql
• Create
SQL
Patch
with
/*+
BIND_AWARE
*/
Hint
– sqlpch.sql
connected
as
SYS
• Load
Plans
for
Query
out
of
Demo
5
– demo5.sql
– load_plans.sql
Enkitec
(c)
2014
56
57. Demo
7
Results
CHILD
EXECUTIONS
BUFFER_GETS
PLAN_HASH_VALUE
SHAR
SENS
AWRE
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐
-‐-‐-‐-‐
-‐-‐-‐-‐
0
1
3052
2163719564
N
Y
Y
1
1
3030
2048551027
Y
Y
Y
2
1
3030
3022804314
Y
Y
Y
3
1
3030
2326939410
Y
Y
Y
4
1
1587
2163719564
Y
Y
Y
CREATED
PLAN_NAME
ENA
ACC
REP
FIX
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐
-‐-‐-‐
-‐-‐-‐
-‐-‐-‐
28-‐DEC-‐13
02.31.11.000000
PM
SQL_PLAN_652hmt7yxthdw501f1424
YES
YES
YES
NO
28-‐DEC-‐13
02.31.11.000000
PM
SQL_PLAN_652hmt7yxthdwb6c39290
YES
YES
YES
NO
28-‐DEC-‐13
02.31.11.000000
PM
SQL_PLAN_652hmt7yxthdwc82a6b0c
YES
YES
YES
NO
28-‐DEC-‐13
02.31.11.000000
PM
SQL_PLAN_652hmt7yxthdwd624c0cd
YES
YES
YES
NO
Enkitec
(c)
2014
57
58. Demo
8
• Execute
queries
1-‐5
and
observe
how
number
of
Execu-ons
increases
per
Child
Cursor
– flush.sql
– demo8.sql
– demo8.sql
– demo8.sql
Enkitec
(c)
2014
58
59. Demo
8
Results
CHILD
EXECUTIONS
BUFFER_GETS
PLAN_HASH_VALUE
SHAR
SENS
AWRE
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐
-‐-‐-‐-‐
-‐-‐-‐-‐
0
1
1585
2163719564
N
Y
Y
1
1
3030
2048551027
Y
Y
Y
2
1
3030
3022804314
Y
Y
Y
3
1
3030
2326939410
Y
Y
Y
4
2
3172
2163719564
Y
Y
Y
Note
-‐-‐-‐-‐-‐
-‐
SQL
patch
"sqlpch_8u0n7w1jug5dg"
used
for
this
statement
-‐
SQL
plan
baseline
SQL_PLAN_652hmt7yxthdwb6c39290
used
for
this
statement
CHILD
EXECUTIONS
BUFFER_GETS
PLAN_HASH_VALUE
SHAR
SENS
AWRE
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
-‐-‐-‐-‐
-‐-‐-‐-‐
-‐-‐-‐-‐
0
1
1585
2163719564
N
Y
Y
1
2
6060
2048551027
Y
Y
Y
2
2
6060
3022804314
Y
Y
Y
3
2
6060
2326939410
Y
Y
Y
4
4
6344
2163719564
Y
Y
Y
Enkitec
(c)
2014
59
60. Evolving
a
Plan
• Evolving
a
Plan
means
“accep-ng”
it
– Promo-ng
it
from
Plan
History
into
SQL
Plan
Baseline
• dbms_spm.evolve_sql_plan_baseline
– sql_handle
(opt)
– plan_name
(opt)
– verify
(default
YES)
– commit
(default
YES)
Enkitec
(c)
2014
60
61. Demo
9
• Drop
SQL
Plan
Baseline
– drop_plans.sql
• Capture
SQL
Plan
Baseline
with
1
accepted
Plan
– demo9.sql
– list_plans.sql
• Evolve
all
Plans
on
Plan
History
– evolve.sql
Enkitec
(c)
2014
61
62. FIXED
Flag
• When
set
to
YES
– Only
FIXED
Plans
are
considered
for
Plan
Selec-on
– No
more
Plans
are
Captured
into
Plan
History
Enkitec
(c)
2014
62
63. Plan
Selec-on
(1)
• At
hard
parse
when
SQL
Plan
Baseline
(SPB)
exists
1. CBO
computes
an
“Op-mal”
Plan
(OP)
•
Adap-ve
Cursor
Sharing
is
used
if
applicable
2. If
OP
exists
in
SPB
then
execute
this
OP
3. If
OP
does
not
exist
in
SPB
then
store
it
in
Plan
History
a.
If
there
are
FIXED
Plans
in
SPB
i.
Re-‐cost
all
FIXED
Plans
using
Binds
and
execute
cheapest
b. If
there
are
no
FIXED
Plans
in
SPM
i.
Re-‐cost
all
Plans
using
Binds
and
execute
cheapest
Enkitec
(c)
2014
63
65. Demo
10
• FIX
one
Plan
from
SQL
Plan
Baseline
– alter_plans.sql
• Verify
only
one
Plan
was
Executed
– flush.sql
– demo8.sql
– demo8.sql
Enkitec
(c)
2014
65
66. SPM
Summary
• SPM
provides
much
desired
Plan
Stability
– Only
“approved”
Plans
are
allowed
to
Execute
• ACS
can
generate
a
healthy
stock
of
Op-mal
Plans
– You
can
restrict
them
to
work
under
SPM
• Combining
ACS
and
SPM
you
can
obtain
– Plan
Flexibility
and
Plan
Stability
Enkitec
(c)
2014
66
67. SPM/ACS
Suggested
Strategy
• Use
dbms_applica-on_info.set_module
in
your
code
to
set
MODULE
and
ACTION
• Use
dbms_spm.load_plans_from_cursor_cache
filtering
with
MODULE
or
ACTION
• Use
sys.dbms_sqldiag_internal.i_create_patch
to
SQL
Patch
with
BIND_AWARE
the
SQL
on
Baselines
• Use
dbms_spm.evolve_sql_plan_baseline
to
verify
and
evolve
periodically
good
performing
Plans
Enkitec
(c)
2014
67
69. References
(1)
• Random
Slowdown
by
Jonathan
Lewis
– h{p://jonathanlewis.wordpress.com/2013/12/23/
plan-‐changes/
• Using
SQL
Patch
to
add
hints
to
a
packaged
applica-on
– h{ps://blogs.oracle.com/op-mizer/entry/
how_can_i_hint_a
Enkitec
(c)
2014
69