Monday, December 1, 2025

How Oracle decides to reuse Shared SQL Area

How Oracle decides to reuse Shared SQL Area


Introduction:
In the previous post, I reviewed an comparison on SHARED SQL AREA vs. PRIVATE SQL AREA. In this topic, I review most internal steps to SQL ready for processing in Oracle Shared SQL Area and explain how Oracle can reuse statistics and informations gathered in the Shared SQL Area.

The process Oracle uses to decide whether to reuse an existing Shared SQL Area is a critical part of its performance tuning mechanism. It's all about avoiding the costly "hard parse" and instead performing a "soft parse."

Here's how Oracle makes that decision:

1. Hashing the SQL Statement
First, Oracle takes the entire SQL statement text and runs it through a hashing algorithm. This algorithm generates a numeric hash value that represents the SQL text.

2. Checking the Library Cache
Next, Oracle uses this hash value to quickly look up an entry in the Library Cache (which is part of the Shared Pool).

•  If no match is found for the hash value: The SQL statement is new. Oracle has no choice but to perform a hard parse.
This involves:

   1. Syntax Check: Verifying the SQL grammar.

    2. Semantic Check: Validating object names (like tables and columns) and user privileges.

   3. Optimization: Generating one or more potential execution plans and selecting the most efficient one.

    4. Creating the Shared SQL Area: Allocating memory in the Shared Pool to store the parsed statement and the chosen execution plan.

This is a CPU and resource-intensive process that Oracle tries to avoid.


• If a match for the hash value is found: This means one or more SQL statements with the same hash value already exist in the Shared Pool. Now, Oracle must perform additional checks to ensure it's an *exact* match.

3. The String-by-String Comparison

This is the most important step. Even if the hash values match, Oracle performs a character-by-character comparison of the new SQL statement against the one stored in the existing Shared SQL Area.

The SQL statements must be absolutely identical to be considered a match.

This includes:

Whitespace:
SELECT * FROM employees
is different from
SELECT  *      FROM        employees؛

Case (unless configured otherwise):
SELECT * FROM EMPLOYEES
is different from
select * from employees؛

Comments:
SELECT * FROM employees --my query
is different from
SELECT * FROM employees.

Object Names:
SELECT * FROM scott.emp
is different from
SELECT * FROM emp
(even if they resolve to the same table via a synonym).

If this character-by-character comparison fails, Oracle treats it as a new statement and performs a hard parse.

Note: This behavior can mostly related to Cursor Sharing parameter value.(see last section)

4. Environment and Security Check

If the text is identical, Oracle performs one final set of checks on the execution environment. The environment for the new statement must match the environment of the stored statement.

This includes:

Optimizer Settings: The OPTIMIZER_MODE (e.g., `ALL_ROWS`, `FIRST_ROWS`) must be the same.

Schema Objects: The underlying tables, indexes, and statistics must not have changed in a way that would invalidate the existing plan.

For example, if an index used by the plan was dropped, the plan is invalid.

Bind Variable Types: The data types of the bind variables must be compatible.

The Result: Soft vs. Hard Parse

Soft Parse (Match Found): If the SQL text is identical AND the environment checks pass, Oracle reuses the existing Shared SQL Area and its execution plan.
This is a soft parse. It's extremely fast as it skips the complex optimization step.
This is the desired outcome for a high-performance application.

Hard Parse (No Match): If any of the checks fail, Oracle performs a hard parse, creating a new Shared SQL Area.


This is why using bind variables is so critical for application performance.

Statements like:

SELECT * FROM products WHERE product_id = 101;
SELECT * FROM products WHERE product_id = 205;

...are considered two different statements by Oracle, leading to two separate hard parses and two Shared SQL Areas.

However, using a bind variable:

SELECT * FROM products WHERE product_id = :id;

...results in a single statement that can be shared and reused for any `product_id`, leading to one hard parse and many fast soft parses.
How CURSOR_SHARING Affects the SQL Parsing

Process The CURSOR_SHARING parameter does not influence the mandatory validation steps of a parse, but it is the deciding factor in the Soft Parse Decision; the process of reusing an existing cursor.

Hard Parse Prerequisites are Independent: The initial steps of a parse is Syntax Check (verifying grammar) and Semantic Check (validating objects and privileges) are mandatory and must be performed regardless of the CURSOR_SHARING setting.
These steps are not modified by the parameter.
The Soft Parse Decision is Directly Controlled: The core function of CURSOR_SHARING is to define what Oracle considers "identical" for the purpose of cursor reuse in the Shared Pool. This decision is made at the point of the String-by-String Comparison.

1- CURSOR_SHARING = EXACT (Default):

The comparison is performed on the raw SQL text.

Any difference, including case, whitespace, or the literal value in a WHERE clause (e.g., WHERE C=1 vs. WHERE C=2), results in a mismatch, forcing a hard parse.

2- CURSOR_SHARING = FORCE:

