SlideShare una empresa de Scribd logo
1 de 36
SQL Plan Directives Explained
Mauro Pagano
Mauro Pagano
• Consultant, working with both DBA and Devs
• Oracle  Enkitec  Accenture
• DBPerf and SQL Tuning
• Training, Workshops, OUG
• Free Tools (SQLd360, TUNAs360, Pathfinder)
2
Some background
• CBO makes mistakes
– Complex code with challenging job
– Based on
• Statistical formula, not perfect in 100% cases
• Partial knowledge of the data (stats)
• Mistakes can translate into poor plans
• Poor plans usually lead to poor performance
3
How to avoid mistakes?
• Improve quality of the model (Oracle)
– Hard, lots of corner cases for CBO to handle
• Improve quality of stats (kind of you)
– Hard, need knowledge of data and queries
• Reactively, learn from them to avoid next time
– Allow to adapt to specific situation
– Requires mistake to be made first though
4
Cardinality Feedback
• Oracle attempt of making CBO learning from its own mistakes
– Introduced in 11.2 (11.1 as one-off)
– Enhanced in 12.1 to deal with joins (but disabled in 12.2)
• Might take a bit to learn
– Kind of iterative approach, up to 5 “refined” attempts
• Lessons learned as OPT_ESTIMATE hints (like STA), very specific
– Not persisted to dictionary (same mistakes over time)
• Few initial bugs gave it a bad reputation
– SQL runs fine the first time, run again and runs slow!
– Supposed to be transparent, not good when gives troubles 
– Some people got burned, decided to turn off system-wise
5
Table TAB1
drop table tab1;
create table tab1 (n1 number, n2 number, n3 number);
-- the data is strongly correlated
insert into tab1
select mod(rownum, 100),
mod(rownum, 100),
mod(rownum, 100)
from dual
connect by rownum <= 100000;
commit;
exec dbms_stats.gather_table_stats(user,'TAB1');
6
Cardinality Feedback
select count(*) from tab1 where n1 = 1 and n2 = 1 and n3 = 1
----------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows |
----------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 1 |
| 1 | SORT AGGREGATE | | 1 | 1 | 1 |
|* 2 | TABLE ACCESS STORAGE FULL| TAB1 | 1 | 1 | 1000 |
----------------------------------------------------------------------
----------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows |
----------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 1 |
| 1 | SORT AGGREGATE | | 1 | 1 | 1 |
|* 2 | TABLE ACCESS STORAGE FULL| TAB1 | 1 | 1000 | 1000 |
----------------------------------------------------------------------
Note
-----
- statistics feedback used for this statement
OPT_ESTIMATE (@"SEL$1" TABLE "TAB1"@"SEL$1" ROWS=1000.000000 )
7
P(A and B and C) =
P(A) * P(B) * P(C)
SQL Plan Directives
• New feature introduced in 12.1
– Several changes introduced in 12.2
• Designed to be transparent
– SPDs are CBO generated
– SPDs are leveraged by CBO once in place
• Enabled by default in 12.1
• Disabled by default in 12.2
8

