Parsing in Oracle SQL | Hard parsing | soft parsing.

Pranav Bakare - Oct 5 - - Dev Community

In Oracle SQL and databases in general, parsing is the process of breaking down and analyzing SQL statements to ensure they are syntactically and semantically correct before execution. There are two primary types of parsing in Oracle SQL: hard parsing and soft parsing. These determine how much work the database has to do to process the query.

1. Hard Parsing

Definition: Hard parsing occurs when the database has to fully parse a SQL query from scratch, as if it is being seen for the first time.

Characteristics:

The SQL statement is not found in the shared pool (a part of the database memory that caches execution plans).

The database must go through the full compilation process: parsing, binding, optimization, code generation, and execution plan creation.

Hard parsing is resource-intensive and can slow down the performance due to CPU overhead, contention on shared resources, and locking.

When It Happens:

A new, unique SQL statement is issued for the first time.

Changes in SQL structure (e.g., different literals or changes in the query itself).

The shared pool is flushed or purged, and cached queries are removed.

Significant differences in the data being queried (such as cardinality changes) that force a new execution plan to be created.

Implication: Hard parsing consumes more time and resources, so minimizing hard parses is key to database performance optimization.

Example:

SELECT * FROM employees WHERE employee_id = 101;

If this query is issued for the first time and no execution plan is found in the cache, hard parsing occurs.

2. Soft Parsing

Definition: Soft parsing occurs when Oracle can reuse an existing execution plan from the library cache for a SQL statement that has already been parsed and optimized earlier.

Characteristics:

Oracle finds the SQL statement in the shared pool and skips most of the compilation process (e.g., parsing, optimization).

Only minor checks are performed, such as verifying object privileges and ensuring that the previously cached execution plan is still valid.

Soft parsing is much faster and more efficient compared to hard parsing, as it reuses the already compiled execution plan.

When It Happens:

The exact same SQL statement is executed multiple times (either with or without bind variables).

The SQL query is found in the shared pool.

Implication: Soft parsing reduces the overhead of executing frequently repeated queries and improves performance.

Example:

SELECT * FROM employees WHERE employee_id = 101;

If this query was issued recently and the execution plan is still cached, Oracle reuses the plan and performs a soft parse.

  1. Cursor Sharing and Bind Variables

Soft parsing is greatly influenced by the use of bind variables and Oracle's cursor sharing mechanism:

Bind Variables: If you use bind variables (e.g., :employee_id instead of hard-coded values like 101), Oracle is more likely to reuse the same execution plan, leading to soft parses.

Example:

SELECT * FROM employees WHERE employee_id = :employee_id;

In this case, the value of :employee_id can change, but the execution plan can still be reused.

Cursor Sharing: Oracle uses cursors to manage SQL statements and execution plans. If cursor sharing is enabled, Oracle can reuse existing cursors for similar queries, reducing the need for hard parsing.

Benefits of Soft Parsing:

Improved Performance: Soft parsing avoids the CPU and memory overhead associated with hard parsing.

Reduced Resource Contention: Since soft parsing does not require the heavy resources that hard parsing does, it results in less contention for shared resources (e.g., latches and locks).

Scalability: Soft parsing helps in systems where the same SQL queries are executed repeatedly, allowing the system to scale by reducing unnecessary overhead.

Challenges with Hard Parsing:

Performance Bottlenecks: Frequent hard parsing can lead to CPU bottlenecks and reduced overall performance.

Shared Pool Contention: Multiple sessions performing hard parses can lead to contention for resources in the shared pool, resulting in poor database performance.

  1. Library Cache Miss and Hit

Library Cache Hit: If Oracle finds an execution plan in the library cache during parsing, it's called a library cache hit, which leads to soft parsing.

Library Cache Miss: If Oracle does not find an execution plan in the library cache, it leads to hard parsing, which is more resource-intensive.

Key Differences Between Hard Parsing and Soft Parsing:

Conclusion:

Hard Parsing is costly and should be minimized through best practices like using bind variables and ensuring the database is properly configured for cursor sharing.

Soft Parsing is more efficient and reuses execution plans, helping reduce the overhead of repeated SQL queries.

By understanding and managing the types of parsing, database administrators and developers can improve the performance and scalability of Oracle databases.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .