Tuning with Cost-Based Optimization (CBO)
Oracle Tips by Burleson
The cost-based optimizer uses “statistics” that
are collected from the table using the analyze table and
analyze index commands. Oracle uses these metrics about the tables
in order to intelligently determine the most efficient way of
servicing the SQL query. It is important to recognize that in many
cases the cost-based optimizer may not always make the proper decision
in terms of the speed of the query, and Oracle has provided numerous
hints to allow the DBA and developer to tune the execution plan. The
cost-based optimizer is constantly being improved, but there are still
many cases where the rule-based optimizer will result in faster Oracle
queries. One of the first things a seasoned Oracle DBA does when
tuning an SQL statement is to add a RULE hint, or use the alter
session set optimizer goal = rule statement in order to change the
default optimizer mode from cost-based to rule-based optimization.
Here is a list of common hints that are used to
change the execution plan in the cost-based optimizer:
is the cost-based approach designed to provide the best overall
throughput and minimum resource consumption.
AND_EQUAL(table_name index_name1) Causes
merge scans of two to five single-column indexes.
a cluster scan of the table_name.
is the cost-based approach designed to provide the best response time.
the bypassing of indexes, doing a full table scan.
a hash scan of table_name.
hint is placed in a NOT IN subquery to perform a hash anti-JOIN.
INDEX(table_name index_name) Requests
the use of the specified index against the table. If no index is
specified, Oracle will choose the best index.
INDEX_ASC(table_name index_name) Requests
to use the ascending index on a range scan operation.
INDEX_COMBINE(table_name index_name) Requests
that the specified bitmapped index be used.
INDEX_DESC(table_name index_name) Requests
to use the descending index on a range scan operation.
hint is placed in a NOT IN subquery to perform an anti-JOIN.
the query not to perform OR expansion (i.e., OR concatenation).
hint is used in a view to prevent it from being merged into a parent
hint causes the table CACHE option to be bypassed.
hint turns off the Parallel Query option.
that the tables should be joined in the order that they are specified
(left to right). For example, if you know that a state table has only
50 rows, you may want to use this hint to make state the driving
PARALLEL(table_name degree) For
full table scans, this hint requests that the table_name query
be executed in parallel mode with degree processes servicing
the table access.
hint causes all subqueries in the query block to be executed at the
earliest possible time.
a rowid scan of the specified table.
that the rule-based optimizer should be invoked (sometimes due to the
absence of table statistics).
hint forces the use of a star query plan, provided that there are at
least three tables in the query and a concatenated index exists on the
that a UNION ALL be used for all OR conditions.
USE_HASH(table_name1 table_name2) Requests
a hash JOIN against the specified tables.
a sort merge operation.
a nested loop operation with the specified table as the driving table.
It is beyond the scope of this chapter to go
into all of these hints, so for now just consider hints to be the
tools you use to tune cost-based execution plans.
Invoking the Cost-Based Optimizer
Before retrieving any rows, the cost-based optimizer
must create an execution plan that tells Oracle the
order in which to access the desired table and indexes. The cost-based
optimizer works by weighing the relative costs for different access
paths to the data, and choosing the path with the smallest relative
cost. Once the statistics have been collected, there are three ways to
invoke the cost-based optimizer:
Setting the init.ora parameter optimizer_mode all_rows,
ALTER SESSION SET optimizer_goal all_rows
Cost-based hints /*+
all_rows */ or --+ all_rows
These costs for a query are determined with the
aid of table and index statistics that are computed with the
analyze table and analyze index commands in Oracle.
ANALYZE TABLE xxx ESTIMATE STATISTICS SAMPLE
ANALYZE INDEX xxx COMPUTE STATISTICS;
Note that there are documented problems when sampling
less than a 25–30 percent sample of the rows in a table. This is due
to the way Oracle calculates row counts. Contrary to early
documentation, row counts are done using an average row size vs. total
occupied blocks and not a full count. In tables of several million
rows, the row counts can be off by as much as 15 percent if a sample
of less than 25 percent was used for the analysis.
Gathering Statistics for the CBO
It is important that the statistics be
refreshed periodically, especially when the distribution of data
changes frequently. As such, the following SQL may be used to run the
analyze statements for all of the tables and indexes. There
are two methods for analyzing statistics, and this first example uses
the dbms_utility function to perform the analysis. The code below
estimates the statistics for a schema called CPM. Also, the FOR TABLE
FOR ALL INDEXES analyzes the indexes, but without creating entries in
the dba_histograms view.
('CPM','ESTIMATE',null,20,'FOR TABLE FOR ALL INDEXES');
Once the get_stats procedure os created, it can
be scheduled to run weekly using the dbms_job utility.
(:jobno,'GET_STATS;',SYSDATE,'TRUNC(SYSDATE + 7)+9/24');
However, it is not always a good idea to use
Oracle's dbms_utility.analyze_schema or the dbms_ddl.analyze_object
packages to perform this task, since a failure on one of the
statements can affect the results of subsequent statements. The
following script will generate the proper SQL syntax.
# First, we must set the environment . . . .
ORACLE_HOME=`cat /var/opt/oracle/oratab|grep ^$ORACLE_SID:|cut -f2
set pages 999
set heading off
set echo off
set feedback off
'analyze table ' ||owner||'.'||table_name||' estimate statistics
sample 5000 rows;'
where owner not in ('SYS','SYSTEM','PERFSTAT')'DONALD';
'analyze index '||owner||'.'||index_name||' compute statistics;'
where owner not in ('SYS','SYSTEM','PERFSTAT');
set echo on
set feedback on
Most shops schedule a script like this to run
weekly, or whenever there have been significant changes to the table
data. However, it is not necessary to reanalyze tables and indexes
that remain relatively constant. For example, a database where the
tables have a constant number of rows and indexes where the
distribution of values remain constant will not benefit from frequent
This is an excerpt from "Oracle
High Performance tuning with STATSPACK" by Oracle Press.
If you like Oracle tuning, you may enjoy the new book "Oracle
Tuning: The Definitive Reference", over 900 pages of BC's favorite tuning
tips & scripts.
You can buy it direct from the publisher for 30%-off and get instant access to
the code depot of Oracle tuning scripts.