SlideShare a Scribd company logo
An Introduction to 
Cost Based Optimization 
By 
Riyaj Shamsudeen 
©OraInternals Riyaj Shamsudeen
©OraInternals Riyaj Shamsudeen 2 
Me 
 18 years using Oracle products/DBA 
 OakTable member 
 Oracle ACE 
 Certified DBA versions 7.0,7.3,8,8i,9i &10g 
 Specializes in RAC, performance tuning, 
Internals and E-business suite 
 Chief DBA with OraInternals 
 Email: rshamsud@orainternals.com 
 Blog : orainternals.wordpress.com 
 URL: www.orainternals.com
©OraInternals Riyaj Shamsudeen 3 
Disclaimer 
These slides and materials represent the work and opinions of the author and do 
not constitute official positions of my current or past employer or any other 
organization. This material has been peer reviewed, but author assume no 
responsibility whatsoever for the test cases. 
If you corrupt your databases by running my scripts, you are solely responsible 
for that. 
This material should not be reproduced or used without the authors' written 
permission.
©OraInternals Riyaj Shamsudeen 4 
Agenda 
 Selectivity 
 Cardinality & histograms 
 Index based access cost 
 Correlation issues
©OraInternals Riyaj Shamsudeen 5 
Selectivity 
Selectivity is a measure, with values ranging between 0 and 
1, indicating how many elements will be selected from a 
fixed set of elements.
Selectivity – Example 
Dept Code Emp 
10 TX Scott 
10 TX Mary 
10 TX Larry 
20 CA Juan 
20 CA Raj 
20 CA Pele 
20 CA Ronaldinho 
30 FL Ronaldo 
©OraInternals Riyaj Shamsudeen 6 
 Selectivity of predicate 
dept =:b1 
is 1/NDV=1/3=0.333 
 Selectivity of predicate 
(dept=:b1 or code=:b2) 
=~ (1/3) +( 1/3 ) – (1/3)*(1/3) 
= 0.555 
 Selectivity of predicate 
(dept=:b1 and code=:b2) 
=~ (1/3)*(1/3) 
= 0.111 
NDV : # of distinct values 
Demo: demo_01.sql demo_02.sql
©OraInternals Riyaj Shamsudeen 7 
Selectivity 
 Lower the selectivity, better the predicates are. 
 Probability concepts will be applied in calculating selectivity of 
many predicates. 
p(A and B) = p(A) * p (B) 
p(A or B) = p(A) + p(B) – p(A) * p (B)
©OraInternals Riyaj Shamsudeen 8 
Agenda - Part I 
 Selectivity 
 Cardinality & histograms 
 Index cost 
 Correlation issues
©OraInternals Riyaj Shamsudeen 9 
Cardinality 
 Cardinality is a measure, an estimate of number of rows 
expected from a row source. 
 In a very simple sense: 
Cardinality = Selectivity * Number of rows 
 Cardinality estimates are very essential and need to be accurate 
for the optimizer to choose optimal execution plan.
Cardinality – No histogram 
Dept Emp 
10 Scott 
10 Mary 
10 Larry 
20 Karen 
20 Jill 
20 Pele 
20 Ronaldinho 
30 Ronaldo 
 Cardinality of predicate 
dept =:b1 
= (Selectivity * Num_rows) 
= (1/3) * 8 = 2.6 rows 
= 3 rows 
predicate Estimate Actual Change 
dept=10 3 3 = 
dept=20 3 4 
dept=30 3 1 
 Under estimation of cardinality is a root cause 
for many SQL performance issues. 
©OraInternals Riyaj Shamsudeen 10
©OraInternals Riyaj Shamsudeen 11 
List 
 Calculation of selectivity for an in list is sum of selectivity of 
individual elements in the list. 
explain plan for select * from tlist where n3 in (10, 20, 30,40); 
--------------------------------------------------------------------------- 
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 
--------------------------------------------------------------------------- 
| 0 | SELECT STATEMENT | | 400 | 5200 | 104 (2)| 00:00:02 | 
|* 1 | TABLE ACCESS FULL| TLIST | 400 | 5200 | 104 (2)| 00:00:02 | 
--------------------------------------------------------------------------- 
Predicate Information (identified by operation id): 
--------------------------------------------------- 
1 - filter("N3"=10 OR "N3"=20 OR "N3"=30 OR "N3"=40) 
 Individual selectivity of n3= predicate is 1/1000. 
 Cardinality = 100,000 X 4 X (1/1000) = 400 
 Duplicates eliminated for literal variables 
Demo: demo_03.sql demo_03a.sql demo_03b.sql
 Total range : Max - Min = 999 -0 
Selected range: 200-100 
Cardinality = 100,000 * (2*(1/1000) + (200-100)/(999-0) ) 
©OraInternals Riyaj Shamsudeen 12 
Range 
 Selectivity of range predicate is calculated as 
= selected_range/total range + selectivity of range end pts 
explain plan for select * from tlist where n3 between 100 and 200; 
--------------------------------------------------------------------------- 
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 
--------------------------------------------------------------------------- 
| 0 | SELECT STATEMENT | | 10210 | 129K| 103 (1)| 00:00:02 | 
|* 1 | TABLE ACCESS FULL| TLIST | 10210 | 129K| 103 (1)| 00:00:02 | 
--------------------------------------------------------------------------- 
Predicate Information (identified by operation id): 
--------------------------------------------------- 
1 - filter("N3"<=200 AND "N3">=100) 
Demo: demo_04.sql
Range with bind 
 If the bind variables are used then the estimate is approximated 
