Karl Reitschuster researches CBO cardinality
This outstanding
blog has some really good insights into the behavior of Oracle
cost-based SQL optimizer regarding how Oracle estimates
cardinality. Cardinality estimates are used when the optimizer
makes important decisions such as how to access a table, which index
to use, and what order to join tables together:
http://orcasoracle.bloghi.com/2006/01/14/cbo-cardinality-oh-cardinaltiy.html
Reitschuster first
notes that when dealing with flat files (SQL*Loader, Oracle import,
external tables), Oracle seems to use a cardinality=1:
“The
strange thing is the estimated Cardinality of the FULL Table Scan.
The optimizer believed that only one Compare between the
MainCustomer Column and the SubCustomer Column would meet the
Predicate. . .
CBO
estimates it’s costs in for a relational database (You may never
forget that). It believes that all important relational rules are
kept. Specially the rule of normalization. The Relationship designed
in the import table is flat and not expressible via a foreign key
constraint.”
Reitschuster also
explores the impact of CBO cardinality estimation of the choices of
join methods (nested loop vs. hash join) and demonstrates the use of
the CARDINALITY hint to force a hash join:
“The way
Oracle joins depends very strongly (sic) on the expected Cardinality
of the joined table.
Low
Cardinality -> Nested Loops
. . .
High
Cardinality -> HASH JOIN
In the
following example the true expected cardinality (sic) of the
operation was set via the CARDINALITY Hint. This hint makes only
sense if you now that the Cardinality is often the same which is
sometimes given for external application data or for static data.”
Karl concludes:
“If you
look at explain try to concentrate on the cardinality. It has a very
big impact on the design of the whole execution plan. Miss
estimations can lead to SQL’s with dramatically increased response
times.”