DEV Community

Pranav Bakare
Pranav Bakare

Posted on

Oracle Optimizer

Oracle Optimizer follows a series of steps to generate an optimal query execution plan for a SQL query. These steps ensure efficient data retrieval while minimizing resource consumption. Below is a detailed explanation of the process:


  1. Query Parsing

Purpose: Validates the SQL statement for syntax and semantic correctness.

Process:

The SQL is checked for proper syntax and valid table/column references.

The query is converted into an internal query tree (logical representation).

Oracle determines whether the query is valid and executable.


  1. Query Transformation

Purpose: Rewrites the SQL query into a more efficient equivalent version.

Techniques:

Predicate Pushdown: Moves filters closer to the data source for earlier row elimination.

View Merging: Integrates views directly into the query for simpler processing.

Subquery Unnesting: Converts subqueries into joins for better optimization.

Common Subexpression Elimination: Identifies and reuses duplicate expressions.


  1. Estimation of Query Components

Purpose: Estimates the cost of different query operations based on available statistics.

Factors Considered:

Cardinality: Estimated number of rows processed by each step.

Selectivity: Proportion of rows satisfying query conditions.

Cost: Resource consumption (CPU, I/O, and memory).

Data Sources:

Optimizer Statistics: Table/column statistics collected via DBMS_STATS.

Dynamic Sampling: Estimates statistics for missing or stale data.

Histograms: Provides detailed data distribution for better selectivity estimates.


  1. Access Path Determination

Purpose: Selects the best method to retrieve rows from tables.

Options:

Full Table Scan: Scans all rows in a table.

Index Scan: Uses indexes to locate specific rows.

Partition Pruning: Reads only relevant partitions of a table.

Index Fast Full Scan: Reads the entire index without accessing the table.

Example:

SELECT * FROM employees WHERE department_id = 10;

If department_id is indexed, the optimizer may choose an Index Range Scan.


  1. Join Method Selection

Purpose: Determines the most efficient way to join tables in a multi-table query.

Methods:

  1. Nested Loops Join: Efficient for small datasets or highly selective conditions.

  2. Hash Join: Suitable for large, unsorted datasets.

  3. Merge Join: Ideal for pre-sorted data or indexed joins.

  4. Cartesian Join: Used only when no join condition exists (least efficient).

Example:

SELECT e.name, d.name
FROM employees e JOIN departments d
ON e.department_id = d.department_id;

The optimizer evaluates the size of employees and departments to choose the best join method.


  1. Query Block Optimization

Purpose: Treats each subquery or view as a separate query block.

Process:

Optimizes each block individually.

Merges blocks when beneficial (e.g., view merging or subquery unnesting).


  1. Plan Enumeration

Purpose: Generates all possible execution plans for the query.

Process:

Combines different access paths, join methods, and execution orders.

Enumerates potential plans for each query block.


  1. Plan Costing

Purpose: Assigns a "cost" to each generated plan.

Cost Calculation:

Based on cardinality, I/O cost, CPU cost, and memory usage.

The plan with the lowest cost is selected.

Example: For a query with a join and filter:

SELECT *
FROM orders o, customers c
WHERE o.customer_id = c.customer_id
AND c.city = 'New York';

Cost factors include:

Filter selectivity (c.city = 'New York').

Join order (orders → customers or customers → orders).

Access paths (index scan vs. full table scan).


  1. Plan Selection

Purpose: Chooses the best execution plan from the enumerated plans.

Output:

A query execution plan that specifies how Oracle will execute the query.

Visualization:

Use EXPLAIN PLAN or DBMS_XPLAN to view the selected execution plan.


  1. Execution

Once the query plan is generated, the SQL statement is executed as per the chosen plan.

If adaptive optimization is enabled, the optimizer may adjust the plan during execution based on real-time data.


Key Tools to Understand Optimizer Decisions

  1. EXPLAIN PLAN: Displays the planned execution steps.

EXPLAIN PLAN FOR SELECT * FROM employees WHERE department_id = 10;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);

  1. SQL Trace: Captures detailed execution metrics.

  2. TKPROF: Formats SQL trace output.

  3. AUTOTRACE: Provides execution statistics and query plans.


Example of Execution Plan Generation

Query:

SELECT e.name, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.department_id
WHERE e.salary > 5000 AND d.location_id = 1700;

Optimizer Steps:

  1. Parsing: Validates syntax and semantics.

  2. Transformation:

Applies filter pushdown (d.location_id = 1700 pushed to departments).

  1. Estimation:

Estimates cardinality for employees and departments.

Calculates selectivity for e.salary > 5000 and d.location_id = 1700.

  1. Access Path Selection:

Uses an index on department_id for the join.

Applies a filter on location_id early.

  1. Join Method: Chooses a Hash Join due to large table sizes.

  2. Plan Costing: Evaluates and selects the lowest-cost plan.

  3. Plan Selection: Generates the final query execution plan.


By understanding these steps, developers and DBAs can effectively analyze and optimize SQL queries for better performance.

Top comments (0)