SlideShare a Scribd company logo
www.sagecomputing.com.au
penny@sagecomputing.com.au
Meet the Cost Based
Optimiser in 11g
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
penny@sagecomputing.com.au
A Characteristic Problem
I haven’t changed anything
Its really slow this morning
I did the same thing yesterday and it was fine
Actually its OK now
No its not
Thank you so much you’ve fixed it (I haven’t done anything)
Oracle Version < 9
SELECT COUNT(l.quantity)
FROM bookings_skew l
WHERE resource_code = :v1;
‘BRLG’
<Version 9 database
No bind peeking
Shared Pool
SELECT COUNT(l.quantity)
FROM bookings_skew_large l
WHERE resource_code = :v1;
‘PC1’
FULL SCAN
How many
rows do I
expect?
Oracle Version < 9
SELECT COUNT(l.quantity)
FROM bookings_skew l
WHERE resource_code = :v1;
‘BRLG’
<Version 9 database
No bind peeking
Shared Pool
SELECT COUNT(l.quantity)
FROM bookings_skew_large l
WHERE resource_code = :v1;
‘PC1’
FULL SCAN
How many
rows do I
expect?
What is Bind Peeking?
SELECT COUNT(l.quantity)
FROM bookings_skew l
WHERE resource_code = :v1;
‘PC1’
>=Version 9 database
Bind peeking
Shared Pool
SELECT COUNT(l.quantity)
FROM bookings_skew l
WHERE resource_code = :v1;
‘BRLG’
INDEXED
ACCESS
How many
rows do I
expect?
Histogram – Minority First
***************************************
SINGLE TABLE ACCESS PATH
Column (#3): RESOURCE_CODE(VARCHAR2)
AvgLen: 5.00 NDV: 9 Nulls: 0 Density: 9.0892e-008
Histogram: Freq #Bkts: 9 UncompBkts: 5966 EndPtVals: 9
Table: BOOKINGS_SKEW Alias: L
Card: Original: 5464800 Rounded: 43968 Computed: 43967.55 Non Adjusted: 43967.55
Access Path: TableScan
Cost: 7693.35 Resp: 7693.35 Degree: 0
Cost_io: 7426.00 Cost_cpu: 1555877511
Resp_io: 7426.00 Resp_cpu: 1555877511
Access Path: index (AllEqRange)
Index: BK_RESSKEW
resc_io: 2399.00 resc_cpu: 37397785
ix_sel: 0.0080456 ix_sel_with_filters: 0.0080456
Cost: 2405.43 Resp: 2405.43 Degree: 1
Best:: AccessPath: IndexRange Index: BK_RESSKEW
Cost: 2405.43 Degree: 1 Resp: 2405.43 Card: 43967.55 Bytes: 0
***************************************
RESO COUNT(*)
---- ----------
VCR1 495711
CONF 495720
LNCH 743576
BRSM 743583
PC1 47858
FLPC 495720
BRLG 1739277
TAP1 247864
VCR2 495715
Histogram – Minority First
What is the CBO good at in 10g?
Data Condition Literal/Bind Var Histogram
Even Distribution Equality Literal N/A
Even Distribution Equality Bind N/A
Skewed Equality Literal NO
Skewed Equality Literal YES
Skewed Equality Bind NO
Skewed Equality Bind YES
Even Distribution Range Bind N/A
Bind Peeking + Adaptive Cursors in 11g
Statements with bind variables (+histograms) are bind sensitive
The first time you execute a statement with different selectivity it
uses the original plan
The second time it changes the plan and become bind aware
New values will use a plan for the appropriate selectivity range
Adaptive Cursors Views
V$SQL
V$SQL_CS_SELECTIVITY
Adaptive Cursors Views
V$SQL_CS_HISTOGRAM
V$SQL_CS_STATISTICS
PLSQL has soft parse avoidance
Implicit cursor
Explicit cursor
Native dynamic SQL
Ref cursor
Session_cached_cursors = 0
No default adaptive cursor functionality
Default adaptive cursor functionality
Adaptive Cursors Functionality
Bind variable with Equality and Histogram
Not for range conditions
Bind variable with Equality and Histogram
Range conditions
Does not support LIKE
/*+ BIND_AWARE */
Adaptive Cursors 11.1.0.6
Adaptive Cursors 11.1.0.7 and 11.2
So where are we now?
Our own code – works properly first time with hint
in SQL and PL/SQL
Packages – will still get it wrong once
and won’t use adaptive cursors for PL/SQL
at all unless we set session cached cursors =0
Meet the CBO in Version 11g
Meet the CBO in Version 11g
Meet the CBO in Version 11g
Meet the CBO in Version 11g
Meet the CBO in Version 11g
Meet the CBO in Version 11g
Meet the CBO in Version 11g
Meet the CBO in Version 11g
xxxxxxxxxxxxxxxxxxxxx
Maybe you should buy an
Exadata box
Bind Peeking + Adaptive Cursors Summary
Your code
Packages
Minimise statement invalidations
Consider running the statement with minority and majority value on start up
Use the BIND_AWARE hint (SQL and PL/SQL) for skewed data
Use ref cursors (if hints are not allowed)
?manually hack an outline/profile
PL/SQL – set session_cached_cursors = 0 (temporarily)
Adaptive Cursors What we Really Need
Once a statement has been bind aware it knows next time
its parsed
?SQL *Profile to indicate bind aware
Adaptive Cursors Persistence
Try to get rid of your hints
_optimizer_ignore_hints
Create additional statistics
Set _optimizer_ignore_hints to TRUE
Test the app
If it runs OK remove your hints
58 New Hints
SELECT name, inverse, sql_feature, version
FROM v$sql_hint
WHERE version like '11%'
ORDER BY version desc, name
New Hints
New Hints
IGNORE_ROW_ON_DUPKEY_INDEX
This has nothing to do with optimisation I just like it
New Hints
MONITOR
NO_MONITOR
select /*+ MONITOR */
count(comments) from bookings ;
select DBMS_SQLTUNE.REPORT_SQL_MONITOR(
session_id=>sys_context('userenv','sid'),
report_level=>'ALL') as report
from dual;
Much More Query Transformation
SELECT e.event_no, e.start_date, sum(cost) totcost
FROM events_large e, bookings_large b
WHERE e.event_no = b.event_no
GROUP BY e.event_no, e.start_date
alter session set tracefile_identifier = Penny
alter session set events '10053 trace name context forever'
explain plan for
SELECT e.event_no, e.start_date, sum(cost) totcost
FROM events_large e, bookings_large b
WHERE e.event_no = b.event_no
GROUP BY e.event_no, e.start_date;
alter session set events '10053 trace name context off'
Much More Query Transformation
SELECT e.event_no, e.start_date, sum(cost) totcost
FROM events_large e, bookings_large b
WHERE e.event_no = b.event_no
GROUP BY e.event_no, e.start_date
10G – JOIN before the GROUP BY
Meet the CBO in Version 11g
11G – GROUP BY before the JOIN
Meet the CBO in Version 11g
Meet the CBO in Version 11g
Meet the CBO in Version 11g
Meet the CBO in Version 11g
Meet the CBO in Version 11g
10G – NOT IN (PROMISE_NO NULLABLE)
10G – KILLED AFTER 1 hr
11G – Rewrite as Null Aware Anti join
Meet the CBO in Version 11g
10G – Full Outer Join – default behaviour
Meet the CBO in Version 11g
From version 10.2.0.3
SELECT /*+ NATIVE_FULL_OUTER_JOIN */
count(e.comments) nume,
count (b.comments) numb
FROM events_large e
FULL OUTER JOIN bookings_large b
ON (e.event_no = b.event_no)
10G – Full Outer Join – hint
11G Native Full Outer Join
Meet the CBO in Version 11g
Check the differences in plans
Upgrading
Meet the CBO in Version 11g
Meet the CBO in Version 11g
Meet the CBO in Version 11g
Oracle 10g – Run a whole load of random statements – all of
which require parsing
Oracle 11g – Run a whole load of random statements – all of
which require parsing
So Tuning the Shared
Pool becomes more
important,
but overall the 11g
CBO is pretty smart
Results Cache
SGA
populate invalidate
Most recently
used
SQL/PL/SQL
SQL Query
Results Cache
Results Cache
PL/SQL Function
Results Cache
Most recently used result sets
Buffer Cache Library Cache
Most recently
data
Query Results Cache
RESULT_CACHE_MODE MANUAL
FORCE
/*+RESULT_CACHE */
/*+ NO_RESULT_CACHE */
SELECT /*+ RESULT_CACHE */
count(b.comments)
FROM train1.events_large e, train1.bookings_large b
WHERE e.org_id = :v1
AND e.event_no = b.event_no
AND e.comments = :v2;
PL/SQL Function Results Cache
CREATE OR REPLACE FUNCTION quantity_booked
(p_resource_code in resources.code%TYPE,p_event_date in date)
RETURN NUMBER
RESULT_CACHE
IS
v_total_booked number := 0;
BEGIN
SELECT sum(b.quantity)
INTO v_total_booked
FROM bookings b, events e
WHERE e.event_no = b.event_no
AND p_event_date between e.start_date and e.end_date
AND b.resource_code = p_resource_code;
RETURN (v_total_booked);
END;
Monitoring the Results Cache
SELECT * FROM v$result_cache_memory
SELECT * FROM v$result_cache_objects
SELECT * FROM v$result_cache_statistics
SELECT * FROM v$result_cache_dependency
RESULT_CACHE_MAX_SIZE
RESULT_CACHE_MAX_RESULT
Explain Plan
Meet the CBO in Version 11g
Setting Statistics Gathering Defaults
GET_PARAM
SET_PARAM Procedure
GET_PREFS Function
SET_TABLE_PREFS (set for individual tables)
SET_DATABASE_PREFS (sets for all tables, can include/exclude
SYS tables)
SET_GLOBAL_PREFS – for new objects
Obsolete:
Use:
Gathering Statistics
Early CBO: “Make sure you gather statistics regularly”
Later CBO: “Don’t gather statistics unless data patterns
change”
BUT
If you have new majority values you need to recreate the
histogram
Gathering and Publishing
Set preferences to PUBLISH = ‘FALSE’
Gather Statistics
user_tab_pending_stats
user_ind_pending_stats
user_col_pending_stats
Run test case
Alter session set
optimizer_pending_statistics
= TRUE Worse
Better
dbms_stats.
delete_pending_stats
dbms_stats.
publish_pending_stats
user_tab_statistics
user_ind_statistics
user_tab_col_statistics
Gathering and Publishing
SELECT obj#, TO_CHAR(savtime,'dd/mm/yyyy') save_time,
rowcnt, blkcnt, avgrln, samplesize, analyzetime
FROM wri$_optstat_tab_history
ORDER BY savtime desc
CBO - Instability
CBO
?access
path
decision
System parameters
Session parameters
Optimiser StatisticsIndexes
SQL Profile
Software version
System statistics
DBA playing around
CBO - Instability
– Leave it all alone
– SQL Plan Management
– Store plan baseline
– Plans not used till accepted
– Manually accept or
– Allow Oracle to evolve plans
Solution 1
Solution 2
SQL Plan Management
Manual capture
DBMS_SPM.LOAD_PLANS_FROM_SQLSET
DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE
Auto capture of repeatable statements
OPTIMIZER_CAPTURE_SQL_PLAN_BASELINE = TRUE
Baseline =
(Stored and Accepted plans)
SQL Management Base
New Plan
identified during
execution
Stored not accepted
SQL Tuning Advisor identifies new
plan – SQL*Profile accepted
Auto accept of new plan
(if it performs better)
DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE
Manual load/accept of new plan
DBMS_SPM.LOAD_PLANS_FROM_SQLSET
DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE
www.sagecomputing.com.au
penny@sagecomputing.com.au
SAGE Computing Services
Customised Oracle Training Workshops and Consulting
Questions?
www.sagecomputing.com.au
penny@sagecomputing.com.au

More Related Content

ODP
Oracle SQL Advanced
PPTX
Common Coding and Design mistakes (that really mess up performance)
PDF
Ngrx slides
PDF
PerlApp2Postgresql (2)
PDF
Higher-Order Components — Ilya Gelman
PDF
Recompacting your react application
PPTX
Enterprise Data Validation
ODP
SQL Tunning
Oracle SQL Advanced
Common Coding and Design mistakes (that really mess up performance)
Ngrx slides
PerlApp2Postgresql (2)
Higher-Order Components — Ilya Gelman
Recompacting your react application
Enterprise Data Validation
SQL Tunning

What's hot (12)

PPTX
Java script – basic auroskills (2)
PPTX
Ngrx: Redux in angular
PDF
Table partitioning in PostgreSQL + Rails
PDF
QuirksofR
PPTX
Sql Functions And Procedures
DOC
Oracle 11g Invisible Indexes
PDF
Window functions with SQL Server 2016
PPTX
Part3 Explain the Explain Plan
PPT
Ejb5
PPT
Advanced dot net
PPTX
Web Developer make the most out of your Database !
Java script – basic auroskills (2)
Ngrx: Redux in angular
Table partitioning in PostgreSQL + Rails
QuirksofR
Sql Functions And Procedures
Oracle 11g Invisible Indexes
Window functions with SQL Server 2016
Part3 Explain the Explain Plan
Ejb5
Advanced dot net
Web Developer make the most out of your Database !
Ad

Viewers also liked (16)

PPT
ЭстраЮа Đ¶Ò±Đ»ĐŽŃ‹Đ·ĐŽĐ°Ń€Ń‹
PDF
Yeosh Bendayan - Push Button Productions
DOCX
DHBA Newsletter Vol. 10 Issue 6
DOCX
cĂŽng ty làm phim quảng cáo tốt nháș„t
DOCX
HE WHO FINDETH A WILD CAT
PDF
AnaStan
PDF
Fairfax 2015 World Police & Fire Games Official Athlete Entry Book
DOCX
Introduction to programming
PDF
Preguntas analizadas lenguaje saber 9
PDF
013115IRH_SeniorExecutiveLiving
PDF
VolunteerAR2005-JLuetzowForNationalCity
PDF
Gost r 53938 2010
PDF
Schisina Ghost Villages, Sicily
PDF
01. instalasi software oracle database
PPTX
My last vacations
PPTX
ЭстраЮа Đ¶Ò±Đ»ĐŽŃ‹Đ·ĐŽĐ°Ń€Ń‹
Yeosh Bendayan - Push Button Productions
DHBA Newsletter Vol. 10 Issue 6
cĂŽng ty làm phim quảng cáo tốt nháș„t
HE WHO FINDETH A WILD CAT
AnaStan
Fairfax 2015 World Police & Fire Games Official Athlete Entry Book
Introduction to programming
Preguntas analizadas lenguaje saber 9
013115IRH_SeniorExecutiveLiving
VolunteerAR2005-JLuetzowForNationalCity
Gost r 53938 2010
Schisina Ghost Villages, Sicily
01. instalasi software oracle database
My last vacations
Ad

Similar to Meet the CBO in Version 11g (20)

PDF
Bind Peeking - The Endless Tuning Nightmare
PDF
New Tuning Features in Oracle 11g - How to make your database as boring as po...
PDF
The Cost Based Optimiser in 11gR2
PDF
Whose fault is it? - a review of application tuning problems
PDF
Results cache
PPTX
Oracle 12c SPM
PPTX
Oracle Query Optimizer - An Introduction
PDF
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
PDF
Database and application performance vivek sharma
PDF
Transformations - how Oracle rewrites your statements
PDF
Find it. Fix it. Real-World SQL Tuning Cases with Karen Morton
PPT
Oracle SQL, PL/SQL Performance tuning
PDF
Tuning SQL for Oracle Exadata: The Good, The Bad, and The Ugly Tuning SQL fo...
PDF
Databse & Technology 2 | Connor McDonald | Managing Optimiser Statistics - A ...
PDF
Cbo100053
PDF
How Can I tune it When I Can't Change the Code?
PDF
Improving the Performance of PL/SQL function calls from SQL
PPSX
Cost Based Oracle
PDF
query_tuning.pdf
PDF
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdf
Bind Peeking - The Endless Tuning Nightmare
New Tuning Features in Oracle 11g - How to make your database as boring as po...
The Cost Based Optimiser in 11gR2
Whose fault is it? - a review of application tuning problems
Results cache
Oracle 12c SPM
Oracle Query Optimizer - An Introduction
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
Database and application performance vivek sharma
Transformations - how Oracle rewrites your statements
Find it. Fix it. Real-World SQL Tuning Cases with Karen Morton
Oracle SQL, PL/SQL Performance tuning
Tuning SQL for Oracle Exadata: The Good, The Bad, and The Ugly Tuning SQL fo...
Databse & Technology 2 | Connor McDonald | Managing Optimiser Statistics - A ...
Cbo100053
How Can I tune it When I Can't Change the Code?
Improving the Performance of PL/SQL function calls from SQL
Cost Based Oracle
query_tuning.pdf
NOCOUG_201311_Fine_Tuning_Execution_Plans.pdf

More from Sage Computing Services (9)

PDF
Oracle XML DB - What's in it for me?
PDF
Aspects of 10 Tuning
PPT
Back to basics: Simple database web services without the need for SOA
PDF
Lost without a trace
PDF
Take a load off! Load testing your Oracle APEX or JDeveloper web applications
PDF
Application Express - A web development environment for the masses - and for ...
PPTX
OHarmony - How the Optimiser works
PPTX
Oracle Discoverer is dead - Where to next for BI?
Oracle XML DB - What's in it for me?
Aspects of 10 Tuning
Back to basics: Simple database web services without the need for SOA
Lost without a trace
Take a load off! Load testing your Oracle APEX or JDeveloper web applications
Application Express - A web development environment for the masses - and for ...
OHarmony - How the Optimiser works
Oracle Discoverer is dead - Where to next for BI?

Recently uploaded (20)

PPT
Introduction Database Management System for Course Database
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Essential Infomation Tech presentation.pptx
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Materi-Enum-and-Record-Data-Type (1).pptx
PDF
top salesforce developer skills in 2025.pdf
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
ai tools demonstartion for schools and inter college
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Introduction to Artificial Intelligence
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
DOCX
The Five Best AI Cover Tools in 2025.docx
PDF
Understanding Forklifts - TECH EHS Solution
Introduction Database Management System for Course Database
Internet Downloader Manager (IDM) Crack 6.42 Build 41
How to Migrate SBCGlobal Email to Yahoo Easily
L1 - Introduction to python Backend.pptx
Essential Infomation Tech presentation.pptx
Operating system designcfffgfgggggggvggggggggg
PTS Company Brochure 2025 (1).pdf.......
VVF-Customer-Presentation2025-Ver1.9.pptx
Materi-Enum-and-Record-Data-Type (1).pptx
top salesforce developer skills in 2025.pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Online Work Permit System for Fast Permit Processing
ai tools demonstartion for schools and inter college
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Introduction to Artificial Intelligence
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
The Five Best AI Cover Tools in 2025.docx
Understanding Forklifts - TECH EHS Solution

Meet the CBO in Version 11g

  • 1. www.sagecomputing.com.au penny@sagecomputing.com.au Meet the Cost Based Optimiser in 11g Penny Cookson SAGE Computing Services SAGE Computing Services Customised Oracle Training Workshops and Consulting
  • 2. 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 penny@sagecomputing.com.au
  • 3. A Characteristic Problem I haven’t changed anything Its really slow this morning I did the same thing yesterday and it was fine Actually its OK now No its not Thank you so much you’ve fixed it (I haven’t done anything)
  • 4. Oracle Version < 9 SELECT COUNT(l.quantity) FROM bookings_skew l WHERE resource_code = :v1; ‘BRLG’ <Version 9 database No bind peeking Shared Pool SELECT COUNT(l.quantity) FROM bookings_skew_large l WHERE resource_code = :v1; ‘PC1’ FULL SCAN How many rows do I expect?
  • 5. Oracle Version < 9 SELECT COUNT(l.quantity) FROM bookings_skew l WHERE resource_code = :v1; ‘BRLG’ <Version 9 database No bind peeking Shared Pool SELECT COUNT(l.quantity) FROM bookings_skew_large l WHERE resource_code = :v1; ‘PC1’ FULL SCAN How many rows do I expect?
  • 6. What is Bind Peeking? SELECT COUNT(l.quantity) FROM bookings_skew l WHERE resource_code = :v1; ‘PC1’ >=Version 9 database Bind peeking Shared Pool SELECT COUNT(l.quantity) FROM bookings_skew l WHERE resource_code = :v1; ‘BRLG’ INDEXED ACCESS How many rows do I expect?
  • 7. Histogram – Minority First *************************************** SINGLE TABLE ACCESS PATH Column (#3): RESOURCE_CODE(VARCHAR2) AvgLen: 5.00 NDV: 9 Nulls: 0 Density: 9.0892e-008 Histogram: Freq #Bkts: 9 UncompBkts: 5966 EndPtVals: 9 Table: BOOKINGS_SKEW Alias: L Card: Original: 5464800 Rounded: 43968 Computed: 43967.55 Non Adjusted: 43967.55 Access Path: TableScan Cost: 7693.35 Resp: 7693.35 Degree: 0 Cost_io: 7426.00 Cost_cpu: 1555877511 Resp_io: 7426.00 Resp_cpu: 1555877511 Access Path: index (AllEqRange) Index: BK_RESSKEW resc_io: 2399.00 resc_cpu: 37397785 ix_sel: 0.0080456 ix_sel_with_filters: 0.0080456 Cost: 2405.43 Resp: 2405.43 Degree: 1 Best:: AccessPath: IndexRange Index: BK_RESSKEW Cost: 2405.43 Degree: 1 Resp: 2405.43 Card: 43967.55 Bytes: 0 *************************************** RESO COUNT(*) ---- ---------- VCR1 495711 CONF 495720 LNCH 743576 BRSM 743583 PC1 47858 FLPC 495720 BRLG 1739277 TAP1 247864 VCR2 495715
  • 9. What is the CBO good at in 10g? Data Condition Literal/Bind Var Histogram Even Distribution Equality Literal N/A Even Distribution Equality Bind N/A Skewed Equality Literal NO Skewed Equality Literal YES Skewed Equality Bind NO Skewed Equality Bind YES Even Distribution Range Bind N/A
  • 10. Bind Peeking + Adaptive Cursors in 11g Statements with bind variables (+histograms) are bind sensitive The first time you execute a statement with different selectivity it uses the original plan The second time it changes the plan and become bind aware New values will use a plan for the appropriate selectivity range
  • 13. PLSQL has soft parse avoidance Implicit cursor Explicit cursor Native dynamic SQL Ref cursor Session_cached_cursors = 0 No default adaptive cursor functionality Default adaptive cursor functionality
  • 14. Adaptive Cursors Functionality Bind variable with Equality and Histogram Not for range conditions Bind variable with Equality and Histogram Range conditions Does not support LIKE /*+ BIND_AWARE */ Adaptive Cursors 11.1.0.6 Adaptive Cursors 11.1.0.7 and 11.2
  • 15. So where are we now? Our own code – works properly first time with hint in SQL and PL/SQL Packages – will still get it wrong once and won’t use adaptive cursors for PL/SQL at all unless we set session cached cursors =0
  • 25. Bind Peeking + Adaptive Cursors Summary Your code Packages Minimise statement invalidations Consider running the statement with minority and majority value on start up Use the BIND_AWARE hint (SQL and PL/SQL) for skewed data Use ref cursors (if hints are not allowed) ?manually hack an outline/profile PL/SQL – set session_cached_cursors = 0 (temporarily)
  • 26. Adaptive Cursors What we Really Need Once a statement has been bind aware it knows next time its parsed ?SQL *Profile to indicate bind aware Adaptive Cursors Persistence
  • 27. Try to get rid of your hints _optimizer_ignore_hints Create additional statistics Set _optimizer_ignore_hints to TRUE Test the app If it runs OK remove your hints
  • 28. 58 New Hints SELECT name, inverse, sql_feature, version FROM v$sql_hint WHERE version like '11%' ORDER BY version desc, name New Hints
  • 29. New Hints IGNORE_ROW_ON_DUPKEY_INDEX This has nothing to do with optimisation I just like it
  • 30. New Hints MONITOR NO_MONITOR select /*+ MONITOR */ count(comments) from bookings ; select DBMS_SQLTUNE.REPORT_SQL_MONITOR( session_id=>sys_context('userenv','sid'), report_level=>'ALL') as report from dual;
  • 31. Much More Query Transformation SELECT e.event_no, e.start_date, sum(cost) totcost FROM events_large e, bookings_large b WHERE e.event_no = b.event_no GROUP BY e.event_no, e.start_date alter session set tracefile_identifier = Penny alter session set events '10053 trace name context forever' explain plan for SELECT e.event_no, e.start_date, sum(cost) totcost FROM events_large e, bookings_large b WHERE e.event_no = b.event_no GROUP BY e.event_no, e.start_date; alter session set events '10053 trace name context off'
  • 32. Much More Query Transformation SELECT e.event_no, e.start_date, sum(cost) totcost FROM events_large e, bookings_large b WHERE e.event_no = b.event_no GROUP BY e.event_no, e.start_date
  • 33. 10G – JOIN before the GROUP BY
  • 35. 11G – GROUP BY before the JOIN
  • 41. 10G – NOT IN (PROMISE_NO NULLABLE)
  • 42. 10G – KILLED AFTER 1 hr
  • 43. 11G – Rewrite as Null Aware Anti join
  • 45. 10G – Full Outer Join – default behaviour
  • 47. From version 10.2.0.3 SELECT /*+ NATIVE_FULL_OUTER_JOIN */ count(e.comments) nume, count (b.comments) numb FROM events_large e FULL OUTER JOIN bookings_large b ON (e.event_no = b.event_no) 10G – Full Outer Join – hint
  • 48. 11G Native Full Outer Join
  • 50. Check the differences in plans Upgrading
  • 54. Oracle 10g – Run a whole load of random statements – all of which require parsing
  • 55. Oracle 11g – Run a whole load of random statements – all of which require parsing So Tuning the Shared Pool becomes more important, but overall the 11g CBO is pretty smart
  • 56. Results Cache SGA populate invalidate Most recently used SQL/PL/SQL SQL Query Results Cache Results Cache PL/SQL Function Results Cache Most recently used result sets Buffer Cache Library Cache Most recently data
  • 57. Query Results Cache RESULT_CACHE_MODE MANUAL FORCE /*+RESULT_CACHE */ /*+ NO_RESULT_CACHE */ SELECT /*+ RESULT_CACHE */ count(b.comments) FROM train1.events_large e, train1.bookings_large b WHERE e.org_id = :v1 AND e.event_no = b.event_no AND e.comments = :v2;
  • 58. PL/SQL Function Results Cache CREATE OR REPLACE FUNCTION quantity_booked (p_resource_code in resources.code%TYPE,p_event_date in date) RETURN NUMBER RESULT_CACHE IS v_total_booked number := 0; BEGIN SELECT sum(b.quantity) INTO v_total_booked FROM bookings b, events e WHERE e.event_no = b.event_no AND p_event_date between e.start_date and e.end_date AND b.resource_code = p_resource_code; RETURN (v_total_booked); END;
  • 59. Monitoring the Results Cache SELECT * FROM v$result_cache_memory SELECT * FROM v$result_cache_objects SELECT * FROM v$result_cache_statistics SELECT * FROM v$result_cache_dependency RESULT_CACHE_MAX_SIZE RESULT_CACHE_MAX_RESULT Explain Plan
  • 61. Setting Statistics Gathering Defaults GET_PARAM SET_PARAM Procedure GET_PREFS Function SET_TABLE_PREFS (set for individual tables) SET_DATABASE_PREFS (sets for all tables, can include/exclude SYS tables) SET_GLOBAL_PREFS – for new objects Obsolete: Use:
  • 62. Gathering Statistics Early CBO: “Make sure you gather statistics regularly” Later CBO: “Don’t gather statistics unless data patterns change” BUT If you have new majority values you need to recreate the histogram
  • 63. Gathering and Publishing Set preferences to PUBLISH = ‘FALSE’ Gather Statistics user_tab_pending_stats user_ind_pending_stats user_col_pending_stats Run test case Alter session set optimizer_pending_statistics = TRUE Worse Better dbms_stats. delete_pending_stats dbms_stats. publish_pending_stats user_tab_statistics user_ind_statistics user_tab_col_statistics
  • 64. Gathering and Publishing SELECT obj#, TO_CHAR(savtime,'dd/mm/yyyy') save_time, rowcnt, blkcnt, avgrln, samplesize, analyzetime FROM wri$_optstat_tab_history ORDER BY savtime desc
  • 65. CBO - Instability CBO ?access path decision System parameters Session parameters Optimiser StatisticsIndexes SQL Profile Software version System statistics DBA playing around
  • 66. CBO - Instability – Leave it all alone – SQL Plan Management – Store plan baseline – Plans not used till accepted – Manually accept or – Allow Oracle to evolve plans Solution 1 Solution 2
  • 67. SQL Plan Management Manual capture DBMS_SPM.LOAD_PLANS_FROM_SQLSET DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE Auto capture of repeatable statements OPTIMIZER_CAPTURE_SQL_PLAN_BASELINE = TRUE Baseline = (Stored and Accepted plans) SQL Management Base New Plan identified during execution Stored not accepted SQL Tuning Advisor identifies new plan – SQL*Profile accepted Auto accept of new plan (if it performs better) DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE Manual load/accept of new plan DBMS_SPM.LOAD_PLANS_FROM_SQLSET DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE
  • 69. SAGE Computing Services Customised Oracle Training Workshops and Consulting Questions? www.sagecomputing.com.au penny@sagecomputing.com.au