What’s the goal?
• Help CBO make better estimations
• Not SQL-specific
– Can be re-used by similar SQLs
• No specific adjustments like CFB
• Keep track of ”risky paths”
• Determine when to leverage other features
– Dynamic Statistics
– Instruct DBMS_STATS to create Column Groups
9
What does SPD track?
• Cardinality misestimates (usually underestimates)
• “Risky paths” are
– Data correlation on multi-column filter predicates
– Uncommon join correlation
– Data correlation on columns in GROUP BY
• Potentially other paths can be tracked
– For example, cardinality estimation at query block level
• Many TYPEs could be supported
– As of today, only existing TYPE is DYNAMIC_SAMPLING (*)
10
How does SPD track them?
• Entry in the data dictionary
– opt_directive$, opt_finding_obj$
– Externalized as DBA views
• Records table / columns involved
– Different SQLs on same objects can use same SPD
• Info read at hard parse and consumed accordingly
– For example, triggering dynamic sampling
• Used as warning “watch out when estimating X”
11
SPD creation example
select count(*) from tab1 where n1 = 1 and n2 = 1 and n3 = 1;
(from 10053)
SINGLE TABLE ACCESS PATH
Single Table Cardinality Estimation for TAB1[TAB1]
Table: TAB1 Alias: TAB1
Card: Original: 100000.000000 Rounded: 1 Computed: 0.100000 Non Adjusted: 0.10
…
SPD: Generating finding id: type = 1, reason = 1, objcnt = 1, obItr = 0, objid = 135446, objtyp = 1, vecsize = 4, colvec =
[1, 2, 3, ], fid = 29…48
SPD: Inserted felem, fid=29…48, ftype = 1, freason = 1, dtype = 0, dstate = 0, dflag = 0,…
12
Mistake!
We know it’s 1K rows
SPD creation example
select directive_id, type, reason, notes
from dba_sql_plan_directives
where directive_id in (select directive_id
from dba_sql_plan_dir_objects
where owner = sys_context('userenv', 'session_user')
and object_name = 'TAB1');
DIRECTIVE_ID TYPE REASON
-------------------------- ---------------- ------------------------------------
1149064327055029475 DYNAMIC_SAMPLING SINGLE TABLE CARDINALITY MISESTIMATE
NOTES
-----------------------------------------------------
<spd_note>
<internal_state>NEW</internal_state>
<redundant>NO</redundant>
<spd_text>{EC(MPAGANO.TAB1)[N1, N2, N3]}</spd_text>
</spd_note>
13
SPD creation example
select *
from dba_sql_plan_dir_objects
where owner = sys_context('userenv', 'session_user')
and object_name = 'TAB1';
SUBOBJECT_NAME OBJECT
---------------- ------
N1 COLUMN
N2 COLUMN
N3 COLUMN
TABLE  NOTES is at the TABLE level
NOTES
--------------------------------------------------------------------------
<obj_note>
<equality_predicates_only>YES</equality_predicates_only>
<simple_column_predicates_only>YES</simple_column_predicates_only>
<index_access_by_join_predicates>NO</index_access_by_join_predicates>
<filter_on_joining_object>NO</filter_on_joining_object>
</obj_note>
14
SPD creation (another) example
DIRECTIVE_ID TYPE REASON
--------------------- ---------------- ------------------------------------
7426975260533728013 DYNAMIC_SAMPLING JOIN CARDINALITY MISESTIMATE
NOTES
---------------------------------------------------------------------------------
<spd_note>
<internal_state>NEW</internal_state>
<redundant>NO</redundant>
<spd_text>{(SYS.OPT_DIRECTIVE$) - (SYS.OPT_FINDING_OBJ$)}</spd_text>
</spd_note>
DIRECTIVE_ID OBJECT_NAME OBJECT NOTES
--------------------- ---------------- ------ ------------------------------------
7426975260533728013 OPT_DIRECTIVE$ TABLE <obj_note> EVERYTHING NO </obj_note>
7426975260533728013 OPT_FINDING_OBJ$ TABLE <obj_note> EVERYTHING NO </obj_note>
15
SPD creation example
• SPD_TEXT reports the “risky path”
– Specific format within { }, built on the fly
– One or more tables
– Columns included for single table
– Format includes flag on predicate (where applicable)
• E = Equality predicates
• C = Simple column predicates
• J = Index access by join predicates
• F = Filter on joining object
16
SPD creation example
• INTERNAL_STATE shows where in the lifecycle SPD is
– NEW – SPD just created (flushed to disk), not used by SQLs yet
– MISSING_STATS – CBO used to help estimations, likely used ADS
– HAS_STATS – Stats (likely Column Group) helped CBO, SPD “off”
– PERMANENT – Stats not enough, SPD will stay “on” (ADS)
• First two states are transitory
• Last two states are definitive
– HAS_STATS -> PERMANENT if stats don’t help “enough”
– SPD goes trough HAS_STATS before PERMANENT
17
How does CBO use SPD?
• During hard parse applicable SPDs are looked at
– Search done at multiple levels
– Objects looked up by obj#
• Many SPDs are not “current” anymore
– Obsoleted / Superseded
• The valid ones are used
– Action triggered at the specific level
– As of today only CBO action is Dynamic Sampling (ADS)
18
SPD usage example – single table
select count(*) from tab1 where n1 = 1 and n2 = 1 and n3 = 1;
Query Block SEL$1 (#0)
Applicable DS directives:
dirid = 16004857199080262683, state = 1, flags = 1, loc = 1 {EC(135484)[1, 2, 3]}
…
SINGLE TABLE ACCESS PATH
Single Table Cardinality Estimation for TAB1[TAB1]
SPD: Directive valid: dirid = 16004857199080262683, state = 1, flags = 1, loc = 1 {EC(135484)[1, 2, 3]}
…
Table: TAB1 Alias: TAB1
Card: Original: 100000.000000 >> Single Tab Card adjusted from 0.100000 to 1000.000000 due to adaptive
dynamic sampling
Rounded: 1000 Computed: 1000.000000 Non Adjusted: 0.100000
19
SPD usage example – single table
select count(*) from tab1 where n1 = 1 and n2 = 1 and n3 = 1;
(from 10046 during hard parse)
PARSING IN CURSOR #140175694530408 …
SELECT /* DS_SVC */ /*+ … result_cache(snapshot=3600) */ SUM(C1)
FROM (SELECT /*+ qb_name("innerQuery") NO_INDEX_FFS( "TAB1")*/ 1 AS C1
FROM "TAB1" "TAB1"
WHERE ("TAB1"."N1"=1)
AND ("TAB1"."N2"=1)
AND ("TAB1"."N3"=1)
) InnerQuery
STAT #140175694530408 id=1 cnt=1 op='RESULT CACHE 145cmpkvg18at78z0dz8ptfwfw STAT
#140175694530408 id=2 cnt=1 op='SORT AGGREGATE
STAT #140175694530408 id=3 cnt=1000 op='TABLE ACCESS FULL TAB1
20
Use of SPD – single table conclusions
• Allows CBO to get better estimation on the fly
• Cool but not the best approach in the long run
– ADS even if fast still takes some time (overhead)
– Longer parses cause most of the troubles in real-life
• Same result achieved with Column Group (often)
• Real goal is
– Put a band-aid until Column Group in place
– Instruct DBMS_STATS to collect Column Groups
– Verify if Column Group helped, if not keep SPD active
21
SPD usage example – join
select … from dba_sql_plan_directives a, dba_sql_plan_dir_objects b
where a.directive_id = b.directive_id
and a.created >= sysdate-1/24
order by a.create
Query Block SEL$AF75E632 (#0)
Applicable DS directives:
dirid = 10275726999217578058,state = 1,flags = 1,loc = 1{(612)[7]}
dirid = 13360472976608779416,state = 1,flags = 1,loc = 1{(612)[]}
dirid = 7426975260533728013,state = 1,flags = 1,loc = 2{(612)[]; (608)[]}
dirid = 4318109285801377208,state = 1,flags = 1,loc = 1{(608)[]}
dirid = 1215330379436061558,state = 1,flags = 1,loc = 2{(608)[]; (22)[]}
dirid = 17607835686132426105,state = 5,flags = 1,loc = 1{C(22)[1]}
dirid = 17030158486626056340,state = 1,flags = 1,loc = 2{(612)[];(608)[]; (22)[]}
22
SPD usage example – join
(from 10053)
SPD: Directive valid: dirid = 7426975260533728013,…,loc = 2 {(612)[]; (608)[]}
…
Join Card: 13.000000 = outer (9.000000) * inner (13.000000) * sel (0.111111)
>> Join Card adjusted from 13.000000 to 16.000000 due to adaptive dynamic sampling, prelen=2
Adjusted Join Cards: adjRatio=1.230769 cardHjSmj=16.000000 …
Join Card - Rounded: 16 Computed: 16.000000
(from 10046)
PARSING IN CURSOR #139704599064112
SELECT /* DS_SVC */ /*+ … result_cache(snapshot=3600) */ SUM(C1)
FROM (SELECT /*+ qb_name("innerQuery") NO_INDEX_FFS( "F#0") */ 1 AS C1
FROM "SYS"."OPT_FINDING$" "F#0",
"SYS"."OPT_DIRECTIVE$" "D#1"
WHERE ("D#1"."F_ID"="F#0"."F_ID")
) innerQuery
23
Use of SPD – join conclusions
• Allows CBO to get better estimations for joins
– Provide adjustment ratio
– No stored stats provide this info (really cool)
– OPT_ESTIMATE is the closest thing, but still different
• Suffers from same side effects of single table
– Magnified by longer ADS on joins
– SPD from joins can be SUPERSEDED (HAS_STATS) too (*)
24
What happens to SPD after usage?
(from 10053 of the second parse)
SPD: Generating finding id: type = 1, reason = 1, objcnt = 1, obItr = 0, objid = 135484, objtyp = 1,
vecsize = 4, colvec = [1, 2, 3, ], fid = 108…890
SPD: Inserted felem, fid=108…890,ftype = 0,freason = 0,dtype = 1,dstate = 2,…
SPD: qosdRecDSDirChange dirid = 16004857199080262683, retCode = UPDATED
(from dictionary)
NOTES
----------------------------------------------------
<spd_note>
<internal_state>MISSING_STATS</internal_state>
<redundant>NO</redundant>
<spd_text>{EC(MPAGANO.TAB1)[N1, N2, N3]}</spd_text>
</spd_note>
25
CBO is aware there is a
“weak path” and it had to
use ADS in order to limit
the damage
SPD instructs DBMS_STATS
(from 10046 during parse)
merge into sys.col_group_usage$ d using (select :1 obj#, :2 cols from dual) s on (d.obj# = s.obj# and d.cols = s.cols)
when matched then update set d.timestamp = :3, d.flags = d.flags + :4 - bitand(d.flags, :4) when not matched then insert
(obj#, cols, timestamp, flags) values (:1,:2,:3,:4)
:1 -> 135484 :2 -> “1,2,3” :3 -> ”1/12/2017 8:33:7” :4 -> 17
(from 10046 during DBMS_STATS run)
SELECT ... FROM SYS.COL_GROUP_USAGE$ CU
…
UPDATE SYS.COL_GROUP_USAGE$ C
SET C.FLAGS = FLAGS + 8 - BITAND(FLAGS, 8)
WHERE OBJ# = :B2 AND COLS = :B1
COLUMN_NAME DATA_DEFAULT NUM_DISTINCT HISTOGRAM
------------------------------ ------------------------------------ ------------ ---------
SYS_STSOYQUEIAZ7FI9DV53VLN$$$0 SYS_OP_COMBINED_HASH("N1","N2","N3") 100 FREQUENCY
26
Then what happens to SPD?
SINGLE TABLE ACCESS PATH
Single Table Cardinality Estimation for TAB1[TAB1]
SPD: Directive valid: dirid=160…83,state=2,flags=1,loc=1{EC(…)[1, 2, 3]}
...
ColGroup (#1, VC) SYS_STSOYQUEIAZ7FI9DV53VLN$$$0
Col#: 1 2 3 …
Table: TAB1 Alias: TAB1
Card: Original: 100000.000000 Rounded: 1000 Computed: 1000.000000
…
SPD: Inserted felem, fid=10…890,…,dtype = 1, dstate = 3, dflag = 1,…
…
NOTES
------------------------------------------------------
<spd_note>
<internal_state>HAS_STATS</internal_state>
<redundant>NO</redundant>
<spd_text>{EC(MPAGANO.TAB1)[N1, N2, N3]}</spd_text>
</spd_note>
27
If the CG doesn’t help then
SPD is marked as
PERMANENT and behaves like
MISSING_STATS (ADS used)
No mistake 
SPD DS results
• DS results are stored
– Multiple SQL IDs (different parse) would trigger same DS SQL
• In 12.1 Result Cache is used
– Results have lifetime of 3600 secs
– Result Cache itself had some troubles in the past
• Mostly latch contention under heavy load
• In 12.2 SPD repository itself used to store results
– Makes them more permanent
– Search in the repo based on ADS recursive SQL ID
28
Is my SQL using SPD?
• Many SPDs can reference just a few objects
– Even simple SQL can use many SPDs
• CBO looks into them and pick up valid
– A subset of Valid are Used
• OTHER_XML reports #valid/#used
• DBMS_XPLAN to show if SQL is using SPDs
– DISPLAY (explain plan) reports which ones
– DISPLAY_CURSOR (execution) reports just #used
29
DBMS_SPD
• Oracle seeded package to manage SPDs
• Move via pack/unpack in stgtab like baselines
– Base 12.1.0.2 has bugs that impact xfr of SPDs
• Allow to manually flush SPDs to disk
– Instead of waiting 15 minutes
• Can set basic preferences
– For example, retention period before auto drop
30
Why the bad reputation?
• Some issues caused large negative impact
– Hard to spot when the feature is “transparent”
– Main symptoms mutex / latch contention
• SPDs generated based on workload
– Performance tends get better “with time”
• Makes performance in Prod bad on day 1
• Even though it was great in Test on day 200
– Upgrades need extra steps (move SPDs)
• Limited number of Column Groups per table
– Less useful CG could be created instead of useful ones
31
Most common problems
• Most issues are ultimately caused by ADS
– ADS running for too long
– ADS executed even for small app SQLs (no need)
• Longer parse caused by sampling
– Parse is a serialized operation, parser hold X mutex
– Other sessions with same SQL are stuck
– Easy for snowball effect to trigger
• Result Cache maybe not the best decision?
32
Solutions?
• I’m personally a big fan of SPD
– But I’m not a Production DBA 
• Many products require to turn it off in 12.1
– Bad habit was to turn off all Adaptive Features via parameter
– Oracle patch split parameter in two (AdaptPlans vs AdaptStats)
• So just SPD and ADS can be disabled, Adaptive Plans stay ON
• Oracle recommended solution in 12.1, SPD OFF with patch+param
• Disabled by default in 12.2
– When enabled it seems a bit more conservative
– Too early to judge how it will behave in 12.2 prod
33
Summary
• Goal is improve CBO estimations over time
– Tracking correlation in many areas
– Putting stats / ADS in place to catch that
• Might take time and introduce instability
– Makes comparing two systems harder
• Generated issues in 12.1
– Oracle recommends to disable by default
– Enable back specific systems might be beneficial
34
35
Contact Information
• http://mauro-pagano.com
– Email
• mauro.pagano@gmail.com
– Free tools to download
• SQLd360
• TUNAs360
• PAthfinder
36

Más contenido relacionado

La actualidad más candente

Adapting and adopting spm v04
Adapting and adopting spm v04Adapting and adopting spm v04
Adapting and adopting spm v04Carlos Sierra
 
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsOracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsZohar Elkayam
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsEnkitec
 
Tanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata MigrationsTanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata MigrationsTanel Poder
 
Tanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools shortTanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools shortTanel Poder
 
How to Use EXAchk Effectively to Manage Exadata Environments
How to Use EXAchk Effectively to Manage Exadata EnvironmentsHow to Use EXAchk Effectively to Manage Exadata Environments
How to Use EXAchk Effectively to Manage Exadata EnvironmentsSandesh Rao
 
Oracle Database SQL Tuning Concept
Oracle Database SQL Tuning ConceptOracle Database SQL Tuning Concept
Oracle Database SQL Tuning ConceptChien Chung Shen
 
Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle Kyle Hailey
 
Oracle database performance tuning
Oracle database performance tuningOracle database performance tuning
Oracle database performance tuningYogiji Creations
 
SQL Monitoring in Oracle Database 12c
SQL Monitoring in Oracle Database 12cSQL Monitoring in Oracle Database 12c
SQL Monitoring in Oracle Database 12cTanel Poder
 
Understanding oracle rac internals part 1 - slides
Understanding oracle rac internals   part 1 - slidesUnderstanding oracle rac internals   part 1 - slides
Understanding oracle rac internals part 1 - slidesMohamed Farouk
 
Same plan different performance
Same plan different performanceSame plan different performance
Same plan different performanceMauro Pagano
 
Oracle Latch and Mutex Contention Troubleshooting
Oracle Latch and Mutex Contention TroubleshootingOracle Latch and Mutex Contention Troubleshooting
Oracle Latch and Mutex Contention TroubleshootingTanel Poder
 
Survey of some free Tools to enhance your SQL Tuning and Performance Diagnost...
Survey of some free Tools to enhance your SQL Tuning and Performance Diagnost...Survey of some free Tools to enhance your SQL Tuning and Performance Diagnost...
Survey of some free Tools to enhance your SQL Tuning and Performance Diagnost...Carlos Sierra
 
All of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL DeveloperAll of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL DeveloperJeff Smith
 
Oracle AWR Data mining
Oracle AWR Data miningOracle AWR Data mining
Oracle AWR Data miningYury Velikanov
 
The Top 5 Reasons to Deploy Your Applications on Oracle RAC
The Top 5 Reasons to Deploy Your Applications on Oracle RACThe Top 5 Reasons to Deploy Your Applications on Oracle RAC
The Top 5 Reasons to Deploy Your Applications on Oracle RACMarkus Michalewicz
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsCarlos Sierra
 

La actualidad más candente (20)

Adapting and adopting spm v04
Adapting and adopting spm v04Adapting and adopting spm v04
Adapting and adopting spm v04
 
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsOracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning Fundamentals
 
Tanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata MigrationsTanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata Migrations
 
Tanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools shortTanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools short
 
How to Use EXAchk Effectively to Manage Exadata Environments
How to Use EXAchk Effectively to Manage Exadata EnvironmentsHow to Use EXAchk Effectively to Manage Exadata Environments
How to Use EXAchk Effectively to Manage Exadata Environments
 
Oracle Database SQL Tuning Concept
Oracle Database SQL Tuning ConceptOracle Database SQL Tuning Concept
Oracle Database SQL Tuning Concept
 
Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle
 
Oracle database performance tuning
Oracle database performance tuningOracle database performance tuning
Oracle database performance tuning
 
SQL Monitoring in Oracle Database 12c
SQL Monitoring in Oracle Database 12cSQL Monitoring in Oracle Database 12c
SQL Monitoring in Oracle Database 12c
 
Understanding oracle rac internals part 1 - slides
Understanding oracle rac internals   part 1 - slidesUnderstanding oracle rac internals   part 1 - slides
Understanding oracle rac internals part 1 - slides
 
Same plan different performance
Same plan different performanceSame plan different performance
Same plan different performance
 
Oracle Latch and Mutex Contention Troubleshooting
Oracle Latch and Mutex Contention TroubleshootingOracle Latch and Mutex Contention Troubleshooting
Oracle Latch and Mutex Contention Troubleshooting
 
Survey of some free Tools to enhance your SQL Tuning and Performance Diagnost...
Survey of some free Tools to enhance your SQL Tuning and Performance Diagnost...Survey of some free Tools to enhance your SQL Tuning and Performance Diagnost...
Survey of some free Tools to enhance your SQL Tuning and Performance Diagnost...
 
All of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL DeveloperAll of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL Developer
 
Oracle AWR Data mining
Oracle AWR Data miningOracle AWR Data mining
Oracle AWR Data mining
 
Ash and awr deep dive hotsos
Ash and awr deep dive hotsosAsh and awr deep dive hotsos
Ash and awr deep dive hotsos
 
AWR and ASH Deep Dive
AWR and ASH Deep DiveAWR and ASH Deep Dive
AWR and ASH Deep Dive
 
The Top 5 Reasons to Deploy Your Applications on Oracle RAC
The Top 5 Reasons to Deploy Your Applications on Oracle RACThe Top 5 Reasons to Deploy Your Applications on Oracle RAC
The Top 5 Reasons to Deploy Your Applications on Oracle RAC
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning Fundamentals
 

Destacado

Adapting to Adaptive Plans on 12c
Adapting to Adaptive Plans on 12cAdapting to Adaptive Plans on 12c
Adapting to Adaptive Plans on 12cMauro Pagano
 
SQL Tuning, takes 3 to tango
SQL Tuning, takes 3 to tangoSQL Tuning, takes 3 to tango
SQL Tuning, takes 3 to tangoMauro Pagano
 
Is your SQL Exadata-aware?
Is your SQL Exadata-aware?Is your SQL Exadata-aware?
Is your SQL Exadata-aware?Mauro Pagano
 
Histograms in 12c era
Histograms in 12c eraHistograms in 12c era
Histograms in 12c eraMauro Pagano
 
Full Table Scan: friend or foe
Full Table Scan: friend or foeFull Table Scan: friend or foe
Full Table Scan: friend or foeMauro Pagano
 

Destacado (6)

Adapting to Adaptive Plans on 12c
Adapting to Adaptive Plans on 12cAdapting to Adaptive Plans on 12c
Adapting to Adaptive Plans on 12c
 
SQL Tuning, takes 3 to tango
SQL Tuning, takes 3 to tangoSQL Tuning, takes 3 to tango
SQL Tuning, takes 3 to tango
 
SQLd360
SQLd360SQLd360
SQLd360
 
Is your SQL Exadata-aware?
Is your SQL Exadata-aware?Is your SQL Exadata-aware?
Is your SQL Exadata-aware?
 
Histograms in 12c era
Histograms in 12c eraHistograms in 12c era
Histograms in 12c era
 
Full Table Scan: friend or foe
Full Table Scan: friend or foeFull Table Scan: friend or foe
Full Table Scan: friend or foe
 

Similar a SQL Plan Directives explained

Query optimizer vivek sharma
Query optimizer vivek sharmaQuery optimizer vivek sharma
Query optimizer vivek sharmaaioughydchapter
 
COA Chapter 1.pdf
COA Chapter 1.pdfCOA Chapter 1.pdf
COA Chapter 1.pdfAbelAteme
 
Discard inport exchange table & tablespace
Discard inport exchange table & tablespaceDiscard inport exchange table & tablespace
Discard inport exchange table & tablespaceMarco Tusa
 
Most useful queries
Most useful queriesMost useful queries
Most useful queriesSam Depp
 
Oracle sql tutorial
Oracle sql tutorialOracle sql tutorial
Oracle sql tutorialMohd Tousif
 
Why Use EXPLAIN FORMAT=JSON?
 Why Use EXPLAIN FORMAT=JSON?  Why Use EXPLAIN FORMAT=JSON?
Why Use EXPLAIN FORMAT=JSON? Sveta Smirnova
 
2018 db-rainer schuettengruber-beating-oracles_optimizer_at_its_own_game-pres...
2018 db-rainer schuettengruber-beating-oracles_optimizer_at_its_own_game-pres...2018 db-rainer schuettengruber-beating-oracles_optimizer_at_its_own_game-pres...
2018 db-rainer schuettengruber-beating-oracles_optimizer_at_its_own_game-pres...Rainer Schuettengruber
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptxVandanaBharti21
 
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdfNOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdfcookie1969
 
JDD 2016 - Tomasz Borek - DB for next project? Why, Postgres, of course
JDD 2016 - Tomasz Borek - DB for next project? Why, Postgres, of course JDD 2016 - Tomasz Borek - DB for next project? Why, Postgres, of course
JDD 2016 - Tomasz Borek - DB for next project? Why, Postgres, of course PROIDEA
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsNirav Shah
 
In Search of Plan Stability - Part 1
In Search of Plan Stability - Part 1In Search of Plan Stability - Part 1
In Search of Plan Stability - Part 1Enkitec
 
Denis Reznik "Оптимизация запроса. Не знаешь что делать? Делай то, что знаешь"
Denis Reznik "Оптимизация запроса. Не знаешь что делать? Делай то, что знаешь"Denis Reznik "Оптимизация запроса. Не знаешь что делать? Делай то, что знаешь"
Denis Reznik "Оптимизация запроса. Не знаешь что делать? Делай то, что знаешь"Fwdays
 
How to leave the ORM at home and write SQL
How to leave the ORM at home and write SQLHow to leave the ORM at home and write SQL
How to leave the ORM at home and write SQLMariaDB plc
 
Oracle CBO Fundamental
Oracle CBO FundamentalOracle CBO Fundamental
Oracle CBO FundamentalJAEGEUN YU
 

Similar a SQL Plan Directives explained (20)

Query optimizer vivek sharma
Query optimizer vivek sharmaQuery optimizer vivek sharma
Query optimizer vivek sharma
 
Cost Based Oracle
Cost Based OracleCost Based Oracle
Cost Based Oracle
 
COA Chapter 1.pdf
COA Chapter 1.pdfCOA Chapter 1.pdf
COA Chapter 1.pdf
 
Discard inport exchange table & tablespace
Discard inport exchange table & tablespaceDiscard inport exchange table & tablespace
Discard inport exchange table & tablespace
 
Most useful queries
Most useful queriesMost useful queries
Most useful queries
 
Oracle sql tutorial
Oracle sql tutorialOracle sql tutorial
Oracle sql tutorial
 
SQL
SQLSQL
SQL
 
Why Use EXPLAIN FORMAT=JSON?
 Why Use EXPLAIN FORMAT=JSON?  Why Use EXPLAIN FORMAT=JSON?
Why Use EXPLAIN FORMAT=JSON?
 
2018 db-rainer schuettengruber-beating-oracles_optimizer_at_its_own_game-pres...
2018 db-rainer schuettengruber-beating-oracles_optimizer_at_its_own_game-pres...2018 db-rainer schuettengruber-beating-oracles_optimizer_at_its_own_game-pres...
2018 db-rainer schuettengruber-beating-oracles_optimizer_at_its_own_game-pres...
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptx
 
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdfNOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
 
JDD 2016 - Tomasz Borek - DB for next project? Why, Postgres, of course
JDD 2016 - Tomasz Borek - DB for next project? Why, Postgres, of course JDD 2016 - Tomasz Borek - DB for next project? Why, Postgres, of course
JDD 2016 - Tomasz Borek - DB for next project? Why, Postgres, of course
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
 
Table functions - Planboard Symposium 2013
Table functions - Planboard Symposium 2013Table functions - Planboard Symposium 2013
Table functions - Planboard Symposium 2013
 
In Search of Plan Stability - Part 1
In Search of Plan Stability - Part 1In Search of Plan Stability - Part 1
In Search of Plan Stability - Part 1
 
Oracle 12c SPM
Oracle 12c SPMOracle 12c SPM
Oracle 12c SPM
 
Denis Reznik "Оптимизация запроса. Не знаешь что делать? Делай то, что знаешь"
Denis Reznik "Оптимизация запроса. Не знаешь что делать? Делай то, что знаешь"Denis Reznik "Оптимизация запроса. Не знаешь что делать? Делай то, что знаешь"
Denis Reznik "Оптимизация запроса. Не знаешь что делать? Делай то, что знаешь"
 
How to leave the ORM at home and write SQL
How to leave the ORM at home and write SQLHow to leave the ORM at home and write SQL
How to leave the ORM at home and write SQL
 
Dbms &amp; oracle
Dbms &amp; oracleDbms &amp; oracle
Dbms &amp; oracle
 
Oracle CBO Fundamental
Oracle CBO FundamentalOracle CBO Fundamental
Oracle CBO Fundamental
 

Último

Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...Akihiro Suda
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 

Último (20)

Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 

SQL Plan Directives explained

  • 1. SQL Plan Directives Explained Mauro Pagano
  • 2. Mauro Pagano • Consultant, working with both DBA and Devs • Oracle  Enkitec  Accenture • DBPerf and SQL Tuning • Training, Workshops, OUG • Free Tools (SQLd360, TUNAs360, Pathfinder) 2
  • 3. Some background • CBO makes mistakes – Complex code with challenging job – Based on • Statistical formula, not perfect in 100% cases • Partial knowledge of the data (stats) • Mistakes can translate into poor plans • Poor plans usually lead to poor performance 3
  • 4. How to avoid mistakes? • Improve quality of the model (Oracle) – Hard, lots of corner cases for CBO to handle • Improve quality of stats (kind of you) – Hard, need knowledge of data and queries • Reactively, learn from them to avoid next time – Allow to adapt to specific situation – Requires mistake to be made first though 4
  • 5. Cardinality Feedback • Oracle attempt of making CBO learning from its own mistakes – Introduced in 11.2 (11.1 as one-off) – Enhanced in 12.1 to deal with joins (but disabled in 12.2) • Might take a bit to learn – Kind of iterative approach, up to 5 “refined” attempts • Lessons learned as OPT_ESTIMATE hints (like STA), very specific – Not persisted to dictionary (same mistakes over time) • Few initial bugs gave it a bad reputation – SQL runs fine the first time, run again and runs slow! – Supposed to be transparent, not good when gives troubles  – Some people got burned, decided to turn off system-wise 5
  • 6. Table TAB1 drop table tab1; create table tab1 (n1 number, n2 number, n3 number); -- the data is strongly correlated insert into tab1 select mod(rownum, 100), mod(rownum, 100), mod(rownum, 100) from dual connect by rownum <= 100000; commit; exec dbms_stats.gather_table_stats(user,'TAB1'); 6
  • 7. Cardinality Feedback select count(*) from tab1 where n1 = 1 and n2 = 1 and n3 = 1 ---------------------------------------------------------------------- | Id | Operation | Name | Starts | E-Rows | A-Rows | ---------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | | 1 | | 1 | SORT AGGREGATE | | 1 | 1 | 1 | |* 2 | TABLE ACCESS STORAGE FULL| TAB1 | 1 | 1 | 1000 | ---------------------------------------------------------------------- ---------------------------------------------------------------------- | Id | Operation | Name | Starts | E-Rows | A-Rows | ---------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | | 1 | | 1 | SORT AGGREGATE | | 1 | 1 | 1 | |* 2 | TABLE ACCESS STORAGE FULL| TAB1 | 1 | 1000 | 1000 | ---------------------------------------------------------------------- Note ----- - statistics feedback used for this statement OPT_ESTIMATE (@"SEL$1" TABLE "TAB1"@"SEL$1" ROWS=1000.000000 ) 7 P(A and B and C) = P(A) * P(B) * P(C)
  • 8. SQL Plan Directives • New feature introduced in 12.1 – Several changes introduced in 12.2 • Designed to be transparent – SPDs are CBO generated – SPDs are leveraged by CBO once in place • Enabled by default in 12.1 • Disabled by default in 12.2 8 
  • 9. What’s the goal? • Help CBO make better estimations • Not SQL-specific – Can be re-used by similar SQLs • No specific adjustments like CFB • Keep track of ”risky paths” • Determine when to leverage other features – Dynamic Statistics – Instruct DBMS_STATS to create Column Groups 9
  • 10. What does SPD track? • Cardinality misestimates (usually underestimates) • “Risky paths” are – Data correlation on multi-column filter predicates – Uncommon join correlation – Data correlation on columns in GROUP BY • Potentially other paths can be tracked – For example, cardinality estimation at query block level • Many TYPEs could be supported – As of today, only existing TYPE is DYNAMIC_SAMPLING (*) 10
  • 11. How does SPD track them? • Entry in the data dictionary – opt_directive$, opt_finding_obj$ – Externalized as DBA views • Records table / columns involved – Different SQLs on same objects can use same SPD • Info read at hard parse and consumed accordingly – For example, triggering dynamic sampling • Used as warning “watch out when estimating X” 11
  • 12. SPD creation example select count(*) from tab1 where n1 = 1 and n2 = 1 and n3 = 1; (from 10053) SINGLE TABLE ACCESS PATH Single Table Cardinality Estimation for TAB1[TAB1] Table: TAB1 Alias: TAB1 Card: Original: 100000.000000 Rounded: 1 Computed: 0.100000 Non Adjusted: 0.10 … SPD: Generating finding id: type = 1, reason = 1, objcnt = 1, obItr = 0, objid = 135446, objtyp = 1, vecsize = 4, colvec = [1, 2, 3, ], fid = 29…48 SPD: Inserted felem, fid=29…48, ftype = 1, freason = 1, dtype = 0, dstate = 0, dflag = 0,… 12 Mistake! We know it’s 1K rows
  • 13. SPD creation example select directive_id, type, reason, notes from dba_sql_plan_directives where directive_id in (select directive_id from dba_sql_plan_dir_objects where owner = sys_context('userenv', 'session_user') and object_name = 'TAB1'); DIRECTIVE_ID TYPE REASON -------------------------- ---------------- ------------------------------------ 1149064327055029475 DYNAMIC_SAMPLING SINGLE TABLE CARDINALITY MISESTIMATE NOTES ----------------------------------------------------- <spd_note> <internal_state>NEW</internal_state> <redundant>NO</redundant> <spd_text>{EC(MPAGANO.TAB1)[N1, N2, N3]}</spd_text> </spd_note> 13
  • 14. SPD creation example select * from dba_sql_plan_dir_objects where owner = sys_context('userenv', 'session_user') and object_name = 'TAB1'; SUBOBJECT_NAME OBJECT ---------------- ------ N1 COLUMN N2 COLUMN N3 COLUMN TABLE  NOTES is at the TABLE level NOTES -------------------------------------------------------------------------- <obj_note> <equality_predicates_only>YES</equality_predicates_only> <simple_column_predicates_only>YES</simple_column_predicates_only> <index_access_by_join_predicates>NO</index_access_by_join_predicates> <filter_on_joining_object>NO</filter_on_joining_object> </obj_note> 14
  • 15. SPD creation (another) example DIRECTIVE_ID TYPE REASON --------------------- ---------------- ------------------------------------ 7426975260533728013 DYNAMIC_SAMPLING JOIN CARDINALITY MISESTIMATE NOTES --------------------------------------------------------------------------------- <spd_note> <internal_state>NEW</internal_state> <redundant>NO</redundant> <spd_text>{(SYS.OPT_DIRECTIVE$) - (SYS.OPT_FINDING_OBJ$)}</spd_text> </spd_note> DIRECTIVE_ID OBJECT_NAME OBJECT NOTES --------------------- ---------------- ------ ------------------------------------ 7426975260533728013 OPT_DIRECTIVE$ TABLE <obj_note> EVERYTHING NO </obj_note> 7426975260533728013 OPT_FINDING_OBJ$ TABLE <obj_note> EVERYTHING NO </obj_note> 15
  • 16. SPD creation example • SPD_TEXT reports the “risky path” – Specific format within { }, built on the fly – One or more tables – Columns included for single table – Format includes flag on predicate (where applicable) • E = Equality predicates • C = Simple column predicates • J = Index access by join predicates • F = Filter on joining object 16
  • 17. SPD creation example • INTERNAL_STATE shows where in the lifecycle SPD is – NEW – SPD just created (flushed to disk), not used by SQLs yet – MISSING_STATS – CBO used to help estimations, likely used ADS – HAS_STATS – Stats (likely Column Group) helped CBO, SPD “off” – PERMANENT – Stats not enough, SPD will stay “on” (ADS) • First two states are transitory • Last two states are definitive – HAS_STATS -> PERMANENT if stats don’t help “enough” – SPD goes trough HAS_STATS before PERMANENT 17
  • 18. How does CBO use SPD? • During hard parse applicable SPDs are looked at – Search done at multiple levels – Objects looked up by obj# • Many SPDs are not “current” anymore – Obsoleted / Superseded • The valid ones are used – Action triggered at the specific level – As of today only CBO action is Dynamic Sampling (ADS) 18
  • 19. SPD usage example – single table select count(*) from tab1 where n1 = 1 and n2 = 1 and n3 = 1; Query Block SEL$1 (#0) Applicable DS directives: dirid = 16004857199080262683, state = 1, flags = 1, loc = 1 {EC(135484)[1, 2, 3]} … SINGLE TABLE ACCESS PATH Single Table Cardinality Estimation for TAB1[TAB1] SPD: Directive valid: dirid = 16004857199080262683, state = 1, flags = 1, loc = 1 {EC(135484)[1, 2, 3]} … Table: TAB1 Alias: TAB1 Card: Original: 100000.000000 >> Single Tab Card adjusted from 0.100000 to 1000.000000 due to adaptive dynamic sampling Rounded: 1000 Computed: 1000.000000 Non Adjusted: 0.100000 19
  • 20. SPD usage example – single table select count(*) from tab1 where n1 = 1 and n2 = 1 and n3 = 1; (from 10046 during hard parse) PARSING IN CURSOR #140175694530408 … SELECT /* DS_SVC */ /*+ … result_cache(snapshot=3600) */ SUM(C1) FROM (SELECT /*+ qb_name("innerQuery") NO_INDEX_FFS( "TAB1")*/ 1 AS C1 FROM "TAB1" "TAB1" WHERE ("TAB1"."N1"=1) AND ("TAB1"."N2"=1) AND ("TAB1"."N3"=1) ) InnerQuery STAT #140175694530408 id=1 cnt=1 op='RESULT CACHE 145cmpkvg18at78z0dz8ptfwfw STAT #140175694530408 id=2 cnt=1 op='SORT AGGREGATE STAT #140175694530408 id=3 cnt=1000 op='TABLE ACCESS FULL TAB1 20
  • 21. Use of SPD – single table conclusions • Allows CBO to get better estimation on the fly • Cool but not the best approach in the long run – ADS even if fast still takes some time (overhead) – Longer parses cause most of the troubles in real-life • Same result achieved with Column Group (often) • Real goal is – Put a band-aid until Column Group in place – Instruct DBMS_STATS to collect Column Groups – Verify if Column Group helped, if not keep SPD active 21
  • 22. SPD usage example – join select … from dba_sql_plan_directives a, dba_sql_plan_dir_objects b where a.directive_id = b.directive_id and a.created >= sysdate-1/24 order by a.create Query Block SEL$AF75E632 (#0) Applicable DS directives: dirid = 10275726999217578058,state = 1,flags = 1,loc = 1{(612)[7]} dirid = 13360472976608779416,state = 1,flags = 1,loc = 1{(612)[]} dirid = 7426975260533728013,state = 1,flags = 1,loc = 2{(612)[]; (608)[]} dirid = 4318109285801377208,state = 1,flags = 1,loc = 1{(608)[]} dirid = 1215330379436061558,state = 1,flags = 1,loc = 2{(608)[]; (22)[]} dirid = 17607835686132426105,state = 5,flags = 1,loc = 1{C(22)[1]} dirid = 17030158486626056340,state = 1,flags = 1,loc = 2{(612)[];(608)[]; (22)[]} 22
  • 23. SPD usage example – join (from 10053) SPD: Directive valid: dirid = 7426975260533728013,…,loc = 2 {(612)[]; (608)[]} … Join Card: 13.000000 = outer (9.000000) * inner (13.000000) * sel (0.111111) >> Join Card adjusted from 13.000000 to 16.000000 due to adaptive dynamic sampling, prelen=2 Adjusted Join Cards: adjRatio=1.230769 cardHjSmj=16.000000 … Join Card - Rounded: 16 Computed: 16.000000 (from 10046) PARSING IN CURSOR #139704599064112 SELECT /* DS_SVC */ /*+ … result_cache(snapshot=3600) */ SUM(C1) FROM (SELECT /*+ qb_name("innerQuery") NO_INDEX_FFS( "F#0") */ 1 AS C1 FROM "SYS"."OPT_FINDING$" "F#0", "SYS"."OPT_DIRECTIVE$" "D#1" WHERE ("D#1"."F_ID"="F#0"."F_ID") ) innerQuery 23
  • 24. Use of SPD – join conclusions • Allows CBO to get better estimations for joins – Provide adjustment ratio – No stored stats provide this info (really cool) – OPT_ESTIMATE is the closest thing, but still different • Suffers from same side effects of single table – Magnified by longer ADS on joins – SPD from joins can be SUPERSEDED (HAS_STATS) too (*) 24
  • 25. What happens to SPD after usage? (from 10053 of the second parse) SPD: Generating finding id: type = 1, reason = 1, objcnt = 1, obItr = 0, objid = 135484, objtyp = 1, vecsize = 4, colvec = [1, 2, 3, ], fid = 108…890 SPD: Inserted felem, fid=108…890,ftype = 0,freason = 0,dtype = 1,dstate = 2,… SPD: qosdRecDSDirChange dirid = 16004857199080262683, retCode = UPDATED (from dictionary) NOTES ---------------------------------------------------- <spd_note> <internal_state>MISSING_STATS</internal_state> <redundant>NO</redundant> <spd_text>{EC(MPAGANO.TAB1)[N1, N2, N3]}</spd_text> </spd_note> 25 CBO is aware there is a “weak path” and it had to use ADS in order to limit the damage
  • 26. SPD instructs DBMS_STATS (from 10046 during parse) merge into sys.col_group_usage$ d using (select :1 obj#, :2 cols from dual) s on (d.obj# = s.obj# and d.cols = s.cols) when matched then update set d.timestamp = :3, d.flags = d.flags + :4 - bitand(d.flags, :4) when not matched then insert (obj#, cols, timestamp, flags) values (:1,:2,:3,:4) :1 -> 135484 :2 -> “1,2,3” :3 -> ”1/12/2017 8:33:7” :4 -> 17 (from 10046 during DBMS_STATS run) SELECT ... FROM SYS.COL_GROUP_USAGE$ CU … UPDATE SYS.COL_GROUP_USAGE$ C SET C.FLAGS = FLAGS + 8 - BITAND(FLAGS, 8) WHERE OBJ# = :B2 AND COLS = :B1 COLUMN_NAME DATA_DEFAULT NUM_DISTINCT HISTOGRAM ------------------------------ ------------------------------------ ------------ --------- SYS_STSOYQUEIAZ7FI9DV53VLN$$$0 SYS_OP_COMBINED_HASH("N1","N2","N3") 100 FREQUENCY 26
  • 27. Then what happens to SPD? SINGLE TABLE ACCESS PATH Single Table Cardinality Estimation for TAB1[TAB1] SPD: Directive valid: dirid=160…83,state=2,flags=1,loc=1{EC(…)[1, 2, 3]} ... ColGroup (#1, VC) SYS_STSOYQUEIAZ7FI9DV53VLN$$$0 Col#: 1 2 3 … Table: TAB1 Alias: TAB1 Card: Original: 100000.000000 Rounded: 1000 Computed: 1000.000000 … SPD: Inserted felem, fid=10…890,…,dtype = 1, dstate = 3, dflag = 1,… … NOTES ------------------------------------------------------ <spd_note> <internal_state>HAS_STATS</internal_state> <redundant>NO</redundant> <spd_text>{EC(MPAGANO.TAB1)[N1, N2, N3]}</spd_text> </spd_note> 27 If the CG doesn’t help then SPD is marked as PERMANENT and behaves like MISSING_STATS (ADS used) No mistake 
  • 28. SPD DS results • DS results are stored – Multiple SQL IDs (different parse) would trigger same DS SQL • In 12.1 Result Cache is used – Results have lifetime of 3600 secs – Result Cache itself had some troubles in the past • Mostly latch contention under heavy load • In 12.2 SPD repository itself used to store results – Makes them more permanent – Search in the repo based on ADS recursive SQL ID 28
  • 29. Is my SQL using SPD? • Many SPDs can reference just a few objects – Even simple SQL can use many SPDs • CBO looks into them and pick up valid – A subset of Valid are Used • OTHER_XML reports #valid/#used • DBMS_XPLAN to show if SQL is using SPDs – DISPLAY (explain plan) reports which ones – DISPLAY_CURSOR (execution) reports just #used 29
  • 30. DBMS_SPD • Oracle seeded package to manage SPDs • Move via pack/unpack in stgtab like baselines – Base 12.1.0.2 has bugs that impact xfr of SPDs • Allow to manually flush SPDs to disk – Instead of waiting 15 minutes • Can set basic preferences – For example, retention period before auto drop 30
  • 31. Why the bad reputation? • Some issues caused large negative impact – Hard to spot when the feature is “transparent” – Main symptoms mutex / latch contention • SPDs generated based on workload – Performance tends get better “with time” • Makes performance in Prod bad on day 1 • Even though it was great in Test on day 200 – Upgrades need extra steps (move SPDs) • Limited number of Column Groups per table – Less useful CG could be created instead of useful ones 31
  • 32. Most common problems • Most issues are ultimately caused by ADS – ADS running for too long – ADS executed even for small app SQLs (no need) • Longer parse caused by sampling – Parse is a serialized operation, parser hold X mutex – Other sessions with same SQL are stuck – Easy for snowball effect to trigger • Result Cache maybe not the best decision? 32
  • 33. Solutions? • I’m personally a big fan of SPD – But I’m not a Production DBA  • Many products require to turn it off in 12.1 – Bad habit was to turn off all Adaptive Features via parameter – Oracle patch split parameter in two (AdaptPlans vs AdaptStats) • So just SPD and ADS can be disabled, Adaptive Plans stay ON • Oracle recommended solution in 12.1, SPD OFF with patch+param • Disabled by default in 12.2 – When enabled it seems a bit more conservative – Too early to judge how it will behave in 12.2 prod 33
  • 34. Summary • Goal is improve CBO estimations over time – Tracking correlation in many areas – Putting stats / ADS in place to catch that • Might take time and introduce instability – Makes comparing two systems harder • Generated issues in 12.1 – Oracle recommends to disable by default – Enable back specific systems might be beneficial 34
  • 35. 35
  • 36. Contact Information • http://mauro-pagano.com – Email • mauro.pagano@gmail.com – Free tools to download • SQLd360 • TUNAs360 • PAthfinder 36

Notas del editor

  1. Not an easy task for Oracle to improve the code, the complex you make the model the easier it is to break stuff and not cover all ground Quality of the stats is easier to improve but it’s on you, not much on Oracle Reactively it’s the easiest one, but it takes a mistake to be made to realize it was a mistake
  2. Mention a little about tab1 having strong correlation, DDL will come later
  3. Presentation focused on the feature itself, differences between 12.1 and 12.2 presented as we go through the details If disabled in 12.2, why are we even talking about it? Reason is SPD can help a lot, so knowing what they are can help to realize when to turn it back on
  4. SPD is kind of built on top of CFB experience, slightly different goal but ironing out things that could have been done “maybe differently” in CFB Main difference is CFB is SQL-ID (cursor-based), while SPD is “object” based, track relationship across entities
  5. The TYPE is kind of the action to take (which feature to use) based on the finding In 12.2 we have DYNAMIC_SAMPLING_RESULT too
  6. It’s like taking notes during a parse and “similar” parse can re-use those notes
  7. SPD here is used to track a potential mistake made when filtering on (n1,n2,n3) together. We don’t know it yet, we just “keep track” of it qosdGenFindId / qosdDumpSgaFind SPD: Generating finding id: type = 1, reason = 1, objcnt = 1, obItr = 0, objid = 135443, objtyp = 1, vecsize = 4, colvec = [1, 2, 3, ], fid = 3950674910938887825 SPD: Inserted felem, fid=3950674910938887825, ftype = 1, freason = 1, dtype = 0, dstate = 0, dflag = 0, ver = NO, keep = NO (gdb) bt #0 0x000000000218d550 in qosdGenFindId () #1 0x000000000218d172 in qosdRecFindInCFB () #2 0x00000000017453e3 in kkocfbNodeAllocated () #3 0x0000000001fc60c3 in qknstAllocate () #4 0x0000000001fb5689 in qkatab () #5 0x0000000001f85459 in qkajoi () #6 0x0000000001f97b0f in qkaqkn () #7 0x0000000001f91049 in qkadrv () …
  8. Doesn’t matter how many times the SQL is executed, until the SPD is flushed to disk even a new hard parse (for example, because of different CBO environment) wouldn’t use it From a 10053 for two consecutive executions with different CBO env SPD: Return code in qosdDSDirSetup: NOCTX, estType = TABLE SPD: Generating finding id: type = 1, reason = 1, objcnt = 1, obItr = 0, objid = 135450, objtyp = 1, vecsize = 4, colvec = [1, 2, 3, ], fid = 17828064754844457670 SPD: Inserted felem, fid=17828064754844457670, ftype = 1, freason = 1, dtype = 0, dstate = 0, dflag = 0, ver = NO, keep = NO SPD: Return code in qosdDSDirSetup: NOCTX, estType = TABLE SPD: Generating finding id: type = 1, reason = 1, objcnt = 1, obItr = 0, objid = 135450, objtyp = 1, vecsize = 4, colvec = [1, 2, 3, ], fid = 17828064754844457670 SPD: Modified felem, fid=17828064754844457670, ftype = 1, freason = 1, dtype = 0, dstate = 0, dflag = 0, ver = YES, keep = YES
  9. SPD for join generated by “SELECT DIRECTIVE_ID FROM DBA_SQL_PLAN_DIRECTIVES” triggered recursively by DBMS_SPD.DROP_SQL_PLAN_DIRECTIVE SPD reference only tables, no column It “exposes” the relationship between the two objects
  10. select directive_id, xt.text from dba_sql_plan_directives s, xmltable ('/spd_note' passing s.notes columns text varchar2(100) PATH 'spd_text') xt where xt.text like'{F%'; F means there was a filter on a column for one of the objects involved in this join SPD Not the join key itself, another column (basically filtering out part of one of the two tables)
  11. Focus on status “alone” first, then (next slides) on transition Bug 20311655 (that will probably be closed as dup of another bug, which is closed as not a bug)
  12. Multiple level since SPD supports multiple levels (single table, join, QB, etc)
  13. CBO identifies there is a SPD in place for obj 135484 col ids 1,2,3 Used at the right level (SINGLE TABLE ACCESS PATH)
  14. Mention that parse is a serialized operation so everything that increases parse time affect system scalability
  15. select a.directive_id, a.state, a.reason, a.created, b.object_name, a.notes from dba_sql_plan_directives a, dba_sql_plan_dir_objects b where a.directive_id = b.directive_id and a.created >= sysdate-1/24 order by a.create
  16. Here the SPD is used during Join order[] evaluation, different section of the code Oracle here comes up with adjRatio instead of fixed number, used because you can have different joins (including semi, etc etc)
  17. (*) even though likely just temporary (unless an identical one one is created), bug 20311655 is an example (this bug is a duplicate of something else, close as not a bug) The HAS_STATS here comes from CFB, which makes the SPD look like ”superseded”, until the next parse comes and CFB info aren’t around, then SPD becomes PERMANENT
  18. https://mauro-pagano.com/2016/11/28/something-new-about-sql-plan-directives-and-12-2/ TYPE = DYNAMIC_SAMPLING_RESULT REASON = VERIFY CARDINALITY ESTIMATE SPD_TEXT = {(MPAGANO.TAB1, num_rows=100000) - (SQL_ID:7pjdz422db2xk, T.CARD=1000[-2 -2])}
  19. alter session set events 'trace [SQL_Plan_Directive.*]';
  20. Three main issues, first one the biggest (last one a bit of a corner case but not much)
  21. Long parse because high number of SPD is fixed by bug 20807398 that supersedes 20465582
  22. https://support.oracle.com/epmos/faces/DocContentDisplay?id=2187449.1