Explanation of Cost-based Optimizer and Rule-based Optimizer

SQL Optimizers
Cost-based optimizer (henceforth CBO) can be used from release 7.2 and above, while rule-based optimizer (henceforth RBO) is recommended for releases 7.1 and below. When using the rule-base optimizer, the indexing of tables and order of clauses within an SQL statement control the access path. The CBO automatically determines the most efficient execution path, and hints can be added to a query to alter the access path. CBO or RBO can be set in the "init.ora" using the following parameter and values associated with it :

When you give Oracle the ability to choose the optimizer mode, it will favour the CBO if any table in a query has statistics (in other words table is analyzed ). And if this happens, Oracle will issue an ANALYZE TABLE ESTIMATE STATISTICS at runtime, slowing down any queries.

All things being equal RBO chooses the driving order by taking the tables in the FROM clause RIGHT to LEFT. CBO determines join order from costs derived from gathered statistics. If there are no stats then CBO chooses the driving order of tables from LEFT to RIGHT in the FROM clause.  This is OPPOSITE to the RBO
 

With the RBO, table names are read in the FROM from RIGHT to LEFT.  In a CBO, table names are read in the FROM from LEFT to RIGHT (if tables are not analyzed).

Hence, it is important to know the driving table, which has the smallest number of rows in a query.

Cost-Based Optimizer
CBO works by weighing the relative 'costs' for different access paths to the data, and choosing the path with the smallest relative cost. The CBO uses statistics derived from tables and indexes. There are three ways to invoke the CBO :

The 'costs' for a query are determined with the aid of table and index statistics that are computed with the following commands :
analyze table estimate statistics sample 10%;
analyze index compute statistics;

ANALYZE with the ESTIMATE option can produce inaccurate results for some tables, especially for small sample sizes.

NOTE: It is important that the statistics are refreshed periodically, especially when the distribution of data changes frequently. Hints override all settings for optimizer_mode and optimizer_goal , while optimizer_goal overrides optimizer_mode settings. The optimizer_mode settings only take effect when neither optimizer_goal settings nor hints are present. Because hints are coded into queries as comments, SQL will not indicate an error if a hint is improperly entered. So it is necessary to ensure that hints are properly stated within your queries.

When issuing the ALTER SESSION SET OPTIMIZER_GOAL command, it is important to remember that the command will not take effect on SQL that is already in shared pool because Oracle will directly load the plan from the shared pool, ignoring the new setting for OPTIMIZER_GOAL. The two ways to get around this problem:


Rule-Based Optimizer
In RBO, the ordering of the table names in the FROM clause determines the driving table. The driving table is important because it is retrieved first, and the rows from the second table are then merged into the result set from the first table. Following are some tips while using RBO :

NOTE: If there are situations where SQL*Plus queries would benefit from an optimizer_mode that is different from SQL in application programs and PL/SQL, then the ALTER SESSION SET OPTIMIZER GOAL statement can be placed in the SQL*Plus global login script to ensure that all SQL*Plus queries use the same optimizer. The global login script can be found in  "$ORACLE_HOME/sqlplus/admin/glogin.sql".

Hints
Hints allow you to provide some input to the CBO as to how to access data. Unfortunately, invalid hints cause the hint to be ignored with no warning so one has to be very careful with the syntax.  The main points to remember when using hints are:

Summary
To make effective use of the CBO you should: