2. About
me…
Vivek
Sharma,
Blog
url
:
h3p://viveklsharma.wordpress.com
Email
:
vlsharma@hotmail.com
A
Regular
Speaker
for
AIOUG
Speaker
of
the
Year
–
Sangam
2012
4. • Cursors
• SelecMvity
• Cardinality
Key
Concepts…
Client
/
Applica-on
Handle
Server
Process
Memory
Private
SQL
Area
SGA
Library
Cache
Shared
SQL
Area
5. Syntax
/
Seman-cs
Checks
Sharable
Parent
Cursor
Available
?
Execute
Store
Parent
Cursor
in
Library
Cache
Query
Transforma-on
/
Execu-on
Plans
Store
child
Cursor
in
Library
Cache
Sharable
Child
Cursor
Available
?
N
N
Y
Y
Life
4me
of
a
Cursor…
6. Syntax
/
Seman-cs
Checks
Sharable
Parent
Cursor
Available
?
Execute
Store
Parent
Cursor
in
Library
Cache
Query
Transforma-on
/
Execu-on
Plans
Store
child
Cursor
in
Library
Cache
Sharable
Child
Cursor
Available
?
N
N
Y
Y
Life
4me
of
a
Cursor…
7. • Cursors
• SelecMvity
• Cardinality
Key
Concepts…
ü Value
between
0
and
1
ü Represents
FracMon
or
%age
of
rows
filtered
ü Drives
the
Access
Path
ü With
no-‐histograms,
it
is
Density
or
1/Num_DisMnct
ü Close
to
0
–
Low
/
Close
to
1
-‐
High
8. • Cursors
• SelecMvity
• Cardinality
Key
Concepts…
ü Number
of
rows
returned
by
an
OperaMon
ü Drives
Access
Order
ü SelecMvity
*
Num_Rows
9. Introduc4on
to
Query
Op4mizer…
SQL
Query
OpMmizer
Object
StaMsMcs
System
StaMsMcs
Database
Design
Fixed
Plans
ExecuMon
Environment
Bind
Variables
Dynamic
Sampling
Cardinality
Feedback
ExecuMon
Plan
10. Object
Sta4s4cs…
• Table
StaMsMcs
o num_rows
o Blocks
• Index
StaMsMcs
o blevel
o leaf_blocks
o clustering_factor
• Column
StaMsMcs
o num_disMnct
o num_nulls
o density
(1/num_disMnct)
o low_value
o high_value
• ParMMon
Level
StaMsMcs
o Single
ParMMon
Scan
• Global
StaMsMcs
(Table
Level)
o All
or
MulMple
ParMMons
Scan
• Histograms
o Frequency
Based
o Height
Balanced
o Top
Frequency
o Hybrid
With
Frequency
1/(2*num_rows)
11. Object
Sta4s4cs
(Cardinality
-‐
No
Nulls)…
Statistics Values
Num_rows 1000
Num_distinct 10 (low 1 and high 10)
Blocks 39
Predicate ORG_ID 1
Density (org_id) 0.1 (No Nulls)
SQL> select id, name from aioug where org_id = 1;
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------
Plan hash value: 1667281916
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 100 | 1800 | 12 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| AIOUG | 100 | 1800 | 12 (0)| 00:00:01 |
---------------------------------------------------------------------------
12. Statistics Values
Num_rows 1000
Num_distinct 10 (low 1 and high 10)
Blocks 39
Density (org_id) 0.1 (No Nulls)
Base Selectivity 0.1 * ((1000-0)/1000) = 0.1
Cardinality 0.1 * 1000 = 100
Object
Sta4s4cs
(Cardinality
-‐
No
Nulls)…
13. Object
Sta4s4cs
(Cardinality
Few
Nulls)…
Statistics Values
Num_rows 1000
Num_distinct 10 (low 1 and high 10)
Blocks 39
Predicate ORG_ID_N 1
Density (org_id_n) 0.1 (100 Nulls)
SQL> select id, name from aioug where org_id_n = 1;
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------
Plan hash value: 1667281916
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 90 | 1620 | 12 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| AIOUG | 90 | 1620 | 12 (0)| 00:00:01 |
---------------------------------------------------------------------------
14. Object
Sta4s4cs
(Cardinality
Few
Nulls)…
Statistics Values
Num_rows 1000
Num_distinct 10 (low 1 and high 10)
Blocks 39
Density (org_id_n) 0.1 (100 Nulls)
Base Selectivity 0.1 * ((1000-100)/1000) = 0.09
Cardinality 0.09 * 1000 = 90
15. Out
of
Bound
(Cardinality
-‐
No
Nulls)…
Statistics Values
Num_rows 1000
Num_distinct 10 (low 1 and high 10)
Blocks 39
Predicate ORG_ID 14
Density (org_id) 0.1 (No Nulls)
SQL> select id, name from aioug where org_id = 14;
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------
Plan hash value: 1667281916
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 56 | 1008 | 11 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| AIOUG | 56 | 1008 | 11 (0)| 00:00:01 |
---------------------------------------------------------------------------
16. Statistics Values
Num_rows 1000
Num_distinct 10 (low 1 and high 10)
Blocks 39
Density (org_id) 0.1 (No Nulls)
Base Selectivity 0.1 * ((1000-0)/1000) = 0.1
Required Value (org_id) 14
Out of Bound Factor ((14-10)/(10-1)) = 0.44444
Cardinality (0.1 * 1000) –
(0.1*1000*0.4444) = 56
Out
of
Bound
(Cardinality
-‐
No
Nulls)…
17. • utl_raw.cast_to_*
• dbms_stats.convert_raw_value
Low
&
High
Value
(Deciphering)…
select column_name, low_value, high_value from dba_tab_columns
where owner='SCOTT' and table_name='AIOUG' order by 1;
COLUMN_NAME LOW_VALUE HIGH_VALUE
-------------------- ----------------------- -----------------------
ID C102 C20B
NAME 41477349586C68677566 7A7073484A505A44544F
ORG_ID C102 C10B
ORG_ID_N C102 C10B
18. Object
Sta4s4cs…
• Table
StaMsMcs
o num_rows
o Blocks
• Index
StaMsMcs
o blevel
o leaf_blocks
o clustering_factor
• Column
StaMsMcs
o num_disMnct
o num_nulls
o density
(1/num_disMnct)
o low_value
o high_value
• ParMMon
Level
StaMsMcs
o Single
ParMMon
Scan
• Global
StaMsMcs
(Table
Level)
o All
or
MulMple
ParMMons
Scan
• Histograms
o Frequency
Based
o Height
Based
o Top
Frequency
o Hybrid
(12c)
With
Frequency
1/(2*num_rows)
19. Index
Sta4s4cs
(low
Clustering
Factor)…
Statistics
Indexed Column ORG_ID ORG_ID_M
Num_rows 1000 1000
Blevel 1 1
Leaf_Blocks 2 2
Clustering_Factor 34 337
SQL> select id, name from aioug where org_id = 1;
Plan hash value: 1442557996
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)|
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 100 | 5 (0)|
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| AIOUG | 100 | 5 (0)|
| 2 | INDEX RANGE SCAN | AIOUG_ORG_ID_IDX | 100 | 1 (0)|
-------------------------------------------------------------------------------------
21. Cost = blevel
+ ceil(leaf_blocks * index_selectivity)
+ ceil(clustering_factor * table_selectivity)
Cost
of
an
Index
Scan
Cost
of
a
Table
Scan
via
Index
CalculaMon
of
CF
(10
Row
3
Block
Table
with
an
Index)
Index Entry Table Block CF
A 1 1
A 1 1
A 2 2
B 3 3
B 1 4
C 3 5
C 2 6
C 2 6
D 1 7
D 3 8
Good
:
Near
to
Table.blocks
Bad
:
Near
to
Table.num_rows
Index
Sta4s4cs
(Clustering
Factor)…