Oracle Query Execution phases- How query flows?

SQL query flows through the Oracle engine in the following steps:

Step 1: Parsing

  • The SQL query is parsed to check syntax and semantics.
  • The parser breaks the query into smaller components, such as keywords, identifiers, and literals.

Step 2: Optimization

  • The parsed query is optimized to determine the most efficient execution plan.
  • The optimizer considers factors like indexes, statistics, and system resources.

Step 3: Row Source Generation

  • The optimized query is converted into a row source tree, which represents the query’s execution plan.
  • The row source tree includes operators like table scans, index scans, and joins.

Step 4: Execution

  • The row source tree is executed, and data is retrieved from the database.
  • Operators in the row source tree perform actions like scanning tables, joining rows, and filtering data.

Step 5: Fetch

  • The executed query’s results are fetched and returned to the user.
  • The fetch operation may involve sorting, aggregating, or transforming data.

Additional Steps:

  • Binding: Variables in the query are replaced with actual values.
  • Authentication: The user’s credentials are verified.
  • Authorization: Access control checks are performed to ensure the user has necessary privileges.
  • Result caching: Results may be cached to improve performance for future queries.

This high-level overview shows how a query flows through the Oracle engine. Depending on the query complexity and database configuration, additional steps or variations may occur. Now Everything expalined in Detail:-

When a SELECT or DML (Data Manipulation Language) query (such as INSERT, UPDATE, or DELETE) is issued in Oracle, the database engine follows a specific process to ensure efficient execution. Here’s an overview of how a query flows through the Oracle engine:

1. SQL Parsing Phase

Steps Involved:

  • SQL Parsing: When a query is submitted, the Oracle engine parses the SQL statement. During this phase, the query is checked for syntactical correctness (i.e., ensuring the SQL statement adheres to the correct grammar and structure).
  • Semantic Check: The query is also checked for semantic correctness, such as verifying that the referenced tables, columns, and objects exist and the user has the required privileges.

Key Components:

  • SQL Parser: Breaks down the SQL statement and verifies the structure and correctness.
  • Data Dictionary: The engine refers to the data dictionary to check for table and column names, data types, and user privileges.
  • Shared Pool: Oracle checks the Shared Pool (a part of the System Global Area (SGA)) for a previously executed, identical query. If a match is found, the query might skip the parsing and optimization phases (i.e., soft parsing occurs). If no match is found, the query undergoes hard parsing.

Output: After parsing, a parse tree (a data structure representing the syntactic structure of the SQL query) is generated.

2. Optimization Phase

Steps Involved:

  • Query Transformation: Oracle may rewrite the query internally to optimize it. For example, subqueries might be transformed into joins, or certain conditions might be rewritten.
  • Cost-Based Optimization (CBO): The optimizer determines the most efficient execution plan based on statistics (such as the size of the tables, indexes, cardinality, and histograms). The optimizer evaluates different access paths and join methods (e.g., hash joins, nested loop joins) and selects the least costly option. This is done using the Cost-Based Optimizer (CBO) or, in older versions, the Rule-Based Optimizer (RBO).
  • Access Paths: Oracle decides how it will access the data, such as using full table scans, index scans, or partition scans.
  • Join Methods: For queries involving multiple tables, the optimizer chooses how to join them (e.g., nested loops, hash joins, or merge joins).

Key Components:

  • Statistics: The optimizer uses table, index, and column statistics (analyzed via the ANALYZE or DBMS_STATS package) to estimate the cost of different execution plans.
  • Optimizer Hints: Developers can include hints in the SQL statement to guide the optimizer to use specific indexes or join methods.

Output: The result of this phase is an execution plan, which is a step-by-step blueprint for retrieving or modifying data.

3. Row Source Generation Phase

  • In this phase, Oracle converts the optimized execution plan into a sequence of row sources, which are the internal representations of operations (e.g., table scans, index lookups) that will be executed to retrieve the data.
  • Row Sources define the actual operations that Oracle will perform on the data to fetch or manipulate rows.

Key Components:

  • Operators: These are the basic building blocks, such as TABLE ACCESS, INDEX SCAN, SORT, JOIN, etc.

4. Query Execution Phase

