Professional Documents
Culture Documents
Oracle Optimizer
The optimizer is the part of the Oracle Server that creates the execution plan for a SQL
statement. An execution plan is a series of operations that are performed to execute the
statement. The optimizer uses various pieces of information to determine the best path:
• Hints supplied by the developer
• Statistics
• Information in the dictionary
• WHERE clause
The optimizer usually works in the background. However, with diagnostic tools such as
EXPLAIN and SQL*Plus AUTOTRACE, you can see the decisions that the optimizer makes.
The optimizer determines the least-cost plan (most efficient way) to execute a SQL statement
after considering many factors related to the objects referenced and the conditions specified in
the query. This determination is an important step in the processing of any SQL statement and
can greatly affect execution time.
Note: The optimizer may not make the same decisions from one version of the Oracle Database
to the next. In recent versions, the optimizer may make different decisions because better
information is available.
Query
transformer
Transformed query
Estimator Dictionary
Statistics
Query + estimates
Plan
generator
Query plan
(to row-source generator)
Selectivity
Selectivity represents a fraction of rows from a row set. The row set can be a base table, a view,
or the result of a join or a GROUP BY operator. The selectivity depends on the predicate, such as
last_name = 'Smith', or a combination of predicates, such as last_name = 'Smith'
AND salary >2000. The selectivity of a predicate indicates how many rows from a row set
will pass the predicate test.
# rows satisfying a condition
Selectivity = -----------------------------
Total # of rows
When statistics are available, the estimator uses them to estimate selectivity. For example, for an
equality predicate (last_name = 'Smith'), selectivity is set to the reciprocal of the
number of n distinct values of last_name, because the query selects rows that all contain one
out of n distinct values. If a histogram is available on the last_name column, then the
estimator uses it instead of the number of distinct values. If no statistics are available, then the
optimizer uses either dynamic sampling or an internal default value, depending on the value of
the OPTIMIZER_DYNAMIC_SAMPLING initialization parameter. Assumption of a default
value can result in a less-than-optimal plan being used.
Best throughput
• Time required to complete the request
• Suitable for:
– Batch processing
– Report applications
Fast response
• Time for retrieving the first rows
• Suitable for:
– Interactive applications
– Web-based or GUI applications
• Full-table scans
• Row ID scans
• Index scans
• Cluster scans
• Hash scans
Access Paths
Access paths are ways in which data is retrieved from the database. Any row can be located and
retrieved with one of the methods listed in the slide.
Join Orders
A join order is the order in which different join items (such as tables) are accessed and joined
together. For example, in a join order of table1, table2, and table3, table1 is accessed first. Next
table2 is accessed, and its data is joined to table1 data to produce a join of table1 and table2.
Finally, table3 is accessed, and its data is joined to the result of the join between table1 and
table2.
Join Methods
A join operation combines the output from two row sources (such as tables or views ) and returns
one resulting row source (data set). The Oracle Server supports the join methods that are listed in
the slide.
Summary
This lesson introduced the Oracle optimizer. The optimizer uses statistics to calculate the cost of
access paths, taking into account the number of logical I/Os, CPU usage, and network traffic.
You also learned how to set the optimizer approach at the instance and session level with
different settings:
• FIRST_ROWS_n Optimizes response time for the first result set
• ALL_ROWS Optimizes overall throughput