
Cost
Control: Inside the Oracle Optimizer
By Donald K. Burleson
OTN Member since 2001
Designing
new applications for the Oracle Cost-Based Optimizer?
Here's the latest information about how it works. |
|
|
This article has the following sections:
External Costing with the Optimizer
Starting in
Oracle9i
Database and continuing in
Oracle Database 10g, the optimizer has been enhanced to consider external influences when determining the best execution plan.
Because the Oracle Database does not run in a vacuum, the optimizer must be able to factor-in the costs
of external disk I/O and the cost of CPU cycles for each SQL operation. This process is especially critical
for queries running all_rows
optimization, where minimizing server resources is a primary goal.
- CPU_COST
— The optimizer can now estimate the number of machine cycles
required for an operation, and factors this
cost into the execution plan calculation. The CPU costs associated
with servicing an Oracle query depends upon the current server
configuration (which Oracle cannot see). In Oracle Database 10g,
CPU costing is the default behavior because of the importance of
considering the CPU costs
associated with each SQL execution phase—thus, the savvy Oracle
professional should
turn on CPU costing with dbms_stats.get_system_stats.
CPU costs are generally not important unless the entire Oracle
instance is using excessive CPU resources.
- IO_COST
— The optimizer had been enhanced to estimate the number of physical
block reads required for an operation.
The I/O cost is proportional to the number of physical data blocks
read by the operation. However, the optimizer has no a priori
knowledge of the data buffer contents and cannot
distinguish between a logical read (in-buffer) and a physical read.
Because of this shortcoming, the
optimizer cannot know if the data blocks are already in the RAM data
buffers.
According to the Oracle documentation, the
I/O and CPU costs are evaluated as follows:
Cost = (#SRds * sreadtim +
#MRds * mreadtim +
#CPUCycles / cpuspeed ) / sreadtim
where:
#SRDs - number of single block reads
#MRDs - number of multi block reads
#CPUCycles - number of CPU Cycles *)
sreadtim - single block read time
mreadtim - multi block read time
cpuspeed - CPU cycles per second
Note that the costs are a function of the
number of reads and the relative read times, plus the CPU cost
estimate for the query. Also note the external costing does not
consider the number of data blocks that reside
in the RAM data buffers, but a future release of the optimizer is
likely to consider this factor.
Here we see that Oracle uses the both the
CPU and I/O cost estimations in evaluating the execution plans.
This equation becomes even more complex when we factor-in parallel
query where many concurrent processes are servicing the query.
The best benefit for using CPU costing is
for all_rows execution plans
where costs is more important than with
first_rows optimization.
The all_rows optimizer mode is designed to minimize computing
resources and it favors full-table scans. Index access
(first_rows) adds additional I/O overhead, but they return rows
faster, back to the originating query:

Oracle full-table scan
Illustration

Oracle Index
access illustration
Next, let's look at how the optimizer is
influenced by statistics. In order to make an intelligent decision
about the best execution plan, the
optimizer must use information about all of the data objects that are
involved in the query.
Because you control how the statistics are collected, this aspect of
optimizer tuning is a critical.
BC performance tool:

Our favorite is the Ion tool,
the easiest way to analyze Oracle performance and Ion
allows you to spot hidden performance trends.
Donald K. Burleson [info@remote-dba.net]
is one of the world's most widely-read Oracle database experts. He has written 19
books, published more than 100 articles in national magazines, and
serves as editor-in-chief of Oracle Internals, a leading Oracle
database journal. Burleson's latest book is
Creating a Self-Tuning Database
by Rampant TechPress. Don's Web sites are
http://www.dba-oracle.com ,
http://www.remote-dba.net/ and
http://rampant.cc .
|