to 0.25% 
explain plan for select * from tlist where n3 between :b1 and :b2; 
---------------------------------------------------------------------------- 
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 
---------------------------------------------------------------------------- 
| 0 | SELECT STATEMENT | | 250 | 3250 | 104 (2)| 00:00:02 | 
|* 1 | FILTER | | | | | | 
|* 2 | TABLE ACCESS FULL| TLIST | 250 | 3250 | 104 (2)| 00:00:02 | 
---------------------------------------------------------------------------- 
Predicate Information (identified by operation id): 
--------------------------------------------------- 
1 - filter(TO_NUMBER(:B1)<=TO_NUMBER(:B2)) 
2 - filter("N3">=TO_NUMBER(:B1) AND "N3"<=TO_NUMBER(:B2)) 
©OraInternals Riyaj Shamsudeen 13 
Demo: demo_05.sql
Cardinality & Skew 
drop table t1; 
create table t1 (color_id number, color varchar2(10) ); 
90 
©OraInternals Riyaj Shamsudeen 14 
insert into t1 
select l1, 
case 
when l1 <10 then 'blue' 
when l1 between 10 and 99 then 'red' 
when l1 between 100 and 999 then ‘black' 
end case 
from 
(select level l1 from dual connect by level <1000) 
/ 
commit; 
9 
900 
Begin 
dbms_stats.gather_table_stats ('cbo2','t1', 
estimate_percent =>100, method_opt =>' for all columns size 1'); 
End; 
/
Cardinality : w/o histograms 
select color, count(*) from t1 group by color order by 2 
COLOR COUNT(*) 
---------- ---------- 
blue 9 
red 90 
white 900 
select count(*) from t1 where color='blue'; 
SQL> select * from table (dbms_xplan.display_cursor); 
------------------------------------------------------------------------ 
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 
------------------------------------------------------------------------ 
| 0 | SELECT STATEMENT | | | | 3 (100)| 
| 1 | SORT AGGREGATE | | 1 | 6 | | 
|* 2 | TABLE ACCESS FULL| T1 | 333 | 1998 | 3 (0)| 00:01 | 
------------------------------------------------------------------------ 
Predicate Information (identified by operation id): 
--------------------------------------------------- 
2 - filter("COLOR"='blue') 
©OraInternals Riyaj Shamsudeen 15
If the column values have skew and if there are predicates 
accessing that column, then use histograms. But, use them 
sparingly! 
©OraInternals Riyaj Shamsudeen 16 
Histograms
Cardinality : with histograms 
©OraInternals Riyaj Shamsudeen 17 
Begin 
dbms_stats.gather_table_stats ('cbo2','t1', estimate_percent =>100, 
method_opt =>' for all columns size 10'); 
end; 
/ 
PL/SQL procedure successfully completed. 
Select count(*) from t1 where color=‘blue’; 
SQL> select * from table (dbms_xplan.display); 
------------------------------------------------------------------------ 
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time 
| 
------------------------------------------------------------------------ 
| 0 | SELECT STATEMENT | | 1 | 6 | 3 (0)|00:00:01 
| 1 | SORT AGGREGATE | | 1 | 6 | | 
|* 2 | TABLE ACCESS FULL| T1 | 9 | 54 | 3 (0)|00:00:01 
------------------------------------------------------------------------ 
Predicate Information (identified by operation id): 
--------------------------------------------------- 
2 - filter("COLOR"='blue') 
14 rows selected.
 If there are few distinct values then use that many # of buckets. 
Else use size auto. 
 In many cases, 100% statistics might be needed. 
©OraInternals Riyaj Shamsudeen 18 
Histograms … 
 Collecting histograms in all columns is not recommended and 
has a side effect of increasing CPU usage. Also, statistics 
collection can run longer.
©OraInternals Riyaj Shamsudeen 19 
Agenda - Part I 
 Selectivity 
 Cardinality & histograms 
 Index base access 
 Correlation issues
Correlation explained 
Optimizer assumes no correlation between predicates. 
In this example, 
All triangles are blue. 
All circles are red. 
All squares are black. 
Predicates shape=‘CIRCLE’ and color =‘RED’ 
are correlated. 
But optimizer assumes no-correlation. 
©OraInternals Riyaj Shamsudeen 20
©OraInternals Riyaj Shamsudeen 21 
Correlation … 
drop table t1; 
create table t1 
(color_id number, color varchar2(10), shape varchar2(10) ); 
insert into t1 
select l1, 
case 
when l1 <10 then 'blue' 
when l1 between 10 and 99 then 'red' 
when l1 between 100 and 999 then ‘black' 
end case, 
case 
when l1 <10 then 'triangle' 
when l1 between 10 and 99 then ‘circle' 
when l1 between 100 and 999 then 'rectangle' 
end case 
from 
(select level l1 from dual connect by level <1000) 
/ 
commit; 
exec dbms_stats.gather_table_stats ('cbo2','t1', estimate_percent =>100, 
method_opt =>' for all columns size 1');
Correlation & cardinality 
1* select color, shape, count(*) from t1 group by color,shape 
SQL> / 
COLOR SHAPE COUNT(*) 
---------- ---------- ---------- 
blue triangle 9 
red circle 90 
black rectangle 900 
explain plan for select count(*) from t1 
where color='blue' and shape='triangle'; 
Cardinality estimates 
select * from table(dbms_xplan.display); 
------------------------------------------------------------------------ 
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time 
©OraInternals Riyaj Shamsudeen 22 
| 
------------------------------------------------------------------------ 
| 0 | SELECT STATEMENT | | 1 | 16 | 3 (0)| 0:00:01 
| 1 | SORT AGGREGATE | | 1 | 16 | | 
|* 2 | TABLE ACCESS FULL| T1 | 111 | 1776 | 3 (0)| 0:00:01 
----------------------------------------------------------------------- 
Predicate Information (identified by operation id): 
--------------------------------------------------- 
2 - filter("COLOR"='blue' AND "SHAPE"='triangle') 
are way off!
(No)Correlation ..why? 
 Selectivity of first single column predicate 
