DEV Community

Pranav Bakare
Pranav Bakare

Posted on

Oracle Optimizer with Analogy Explanation

In Oracle, the optimizer is responsible for determining the most efficient execution plan for a given SQL query. It uses various factors such as available indexes, table statistics, available memory, and more to choose the best method for executing a query.

Let's illustrate this with a simple example and draw an analogy to help understand the concept:

Scenario:

Imagine you need to travel from Point A to Point B. There are multiple routes to take: you could drive through a highway, a back road, or even a scenic route. The Oracle Optimizer is like a GPS system that, based on various parameters (traffic conditions, road type, time of day, distance), will determine which route is optimal for your travel.

Now, let's apply this to a SQL query.

SQL Query Example:

SELECT name, salary
FROM employees
WHERE department_id = 10;

Step-by-Step Process:

  1. Initial Analysis:

The optimizer looks at the query and understands the following:

You want the name and salary of employees in department_id 10.

There are two possible ways to access the data: through a full table scan or via an index scan on the department_id column (if it exists).

  1. Consideration of Available Options (Analogous to routes in the GPS):

Full Table Scan: If there is no index on department_id, the optimizer might choose to perform a full table scan. This is like driving through a back road when no highway exists.

Index Scan: If an index exists on department_id, the optimizer might choose an index scan. This is like taking the highway, which is faster for reaching your destination.

  1. Cost Calculation (GPS estimating travel time):

The optimizer uses statistics (like the number of rows in the employees table, the distribution of department_id, the size of the table, etc.) to estimate the cost of both approaches:

Full Table Scan Cost: If the table is small, a full scan might be more efficient.

Index Scan Cost: If the table is large but the department_id values are evenly distributed, an index scan might be faster.

  1. Choosing the Best Plan (GPS making a recommendation):

Based on the statistics and available options, the optimizer chooses the best plan. For example:

If the employees table is small, it may choose a full table scan (back road).

If the table is large and there is an index on department_id, it may choose an index scan (highway).

Execution Plan Example:

Let's see the plan the Oracle Optimizer generates.

Scenario 1: Without Index on department_id

EXPLAIN PLAN FOR
SELECT name, salary
FROM employees
WHERE department_id = 10;

SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);

Execution Plan Output:


| Id | Operation | Name | Cost (%CPU) |

| 0 | SELECT STATEMENT | | 5 (100) |

| 1 | TABLE ACCESS FULL | EMPLOYEES | 5 (100) |

TABLE ACCESS FULL: The optimizer chose a full table scan because no index is available or the table is small enough for this approach to be efficient.

Scenario 2: With Index on department_id

Assume we create an index on department_id:

CREATE INDEX idx_dept_id ON employees(department_id);

Now, let's run the query again:

EXPLAIN PLAN FOR
SELECT name, salary
FROM employees
WHERE department_id = 10;

SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);

Execution Plan Output:


| Id | Operation | Name | Cost (%CPU) |

| 0 | SELECT STATEMENT | | 3 (100) |
| 1 | INDEX RANGE SCAN | IDX_DEPT_ID | 3 (100) |

| 2 | TABLE ACCESS BY ROWID | EMPLOYEES | 1 (0) |

INDEX RANGE SCAN: The optimizer chose an index scan, where it uses the index on department_id to quickly locate the rows that match the department_id = 10.

TABLE ACCESS BY ROWID: Once the rows are identified, it fetches the actual data from the table using the row IDs obtained through the index.

Summary (Analogy Conclusion):

Oracle Optimizer is like a GPS system that determines the most efficient route (execution plan) for getting from Point A (start of query) to Point B (query results).

It evaluates different options (full scan or index scan) and chooses the best one based on factors like data volume, availability of indexes, and the estimated cost of each route (method).

Top comments (0)