Before the comparison, Oracle internally rewrites the SQL statement by replacing all literal values with system-generated bind variables (e.g., :"SYS_B_0").

The comparison is then performed on this rewritten, bind-variable-substituted text.

This allows statements that differ only by their literal values to be considered identical, enabling a soft parse and avoiding the costly Optimization and Shared SQL Area Creation steps.

Impact on Hard Parse Steps: By enabling a soft parse, CURSOR_SHARING allows the database to skip the resource-intensive Optimization (plan generation) and Shared SQL Area Creation steps, which are the most expensive parts of a hard parse.

In summary, CURSOR_SHARING acts as a pre-processor for the soft-parse comparison, effectively expanding the pool of statements that can be considered identical for cursor reuse.

Shared SQL Area vs. Private SQL Area

A comparison between Shared SQL Area and Private SQL Area in Oracle memory architecture


The Shared SQL Area and the Private SQL Area are fundamental memory structures in the Oracle Database, each playing a distinct role in the execution of SQL statements. Here's a comparison of the two:

Shared SQL Area:

The Shared SQL Area is a component of the Shared Pool, which itself resides within the System Global Area (SGA). The SGA is a memory region shared by all Oracle processes.

Key characteristics of the Shared SQL Area include:

Shared Access: As its name suggests, the Shared SQL Area is accessible to all users and sessions in the database.

Contains Execution Plan: It stores the parsed representation and execution plan of a unique SQL statement. This allows the database to reuse the plan for subsequent identical SQL statements, avoiding the overhead of reparsing.

Reduces Memory Usage: By sharing the SQL statement's parsed form and execution plan, the Shared SQL Area significantly reduces overall memory consumption.

Located in the Library Cache: The Shared SQL Area is part of the Library Cache, a key component of the Shared Pool.

LRU Algorithm: The database uses a Least Recently Used (LRU) algorithm to manage the objects in the Shared Pool, including the Shared SQL Areas. This means that if a SQL statement is not executed for a while, its Shared SQL Area might be aged out to make space for new statements.

Private SQL Area (PGA):

The Private SQL Area (PGA) is a memory region that is private to a specific session or connection. It is not shared with other sessions.


Key characteristics of the Private SQL Area include:

Private to a Session: Each session that issues a SQL statement has its own Private SQL Area.

Contains Session-Specific Data: The Private SQL Area stores data that is specific to a particular execution of a SQL statement by a session.

This includes:

Bind variable values: The values supplied to the SQL statement at runtime.

Query execution state information: Information about the current state of the query execution.

Runtime memory structures: Buffers and work areas used for operations like sorting or hash joins.

Location: The location of the Private SQL Area depends on the server connection model:

Dedicated Server: The Private SQL Area is located in the Program Global Area (PGA) of the server process.

Shared Server: Part of the Private SQL Area is located in the System Global Area (SGA), specifically in the Large Pool or Shared Pool.

Cursor Association: A cursor is essentially a handle to a specific Private SQL Area. Closing a cursor releases the memory in the persistent area of the Private SQL Area.

Two Main Areas: The Private SQL Area is divided into two main parts:

Persistent Area: Contains bind information and is freed only when the cursor is closed.

Runtime Area: Contains information about the execution state and is freed when the execution is complete.

Summary of Differences:

Location:

Shared SQL Area: Resides in the Shared Pool, which is a part of the System Global Area (SGA).

Private SQL Area: Typically located in the Program Global Area (PGA) for a dedicated server connection. For shared server connections, part of it is in the SGA.

Accessibility:

Shared SQL Area: It is a public resource, shared by all sessions connected to the database.

Private SQL Area: It is a private resource, accessible only to the specific session that created it.

Contents:

Shared SQL Area: Contains the parsed SQL text and the execution plan, which are not specific to any single session.

Private SQL Area: Holds session-specific information, such as bind variable values, query execution state, and runtime memory buffers.

Purpose:

Shared SQL Area: Its primary purpose is to enable code sharing and reuse, which reduces the overhead of parsing the same SQL statement multiple times.

Private SQL Area: Its purpose is to store the private data and state needed for a specific execution of a SQL statement by a single session.
Lifespan:

Shared SQL Area: Its lifetime is managed by a Least Recently Used (LRU) algorithm within the Shared Pool. It can be aged out if not used, to make space for other statements.

Private SQL Area: Its lifespan is tied to a cursor. The persistent part is freed when the cursor is closed, and the runtime part is freed when the execution is complete.

In essence, when a SQL statement is executed, Oracle checks the Shared Pool for an existing Shared SQL Area for that statement. If one is found, it's reused.

Regardless, a new Private SQL Area is created for the current session to hold its private data for that execution. This architecture allows Oracle to efficiently manage memory and improve performance by sharing the resource-intensive parts of SQL execution while keeping session-specific data separate.

How Oracle decides to reuse Shared SQL Area

How Oracle decides to reuse Shared SQL Area Introduction : In the previous post, I reviewed an comparison on SHARED SQL AREA vs. PRIVATE SQ...