color = ‘blue’ is 1/3. 
 Selectivity of next single column predicate 
shape=‘triangle’ is 1/3. 
 Combined selectivity of both predicates are 
Optimizer assumes no 
Correlation between 
Predicates. 
sel(p1) * sel(p2) =(1/3)*(1/3)=1/9 [ Probablity theory ] 
 Cardinality estimates, then, becomes 
999 * (1/9) = 111 
©OraInternals Riyaj Shamsudeen 23
Correlation w/ Histograms.. 
alter session set optimizer_dynamic_sampling=0; 
exec dbms_stats.gather_table_stats ('cbo2','t1', estimate_percent =>100, 
method_opt =>' for all columns size 5'); 
explain plan for select count(*) from t1 
where color='blue' and shape='triangle'; 
With histograms, row 
Estimates are farther away 
from reality 
select * from table(dbms_xplan.display); 
------------------------------------------------------------------------ 
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time 
------------------------------------------------------------------------ 
| 0 | SELECT STATEMENT | | 1 | 16 | 3 (0)| 0:00:01 
| 1 | SORT AGGREGATE | | 1 | 16 | | 
|* 2 | TABLE ACCESS FULL| T1 | 1 | 16 | 3 (0)|00:00:01 
------------------------------------------------------------------------ 
Predicate Information (identified by operation id): 
--------------------------------------------------- 
2 - filter("SHAPE"='triangle' AND "COLOR"='blue') 
©OraInternals Riyaj Shamsudeen 24
So what do we do? 
 Until version 11g, this is a real problem. There is no easy way to 
fix this. Column statistics might need to be manually adjusted. 
 In version 10g, optimizer_dynamic_sampling at level 4 can be 
used to mitigate this. 
 Version 11g provides extended statistics to resolve this 
correlation issue. Refer my blog entry 
http://orainternals.wordpress.com/2008/03/21/correlation-between-column-predicates/ 
for more information on this topic. 
©OraInternals Riyaj Shamsudeen 25
Extended statistics 
 Dbms_stats package provides a function to create extended 
statististics. 
 Following code creates an extended statistics on (color,shape) 
capturing correlation between the columns color and shape. 
SELECT dbms_stats.create_extended_stats( ownname=>user, 
tabname => 'T1',extension => '(color, shape )' ) AS c1_c2_correlation 
FROM dual; 
C1_C2_CORRELATION 
--------------------------------- 
SYS_STUAOJW6_2K$IUXLR#$DK235BV 
©OraInternals Riyaj Shamsudeen 26
 Creating extended statistics adds a hidden, virtual column to that 
table. 
 Following code collects statistics on virtual column with 
histogram. 
©OraInternals Riyaj Shamsudeen 27 
Virtual column 
select owner, table_name, column_name, hidden_column, virtual_column 
from dba_tab_cols where table_name='T1' and owner='CBO2' 
order by column_id; 
OWNER TABLE COLUMN_NAME HID VIR 
----- ----- ------------------------------ --- --- 
... 
CBO2 T1 SHAPE NO NO 
CBO2 T1 SYS_STUAOJW6_2K$IUXLR#$DK235BV YES YES 
begin 
dbms_stats.gather_Table_stats( user, 'T1', 
estimate_percent => 100, 
method_opt => 'for all columns size 254'); 
end; 
/
Better estimates …2 
 Cardinality estimates are much better. 
explain plan for select count(*) from t1 where 
color='blue' and shape='triangle'; 
select * from table(dbms_xplan.display); 
--------------------------------------------------------------------------- 
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 
--------------------------------------------------------------------------- 
| 0 | SELECT STATEMENT | | 1 | 16 | 4 (0)| 00:00:01 | 
| 1 | SORT AGGREGATE | | 1 | 16 | | | 
|* 2 | TABLE ACCESS FULL| T1 | 9 | 144 | 4 (0)| 00:00:01 | 
--------------------------------------------------------------------------- 
Predicate Information (identified by operation id): 
--------------------------------------------------- 
2 - filter("SHAPE"='triangle' AND "COLOR"='blue') 
©OraInternals Riyaj Shamsudeen 28
Better estimates 
 For other combinations also estimate is very close. 
SQL> explain plan for select count(*) from t1 where color='black' and 
©OraInternals Riyaj Shamsudeen 29 
shape='rectangle'; 
Explained. 
SQL> select * from table(dbms_xplan.display); 
--------------------------------------------------------------------------- 
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 
--------------------------------------------------------------------------- 
| 0 | SELECT STATEMENT | | 1 | 16 | 4 (0)| 00:00:01 | 
| 1 | SORT AGGREGATE | | 1 | 16 | | | 
|* 2 | TABLE ACCESS FULL| T1 | 900 | 14400 | 4 (0)| 00:00:01 | 
--------------------------------------------------------------------------- 
Predicate Information (identified by operation id): 
--------------------------------------------------- 
2 - filter("COLOR"='black' AND "SHAPE"='rectangle')
Non-existing values 
 For non-existing combinations also, estimates are much better. 
