Introduction to the Query Optimizer
The query optimizer is built-in database software that determines the
most efficient method for a SQL statement to access requested data.
Purpose of the Query Optimizer
The optimizer attempts to generate the most optimal execution plan
for a SQL statement.
The optimizer chooses the plan with the lowest cost among all
considered candidate plans.
The optimizer uses available statistics to calculate cost.
For a specific query in a given environment, the cost computation
accounts for factors of query execution such as I/O, CPU, and
communication.
Cost-Based Optimization
Query optimization is the overall process of choosing the most
efficient means of executing a SQL statement.
SQL is a nonprocedural language, so the optimizer is free to merge,
reorganize, and process in any order.
The database optimizes each SQL statement based on statistics
collected about the accessed data.
The optimizer determines the optimal plan for a SQL statement by
examining multiple access methods, such as full table scan or index
scans, different join methods such as nested loops and hash joins,
different join orders, and possible transformations.
Execution Plans
An execution plan describes a recommended method of execution
for a SQL statement.
The plan shows the combination of the steps Oracle Database uses
to execute a SQL statement.
Each step either retrieves rows of data physically from the database
or prepares them for the user issuing the statement.
SELECT first_name, last_name
FROM hr.employees
WHERE department_id
IN (SELECT department_id
FROM hr.departments
WHERE location_id = 1800);
Analogy for the Optimizer
I need the most efficient route from point A to point B"
or
"I need the most efficient route from point A to point B by way of point
C."
"I want to arrive as fast as possible"
Or
"I want the easiest ride possible."
Optimizer Components
Query Transformer
Estimator
The estimator is the component of the optimizer that determines the
overall cost of a given execution plan.
Plan Generator
The plan generator explores various plans for a query block by
trying out different access paths, join methods, and join orders.
Many plans are possible because of the various combinations that
the database can use to produce the same result.
The optimizer picks the plan with the lowest cost.