For a SELECT Query:

  • The row sources generated in the previous phase are executed.
  • Oracle retrieves the necessary data from the database tables, indexes, or other sources based on the execution plan.
  • Buffers and Caching: Data blocks are retrieved from the buffer cache (if available) or from disk storage (if not cached).
  • PGA Memory: If sorting, hashing, or other intermediate processing is required, it will be done using memory allocated in the Program Global Area (PGA). If the PGA is insufficient, Oracle will use temporary tablespace for disk-based operations.
  • Result Set: Oracle returns the result set to the user after all steps in the execution plan are completed.

For a DML Query (INSERT, UPDATE, DELETE):

  • Data Manipulation: Oracle modifies the data as instructed by the DML query, either by inserting new rows, updating existing rows, or deleting rows.
  • Redo Log Generation: Oracle generates redo logs that record the changes for recovery purposes. These are written to the redo log buffer in the SGA and eventually flushed to the redo log files on disk.
  • Undo Generation: Oracle generates undo records that allow the changes to be rolled back if needed (e.g., in case of transaction failure or rollback). Undo records are stored in the undo tablespace.

Commit and Rollback:

  • COMMIT: If a COMMIT is issued, Oracle makes the changes permanent by writing the redo log buffer to disk, updating control files, and marking the transaction as committed.
  • ROLLBACK: If a ROLLBACK is issued, Oracle uses the undo data to reverse the changes.

5. Fetching and Result Return (For SELECT)

  • Oracle fetches rows from the underlying tables, applies any necessary filters (e.g., WHERE clause), and performs join operations if needed.
  • The result is sent back to the client or application.
  • Oracle continues to fetch and return rows until the entire result set is processed.

Oracle Query Execution Architecture (High-Level):

  1. Client/SQL Developer: The client submits a query.
  2. Oracle SQL Engine:
    • Parsing: Syntax and semantic checks.
    • Optimization: Generating an optimal execution plan.
    • Row Source Generation: Mapping the execution plan into actual operations.
    • Execution: Retrieving data or modifying data according to the plan.
    • Result Fetching: Returning results to the client or committing changes.

Key Oracle Memory Structures Involved

  1. System Global Area (SGA):
    • Shared Pool: Stores parsed SQL, execution plans, and dictionary cache.
    • Buffer Cache: Caches data blocks read from disk to reduce disk I/O.
    • Redo Log Buffer: Temporarily stores redo entries for changes made to data.
  2. Program Global Area (PGA):
    • Sorting: Used for operations like sorting, hashing, and join operations.
    • Session Information: Stores session-specific data for the user.
  3. Undo Tablespace: Stores undo information for transactions, enabling rollback and ensuring read consistency.
  4. Redo Log Files: Ensures data is recoverable in case of system crashes.

Execution Plan Example (EXPLAIN PLAN)

You can use EXPLAIN PLAN to see the steps Oracle will take to execute a query:

sqlCopy codeEXPLAIN PLAN FOR
SELECT * FROM employees WHERE department_id = 10;

SELECT * FROM table(DBMS_XPLAN.DISPLAY);

This will show the execution plan, including details on index scans, full table scans, and join methods.

Conclusion

Oracle’s query execution involves several steps: parsing, optimization, row source generation, and execution. Each step plays a crucial role in ensuring efficient and reliable query execution. By understanding these phases, you can optimize queries and troubleshoot performance issues more effectively.


Discover more from AI HintsToday

Subscribe to get the latest posts sent to your email.

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Entries:-

  • Data Engineering Job Interview Questions :- Datawarehouse Terms
  • Oracle Query Execution phases- How query flows?
  • Pyspark -Introduction, Components, Compared With Hadoop
  • PySpark Architecture- (Driver- Executor) , Web Interface
  • Memory Management through Hadoop Traditional map reduce vs Pyspark- explained with example of Complex data pipeline used for Both used
  • Example Spark submit command used in very complex etl Jobs
  • Deploying a PySpark job- Explain Various Methods and Processes Involved
  • What is Hive?
  • In How many ways pyspark script can be executed? Detailed explanation
  • DAG Scheduler in Spark: Detailed Explanation, How it is involved at architecture Level
  • CPU Cores, executors, executor memory in pyspark- Expalin Memory Management in Pyspark
  • Pyspark- Jobs , Stages and Tasks explained
  • A DAG Stage in Pyspark is divided into tasks based on the partitions of the data. How these partitions are decided?
  • Apache Spark- Partitioning and Shuffling
  • Discuss Spark Data Types, Spark Schemas- How Sparks infers Schema?
  • String Data Manipulation and Data Cleaning in Pyspark

Discover more from AI HintsToday

Subscribe now to keep reading and get access to the full archive.

Continue reading