SQL> explain plan for select count(*) from t1 where color='blue' and 
©OraInternals Riyaj Shamsudeen 30 
shape='rectangle'; 
Explained. 
SQL> select * from table(dbms_xplan.display); 
--------------------------------------------------------------------------- 
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | 
--------------------------------------------------------------------------- 
| 0 | SELECT STATEMENT | | 1 | 16 | 4 (0)| 00:00:01 | 
| 1 | SORT AGGREGATE | | 1 | 16 | | | 
|* 2 | TABLE ACCESS FULL| T1 | 5 | 80 | 4 (0)| 00:00:01 | 
--------------------------------------------------------------------------- 
Predicate Information (identified by operation id): 
--------------------------------------------------- 
2 - filter("COLOR"='blue' AND "SHAPE"='rectangle')
©OraInternals Riyaj Shamsudeen 31 
References 
1. Oracle support site. Metalink.oracle.com. Various documents 
2. Internal’s guru Steve Adam’s website 
www.ixora.com.au 
3. Jonathan Lewis’ website 
www.jlcomp.daemon.co.uk 
4. Julian Dyke’s website 
www.julian-dyke.com 
5. ‘Oracle8i Internal Services for Waits, Latches, Locks, and Memory’ 
by Steve Adams 
6. Randolf Geist : http://oracle-randolf.blogspot.com 
7. Tom Kyte’s website 
Asktom.oracle.com

More Related Content

What's hot

A deep dive about VIP,HAIP, and SCAN
A deep dive about VIP,HAIP, and SCAN A deep dive about VIP,HAIP, and SCAN
A deep dive about VIP,HAIP, and SCAN
Riyaj Shamsudeen
 
Rac introduction
Rac introductionRac introduction
Rac introduction
Riyaj Shamsudeen
 
Advanced RAC troubleshooting: Network
Advanced RAC troubleshooting: NetworkAdvanced RAC troubleshooting: Network
Advanced RAC troubleshooting: Network
Riyaj Shamsudeen
 
Riyaj real world performance issues rac focus
Riyaj real world performance issues rac focusRiyaj real world performance issues rac focus
Riyaj real world performance issues rac focus
Riyaj Shamsudeen
 
Introduction to Parallel Execution
Introduction to Parallel ExecutionIntroduction to Parallel Execution
Introduction to Parallel Execution
Doug Burns
 
Chasing the optimizer
Chasing the optimizerChasing the optimizer
Chasing the optimizer
Mauro Pagano
 
Redo internals ppt
Redo internals pptRedo internals ppt
Redo internals ppt
Riyaj Shamsudeen
 
Histograms in 12c era
Histograms in 12c eraHistograms in 12c era
Histograms in 12c era
Mauro Pagano
 
Exadata - Smart Scan Testing
Exadata - Smart Scan TestingExadata - Smart Scan Testing
Exadata - Smart Scan TestingMonowar Mukul
 
Same plan different performance
Same plan different performanceSame plan different performance
Same plan different performance
Mauro Pagano
 
Tracing Parallel Execution (UKOUG 2006)
Tracing Parallel Execution (UKOUG 2006)Tracing Parallel Execution (UKOUG 2006)
Tracing Parallel Execution (UKOUG 2006)
Doug Burns
 
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321maclean liu
 
你所不知道的Oracle后台进程Smon功能
你所不知道的Oracle后台进程Smon功能你所不知道的Oracle后台进程Smon功能
你所不知道的Oracle后台进程Smon功能
maclean liu
 
Is your SQL Exadata-aware?
Is your SQL Exadata-aware?Is your SQL Exadata-aware?
Is your SQL Exadata-aware?
Mauro Pagano
 
Sangam 19 - PLSQL still the coolest
Sangam 19 - PLSQL still the coolestSangam 19 - PLSQL still the coolest
Sangam 19 - PLSQL still the coolest
Connor McDonald
 
了解Oracle rac brain split resolution
了解Oracle rac brain split resolution了解Oracle rac brain split resolution
了解Oracle rac brain split resolutionmaclean liu
 
Vertica trace
Vertica traceVertica trace
Vertica trace
Zvika Gutkin
 
oracle cloud with 2 nodes processing
oracle cloud with 2 nodes processingoracle cloud with 2 nodes processing
oracle cloud with 2 nodes processingmahdi ahmadi
 
Verification of Concurrent and Distributed Systems
Verification of Concurrent and Distributed SystemsVerification of Concurrent and Distributed Systems
Verification of Concurrent and Distributed Systems
Mykola Novik
 
Using SQL to process hierarchies
Using SQL to process hierarchiesUsing SQL to process hierarchies
Using SQL to process hierarchies
Connor McDonald
 

What's hot (20)

A deep dive about VIP,HAIP, and SCAN
A deep dive about VIP,HAIP, and SCAN A deep dive about VIP,HAIP, and SCAN
A deep dive about VIP,HAIP, and SCAN
 
Rac introduction
Rac introductionRac introduction
Rac introduction
 
Advanced RAC troubleshooting: Network
Advanced RAC troubleshooting: NetworkAdvanced RAC troubleshooting: Network
Advanced RAC troubleshooting: Network
 
Riyaj real world performance issues rac focus
Riyaj real world performance issues rac focusRiyaj real world performance issues rac focus
Riyaj real world performance issues rac focus
 
Introduction to Parallel Execution
Introduction to Parallel ExecutionIntroduction to Parallel Execution
Introduction to Parallel Execution
 
Chasing the optimizer
Chasing the optimizerChasing the optimizer
Chasing the optimizer
 
Redo internals ppt
Redo internals pptRedo internals ppt
Redo internals ppt
 
Histograms in 12c era
Histograms in 12c eraHistograms in 12c era
Histograms in 12c era
 
Exadata - Smart Scan Testing
Exadata - Smart Scan TestingExadata - Smart Scan Testing
Exadata - Smart Scan Testing
 
Same plan different performance
Same plan different performanceSame plan different performance
Same plan different performance
 
Tracing Parallel Execution (UKOUG 2006)
Tracing Parallel Execution (UKOUG 2006)Tracing Parallel Execution (UKOUG 2006)
Tracing Parallel Execution (UKOUG 2006)
 
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
 
你所不知道的Oracle后台进程Smon功能
你所不知道的Oracle后台进程Smon功能你所不知道的Oracle后台进程Smon功能
你所不知道的Oracle后台进程Smon功能
 
Is your SQL Exadata-aware?
Is your SQL Exadata-aware?Is your SQL Exadata-aware?
Is your SQL Exadata-aware?
 
Sangam 19 - PLSQL still the coolest
Sangam 19 - PLSQL still the coolestSangam 19 - PLSQL still the coolest
Sangam 19 - PLSQL still the coolest
 
了解Oracle rac brain split resolution
了解Oracle rac brain split resolution了解Oracle rac brain split resolution
了解Oracle rac brain split resolution
 
Vertica trace
Vertica traceVertica trace
Vertica trace
 
oracle cloud with 2 nodes processing
oracle cloud with 2 nodes processingoracle cloud with 2 nodes processing
oracle cloud with 2 nodes processing
 
Verification of Concurrent and Distributed Systems
Verification of Concurrent and Distributed SystemsVerification of Concurrent and Distributed Systems
Verification of Concurrent and Distributed Systems
 
Using SQL to process hierarchies
Using SQL to process hierarchiesUsing SQL to process hierarchies
Using SQL to process hierarchies
 

Similar to Demystifying cost based optimization

A few things about the Oracle optimizer - 2013
A few things about the Oracle optimizer - 2013A few things about the Oracle optimizer - 2013
A few things about the Oracle optimizer - 2013
Connor McDonald
 
Sydney Oracle Meetup - access paths
Sydney Oracle Meetup - access pathsSydney Oracle Meetup - access paths
Sydney Oracle Meetup - access pathspaulguerin
 
Oracle dbms_xplan.display_cursor format
Oracle dbms_xplan.display_cursor formatOracle dbms_xplan.display_cursor format
Oracle dbms_xplan.display_cursor format
Franck Pachot
 
Adaptive Query Optimization
Adaptive Query OptimizationAdaptive Query Optimization
Adaptive Query Optimization
Anju Garg
 
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ OracleUnderstanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Guatemala User Group
 
Metadata Matters
Metadata MattersMetadata Matters
Metadata Mattersafa reg
 
Managing Statistics for Optimal Query Performance
Managing Statistics for Optimal Query PerformanceManaging Statistics for Optimal Query Performance
Managing Statistics for Optimal Query Performance
Karen Morton
 
Adapting to Adaptive Plans on 12c
Adapting to Adaptive Plans on 12cAdapting to Adaptive Plans on 12c
Adapting to Adaptive Plans on 12c
Mauro Pagano
 
SQLチューニング総合診療Oracle CloudWorld出張所
SQLチューニング総合診療Oracle CloudWorld出張所SQLチューニング総合診療Oracle CloudWorld出張所
SQLチューニング総合診療Oracle CloudWorld出張所
Hiroshi Sekiguchi
 
Writing efficient sql
Writing efficient sqlWriting efficient sql
Writing efficient sqlj9soto
 
5 Cool Things About SQL
5 Cool Things About SQL5 Cool Things About SQL
5 Cool Things About SQL
Connor McDonald
 
More than 12 More things about Oracle Database 12c
More than 12 More things about Oracle Database 12cMore than 12 More things about Oracle Database 12c
More than 12 More things about Oracle Database 12c
Guatemala User Group
 
Randolf Geist – IT-Tage 2015 – Oracle Parallel Execution – Analyse und Troubl...
Randolf Geist – IT-Tage 2015 – Oracle Parallel Execution – Analyse und Troubl...Randolf Geist – IT-Tage 2015 – Oracle Parallel Execution – Analyse und Troubl...
Randolf Geist – IT-Tage 2015 – Oracle Parallel Execution – Analyse und Troubl...
Informatik Aktuell
 
Oracle 12c SPM
Oracle 12c SPMOracle 12c SPM
Oracle 12c SPM
Anton Bushmelev
 
Understanding Optimizer-Statistics-for-Developers
Understanding Optimizer-Statistics-for-DevelopersUnderstanding Optimizer-Statistics-for-Developers
Understanding Optimizer-Statistics-for-DevelopersEnkitec
 
Do You Know The 11g Plan?
Do You Know The 11g Plan?Do You Know The 11g Plan?
Do You Know The 11g Plan?
Mahesh Vallampati
 
Histograms : Pre-12c and Now
Histograms : Pre-12c and NowHistograms : Pre-12c and Now
Histograms : Pre-12c and Now
Anju Garg
 
Five more things about Oracle SQL and PLSQL
Five more things about Oracle SQL and PLSQLFive more things about Oracle SQL and PLSQL
Five more things about Oracle SQL and PLSQL
Connor McDonald
 
All on Adaptive and Extended Cursor Sharing
All on Adaptive and Extended Cursor SharingAll on Adaptive and Extended Cursor Sharing
All on Adaptive and Extended Cursor Sharing
Mohamed Houri
 

Similar to Demystifying cost based optimization (20)

A few things about the Oracle optimizer - 2013
A few things about the Oracle optimizer - 2013A few things about the Oracle optimizer - 2013
A few things about the Oracle optimizer - 2013
 
Sydney Oracle Meetup - access paths
Sydney Oracle Meetup - access pathsSydney Oracle Meetup - access paths
Sydney Oracle Meetup - access paths
 
Oracle dbms_xplan.display_cursor format
Oracle dbms_xplan.display_cursor formatOracle dbms_xplan.display_cursor format
Oracle dbms_xplan.display_cursor format
 
Adaptive Query Optimization
Adaptive Query OptimizationAdaptive Query Optimization
Adaptive Query Optimization
 
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ OracleUnderstanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
 
Metadata Matters
Metadata MattersMetadata Matters
Metadata Matters
 
Managing Statistics for Optimal Query Performance
Managing Statistics for Optimal Query PerformanceManaging Statistics for Optimal Query Performance
Managing Statistics for Optimal Query Performance
 
Adapting to Adaptive Plans on 12c
Adapting to Adaptive Plans on 12cAdapting to Adaptive Plans on 12c
Adapting to Adaptive Plans on 12c
 
SQLチューニング総合診療Oracle CloudWorld出張所
SQLチューニング総合診療Oracle CloudWorld出張所SQLチューニング総合診療Oracle CloudWorld出張所
SQLチューニング総合診療Oracle CloudWorld出張所
 
Writing efficient sql
Writing efficient sqlWriting efficient sql
Writing efficient sql
 
Cluto presentation
Cluto presentationCluto presentation
Cluto presentation
 
5 Cool Things About SQL
5 Cool Things About SQL5 Cool Things About SQL
5 Cool Things About SQL
 
More than 12 More things about Oracle Database 12c
More than 12 More things about Oracle Database 12cMore than 12 More things about Oracle Database 12c
More than 12 More things about Oracle Database 12c
 
Randolf Geist – IT-Tage 2015 – Oracle Parallel Execution – Analyse und Troubl...
Randolf Geist – IT-Tage 2015 – Oracle Parallel Execution – Analyse und Troubl...Randolf Geist – IT-Tage 2015 – Oracle Parallel Execution – Analyse und Troubl...
Randolf Geist – IT-Tage 2015 – Oracle Parallel Execution – Analyse und Troubl...
 
Oracle 12c SPM
Oracle 12c SPMOracle 12c SPM
Oracle 12c SPM
 
Understanding Optimizer-Statistics-for-Developers
Understanding Optimizer-Statistics-for-DevelopersUnderstanding Optimizer-Statistics-for-Developers
Understanding Optimizer-Statistics-for-Developers
 
Do You Know The 11g Plan?
Do You Know The 11g Plan?Do You Know The 11g Plan?
Do You Know The 11g Plan?
 
Histograms : Pre-12c and Now
Histograms : Pre-12c and NowHistograms : Pre-12c and Now
Histograms : Pre-12c and Now
 
Five more things about Oracle SQL and PLSQL
Five more things about Oracle SQL and PLSQLFive more things about Oracle SQL and PLSQL
Five more things about Oracle SQL and PLSQL
 
All on Adaptive and Extended Cursor Sharing
All on Adaptive and Extended Cursor SharingAll on Adaptive and Extended Cursor Sharing
All on Adaptive and Extended Cursor Sharing
 

Recently uploaded

UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
Vlad Stirbu
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
UiPathCommunity
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.
ViralQR
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 

Recently uploaded (20)

UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 

Demystifying cost based optimization

  • 1. An Introduction to Cost Based Optimization By Riyaj Shamsudeen ©OraInternals Riyaj Shamsudeen
  • 2. ©OraInternals Riyaj Shamsudeen 2 Me  18 years using Oracle products/DBA  OakTable member  Oracle ACE  Certified DBA versions 7.0,7.3,8,8i,9i &10g  Specializes in RAC, performance tuning, Internals and E-business suite  Chief DBA with OraInternals  Email: rshamsud@orainternals.com  Blog : orainternals.wordpress.com  URL: www.orainternals.com
  • 3. ©OraInternals Riyaj Shamsudeen 3 Disclaimer These slides and materials represent the work and opinions of the author and do not constitute official positions of my current or past employer or any other organization. This material has been peer reviewed, but author assume no responsibility whatsoever for the test cases. If you corrupt your databases by running my scripts, you are solely responsible for that. This material should not be reproduced or used without the authors' written permission.
  • 4. ©OraInternals Riyaj Shamsudeen 4 Agenda  Selectivity  Cardinality & histograms  Index based access cost  Correlation issues
  • 5. ©OraInternals Riyaj Shamsudeen 5 Selectivity Selectivity is a measure, with values ranging between 0 and 1, indicating how many elements will be selected from a fixed set of elements.
  • 6. Selectivity – Example Dept Code Emp 10 TX Scott 10 TX Mary 10 TX Larry 20 CA Juan 20 CA Raj 20 CA Pele 20 CA Ronaldinho 30 FL Ronaldo ©OraInternals Riyaj Shamsudeen 6  Selectivity of predicate dept =:b1 is 1/NDV=1/3=0.333  Selectivity of predicate (dept=:b1 or code=:b2) =~ (1/3) +( 1/3 ) – (1/3)*(1/3) = 0.555  Selectivity of predicate (dept=:b1 and code=:b2) =~ (1/3)*(1/3) = 0.111 NDV : # of distinct values Demo: demo_01.sql demo_02.sql
  • 7. ©OraInternals Riyaj Shamsudeen 7 Selectivity  Lower the selectivity, better the predicates are.  Probability concepts will be applied in calculating selectivity of many predicates. p(A and B) = p(A) * p (B) p(A or B) = p(A) + p(B) – p(A) * p (B)
  • 8. ©OraInternals Riyaj Shamsudeen 8 Agenda - Part I  Selectivity  Cardinality & histograms  Index cost  Correlation issues
  • 9. ©OraInternals Riyaj Shamsudeen 9 Cardinality  Cardinality is a measure, an estimate of number of rows expected from a row source.  In a very simple sense: Cardinality = Selectivity * Number of rows  Cardinality estimates are very essential and need to be accurate for the optimizer to choose optimal execution plan.
  • 10. Cardinality – No histogram Dept Emp 10 Scott 10 Mary 10 Larry 20 Karen 20 Jill 20 Pele 20 Ronaldinho 30 Ronaldo  Cardinality of predicate dept =:b1 = (Selectivity * Num_rows) = (1/3) * 8 = 2.6 rows = 3 rows predicate Estimate Actual Change dept=10 3 3 = dept=20 3 4 dept=30 3 1  Under estimation of cardinality is a root cause for many SQL performance issues. ©OraInternals Riyaj Shamsudeen 10
  • 11. ©OraInternals Riyaj Shamsudeen 11 List  Calculation of selectivity for an in list is sum of selectivity of individual elements in the list. explain plan for select * from tlist where n3 in (10, 20, 30,40); --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 400 | 5200 | 104 (2)| 00:00:02 | |* 1 | TABLE ACCESS FULL| TLIST | 400 | 5200 | 104 (2)| 00:00:02 | --------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("N3"=10 OR "N3"=20 OR "N3"=30 OR "N3"=40)  Individual selectivity of n3= predicate is 1/1000.  Cardinality = 100,000 X 4 X (1/1000) = 400  Duplicates eliminated for literal variables Demo: demo_03.sql demo_03a.sql demo_03b.sql
  • 12.  Total range : Max - Min = 999 -0 Selected range: 200-100 Cardinality = 100,000 * (2*(1/1000) + (200-100)/(999-0) ) ©OraInternals Riyaj Shamsudeen 12 Range  Selectivity of range predicate is calculated as = selected_range/total range + selectivity of range end pts explain plan for select * from tlist where n3 between 100 and 200; --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 10210 | 129K| 103 (1)| 00:00:02 | |* 1 | TABLE ACCESS FULL| TLIST | 10210 | 129K| 103 (1)| 00:00:02 | --------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("N3"<=200 AND "N3">=100) Demo: demo_04.sql
  • 13. Range with bind  If the bind variables are used then the estimate is approximated to 0.25% explain plan for select * from tlist where n3 between :b1 and :b2; ---------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 250 | 3250 | 104 (2)| 00:00:02 | |* 1 | FILTER | | | | | | |* 2 | TABLE ACCESS FULL| TLIST | 250 | 3250 | 104 (2)| 00:00:02 | ---------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter(TO_NUMBER(:B1)<=TO_NUMBER(:B2)) 2 - filter("N3">=TO_NUMBER(:B1) AND "N3"<=TO_NUMBER(:B2)) ©OraInternals Riyaj Shamsudeen 13 Demo: demo_05.sql
  • 14. Cardinality & Skew drop table t1; create table t1 (color_id number, color varchar2(10) ); 90 ©OraInternals Riyaj Shamsudeen 14 insert into t1 select l1, case when l1 <10 then 'blue' when l1 between 10 and 99 then 'red' when l1 between 100 and 999 then ‘black' end case from (select level l1 from dual connect by level <1000) / commit; 9 900 Begin dbms_stats.gather_table_stats ('cbo2','t1', estimate_percent =>100, method_opt =>' for all columns size 1'); End; /
  • 15. Cardinality : w/o histograms select color, count(*) from t1 group by color order by 2 COLOR COUNT(*) ---------- ---------- blue 9 red 90 white 900 select count(*) from t1 where color='blue'; SQL> select * from table (dbms_xplan.display_cursor); ------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | | | 3 (100)| | 1 | SORT AGGREGATE | | 1 | 6 | | |* 2 | TABLE ACCESS FULL| T1 | 333 | 1998 | 3 (0)| 00:01 | ------------------------------------------------------------------------ Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("COLOR"='blue') ©OraInternals Riyaj Shamsudeen 15
  • 16. If the column values have skew and if there are predicates accessing that column, then use histograms. But, use them sparingly! ©OraInternals Riyaj Shamsudeen 16 Histograms
  • 17. Cardinality : with histograms ©OraInternals Riyaj Shamsudeen 17 Begin dbms_stats.gather_table_stats ('cbo2','t1', estimate_percent =>100, method_opt =>' for all columns size 10'); end; / PL/SQL procedure successfully completed. Select count(*) from t1 where color=‘blue’; SQL> select * from table (dbms_xplan.display); ------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 1 | 6 | 3 (0)|00:00:01 | 1 | SORT AGGREGATE | | 1 | 6 | | |* 2 | TABLE ACCESS FULL| T1 | 9 | 54 | 3 (0)|00:00:01 ------------------------------------------------------------------------ Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("COLOR"='blue') 14 rows selected.
  • 18.  If there are few distinct values then use that many # of buckets. Else use size auto.  In many cases, 100% statistics might be needed. ©OraInternals Riyaj Shamsudeen 18 Histograms …  Collecting histograms in all columns is not recommended and has a side effect of increasing CPU usage. Also, statistics collection can run longer.
  • 19. ©OraInternals Riyaj Shamsudeen 19 Agenda - Part I  Selectivity  Cardinality & histograms  Index base access  Correlation issues
  • 20. Correlation explained Optimizer assumes no correlation between predicates. In this example, All triangles are blue. All circles are red. All squares are black. Predicates shape=‘CIRCLE’ and color =‘RED’ are correlated. But optimizer assumes no-correlation. ©OraInternals Riyaj Shamsudeen 20
  • 21. ©OraInternals Riyaj Shamsudeen 21 Correlation … drop table t1; create table t1 (color_id number, color varchar2(10), shape varchar2(10) ); insert into t1 select l1, case when l1 <10 then 'blue' when l1 between 10 and 99 then 'red' when l1 between 100 and 999 then ‘black' end case, case when l1 <10 then 'triangle' when l1 between 10 and 99 then ‘circle' when l1 between 100 and 999 then 'rectangle' end case from (select level l1 from dual connect by level <1000) / commit; exec dbms_stats.gather_table_stats ('cbo2','t1', estimate_percent =>100, method_opt =>' for all columns size 1');
  • 22. Correlation & cardinality 1* select color, shape, count(*) from t1 group by color,shape SQL> / COLOR SHAPE COUNT(*) ---------- ---------- ---------- blue triangle 9 red circle 90 black rectangle 900 explain plan for select count(*) from t1 where color='blue' and shape='triangle'; Cardinality estimates select * from table(dbms_xplan.display); ------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time ©OraInternals Riyaj Shamsudeen 22 | ------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 1 | 16 | 3 (0)| 0:00:01 | 1 | SORT AGGREGATE | | 1 | 16 | | |* 2 | TABLE ACCESS FULL| T1 | 111 | 1776 | 3 (0)| 0:00:01 ----------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("COLOR"='blue' AND "SHAPE"='triangle') are way off!
  • 23. (No)Correlation ..why?  Selectivity of first single column predicate color = ‘blue’ is 1/3.  Selectivity of next single column predicate shape=‘triangle’ is 1/3.  Combined selectivity of both predicates are Optimizer assumes no Correlation between Predicates. sel(p1) * sel(p2) =(1/3)*(1/3)=1/9 [ Probablity theory ]  Cardinality estimates, then, becomes 999 * (1/9) = 111 ©OraInternals Riyaj Shamsudeen 23
  • 24. Correlation w/ Histograms.. alter session set optimizer_dynamic_sampling=0; exec dbms_stats.gather_table_stats ('cbo2','t1', estimate_percent =>100, method_opt =>' for all columns size 5'); explain plan for select count(*) from t1 where color='blue' and shape='triangle'; With histograms, row Estimates are farther away from reality select * from table(dbms_xplan.display); ------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time ------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 1 | 16 | 3 (0)| 0:00:01 | 1 | SORT AGGREGATE | | 1 | 16 | | |* 2 | TABLE ACCESS FULL| T1 | 1 | 16 | 3 (0)|00:00:01 ------------------------------------------------------------------------ Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("SHAPE"='triangle' AND "COLOR"='blue') ©OraInternals Riyaj Shamsudeen 24
  • 25. So what do we do?  Until version 11g, this is a real problem. There is no easy way to fix this. Column statistics might need to be manually adjusted.  In version 10g, optimizer_dynamic_sampling at level 4 can be used to mitigate this.  Version 11g provides extended statistics to resolve this correlation issue. Refer my blog entry http://orainternals.wordpress.com/2008/03/21/correlation-between-column-predicates/ for more information on this topic. ©OraInternals Riyaj Shamsudeen 25
  • 26. Extended statistics  Dbms_stats package provides a function to create extended statististics.  Following code creates an extended statistics on (color,shape) capturing correlation between the columns color and shape. SELECT dbms_stats.create_extended_stats( ownname=>user, tabname => 'T1',extension => '(color, shape )' ) AS c1_c2_correlation FROM dual; C1_C2_CORRELATION --------------------------------- SYS_STUAOJW6_2K$IUXLR#$DK235BV ©OraInternals Riyaj Shamsudeen 26
  • 27.  Creating extended statistics adds a hidden, virtual column to that table.  Following code collects statistics on virtual column with histogram. ©OraInternals Riyaj Shamsudeen 27 Virtual column select owner, table_name, column_name, hidden_column, virtual_column from dba_tab_cols where table_name='T1' and owner='CBO2' order by column_id; OWNER TABLE COLUMN_NAME HID VIR ----- ----- ------------------------------ --- --- ... CBO2 T1 SHAPE NO NO CBO2 T1 SYS_STUAOJW6_2K$IUXLR#$DK235BV YES YES begin dbms_stats.gather_Table_stats( user, 'T1', estimate_percent => 100, method_opt => 'for all columns size 254'); end; /
  • 28. Better estimates …2  Cardinality estimates are much better. explain plan for select count(*) from t1 where color='blue' and shape='triangle'; select * from table(dbms_xplan.display); --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 16 | 4 (0)| 00:00:01 | | 1 | SORT AGGREGATE | | 1 | 16 | | | |* 2 | TABLE ACCESS FULL| T1 | 9 | 144 | 4 (0)| 00:00:01 | --------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("SHAPE"='triangle' AND "COLOR"='blue') ©OraInternals Riyaj Shamsudeen 28
  • 29. Better estimates  For other combinations also estimate is very close. SQL> explain plan for select count(*) from t1 where color='black' and ©OraInternals Riyaj Shamsudeen 29 shape='rectangle'; Explained. SQL> select * from table(dbms_xplan.display); --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 16 | 4 (0)| 00:00:01 | | 1 | SORT AGGREGATE | | 1 | 16 | | | |* 2 | TABLE ACCESS FULL| T1 | 900 | 14400 | 4 (0)| 00:00:01 | --------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("COLOR"='black' AND "SHAPE"='rectangle')
  • 30. Non-existing values  For non-existing combinations also, estimates are much better. SQL> explain plan for select count(*) from t1 where color='blue' and ©OraInternals Riyaj Shamsudeen 30 shape='rectangle'; Explained. SQL> select * from table(dbms_xplan.display); --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 16 | 4 (0)| 00:00:01 | | 1 | SORT AGGREGATE | | 1 | 16 | | | |* 2 | TABLE ACCESS FULL| T1 | 5 | 80 | 4 (0)| 00:00:01 | --------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("COLOR"='blue' AND "SHAPE"='rectangle')
  • 31. ©OraInternals Riyaj Shamsudeen 31 References 1. Oracle support site. Metalink.oracle.com. Various documents 2. Internal’s guru Steve Adam’s website www.ixora.com.au 3. Jonathan Lewis’ website www.jlcomp.daemon.co.uk 4. Julian Dyke’s website www.julian-dyke.com 5. ‘Oracle8i Internal Services for Waits, Latches, Locks, and Memory’ by Steve Adams 6. Randolf Geist : http://oracle-randolf.blogspot.com 7. Tom Kyte’s website Asktom.oracle.com