steps in select query execution

SQL processing is

  1. The parsing,
  2. Optimization,
  3. Row source generation,
  4. And execution of a SQL statement.

 

Depending on the statement, the database may omit some of these stages.

 

SQL Parsing

 

The first stage of SQL processing is parsing.

 

The parsing stage involves separating the pieces of a SQL statement into a data structure that other routines can process. The database parses a statement when instructed by the application, which means that only the application?, and not the database itself, can reduce the number of parses.

 

When an application issues a SQL statement, the application makes a parse call to the database to prepare the statement for execution.

The parse call opens or creates a cursor, which is a handle for the session-specific private SQL area that holds a parsed SQL statement and other processing information. The cursor and private SQL area are in the program global area (PGA).

 

During the parse call, the database performs the following checks:

?            Syntax Check

?            Semantic Check

?            Shared Pool Check

 

The preceding checks identify the errors that can be found before statement execution. Some errors cannot be caught by parsing. For example, the database can encounter deadlocks or errors in data conversion only during statement execution.

 

Syntax Check

Oracle Database must check each SQL statement for syntactic validity.

A statement that breaks a rule for well-formed SQL syntax fails the check. For example, the following statement fails because the keyword FROM is misspelled as FORM:

SQL> SELECT * FORM employees;

SELECT * FORM employees

        *

ERROR at line 1:

ORA-00923: FROM keyword not found where expected

 

Semantic Check

The semantics of a statement are its meaning. A semantic check determines whether a statement is meaningful, for example, whether the objects and columns in the statement exist.

A syntactically correct statement can fail a semantic check, as shown in the following example of a query of a nonexistent table:

SQL> SELECT * FROM nonexistent_table;

SELECT * FROM nonexistent_table

             *

ERROR at line 1:

ORA-00942: table or view does not exist

 

Shared Pool Check

During the parse, the database performs a shared pool check to determine whether it can skip resource-intensive steps of statement processing.

To this end, the database uses a hashing algorithm to generate a hash value for every SQL statement. The statement hash value is the SQL ID shown in V$SQL.SQL_ID. This hash value is deterministic within a version of Oracle Database, so the same statement in a single instance or in different instances has the same SQL ID.

When a user submits a SQL statement, the database searches the shared SQL area to see if an existing parsed statement has the same hash value. The hash value of a SQL statement is distinct from the following values:

?            Memory address for the statement

Oracle Database uses the SQL ID to perform a keyed read in a lookup table. In this way, the database obtains possible memory addresses of the statement.

?            Hash value of an execution plan for the statement

A SQL statement can have multiple plans in the shared pool. Typically, each plan has a different hash value. If the same SQL ID has multiple plan hash values, then the database knows that multiple plans exist for this SQL ID.

Parse operations fall into the following categories, depending on the type of statement submitted and the result of the hash check:

?            Hard parse

If Oracle Database cannot reuse existing code, then it must build a new executable version of the application code. This operation is known as a hard parse, or a library cache miss.

Note:

The database always performs a hard parse of DDL.

 

During the hard parse, the database accesses the library cache and data dictionary cache numerous times to check the data dictionary. When the database accesses these areas, it uses a serialization device called a latch on required objects so that their definition does not change. Latch contention increases statement execution time and decreases concurrency.

 

?            Soft parse

A soft parse is any parse that is not a hard parse. If the submitted statement is the same as a reusable SQL statement in the shared pool, then Oracle Database reuses the existing code. This reuse of code is also called a library cache hit.

Soft parses can vary in how much work they perform. For example, configuring the session shared SQL area can sometimes reduce the amount of latching in the soft parses, making them "softer."

In general, a soft parse is preferable to a hard parse because the database skips the optimization and row source generation steps, proceeding straight to execution.

 

If a check determines that a statement in the shared pool has the same hash value, then the database performs semantic and environment checks to determine whether the statements have the same meaning. Identical syntax is not sufficient. For example, suppose two different users log in to the database and issue the following SQL statements:

CREATE TABLE my_table ( some_col INTEGER );

SELECT * FROM my_table;

 

The SELECT statements for the two users are syntactically identical, but two separate schema objects are named my_table. This semantic difference means that the second statement cannot reuse the code for the first statement.

 

Even if two statements are semantically identical, an environmental difference can force a hard parse. In this context, the optimizer environment is the totality of session settings that can affect execution plan generation, such as the work area size or optimizer settings (for example, the optimizer mode). Consider the following series of SQL statements executed by a single user:

ALTER SESSION SET OPTIMIZER_MODE=ALL_ROWS;

ALTER SYSTEM FLUSH SHARED_POOL;              # optimizer environment 1

SELECT * FROM sh.sales;

 

ALTER SESSION SET OPTIMIZER_MODE=FIRST_ROWS; # optimizer environment 2

SELECT * FROM sh.sales;

 

ALTER SESSION SET SQL_TRACE=true;            # optimizer environment 3

SELECT * FROM sh.sales;

In the preceding example, the same SELECT statement is executed in three different optimizer environments. Consequently, the database creates three separate shared SQL areas for these statements and forces a hard parse of each statement.

SQL Optimization

During the optimization stage, Oracle Database must perform a hard parse at least once for every unique DML statement and performs the optimization during this parse.

The database never optimizes DDL unless it includes a DML component such as a subquery that requires optimization.

SQL Row Source Generation

The row source generator is software that receives the optimal execution plan from the optimizer and produces an iterative execution plan that is usable by the rest of the database.

The iterative plan is a binary program that, when executed by the SQL engine, produces the result set. The plan takes the form of a combination of steps. Each step returns a row set. The next step either uses the rows in this set, or the last step returns the rows to the application issuing the SQL statement.

A row source is a row set returned by a step in the execution plan along with a control structure that can iteratively process the rows. The row source can be a table, view, or result of a join or grouping operation.

The row source generator produces a row source tree, which is a collection of row sources. The row source tree shows the following information:

?            An ordering of the tables referenced by the statement

?            An access method for each table mentioned in the statement

?            A join method for tables affected by join operations in the statement

?            Data operations such as filter, sort, or aggregation

 

Execution Plan

----------------------------------------------------------

Plan hash value: 975837011

 

--------------------------------------------------------------------------------

| Id| Operation                    | Name       |Rows|Bytes|Cost(%CPU)|Time |

--------------------------------------------------------------------------------

| 0 | SELECT STATEMENT             |            | 3 | 189 | 7(15)| 00:00:01 |

|*1 | HASH JOIN                   |            | 3 | 189 | 7(15)| 00:00:01 |

|*2 |  HASH JOIN                  |            | 3 | 141 | 5(20)| 00:00:01 |

| 3 |   TABLE ACCESS BY INDEX ROWID| EMPLOYEES  | 3 | 60 | 2 (0)| 00:00:01 |

|*4 |    INDEX RANGE SCAN         | EMP_NAME_IX | 3 |    | 1 (0)| 00:00:01 |

| 5 |   TABLE ACCESS FULL         | JOBS       | 19 | 513 | 2 (0)| 00:00:01 |

| 6 |  TABLE ACCESS FULL          | DEPARTMENTS | 27 | 432 | 2 (0)| 00:00:01 |

--------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------

  1 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")

  2 - access("E"."JOB_ID"="J"."JOB_ID")

  4 - access("E"."LAST_NAME" LIKE 'A%')

      filter("E"."LAST_NAME" LIKE 'A%')

SQL Execution

During execution, the SQL engine executes each row source in the tree produced by the row source generator. This step is the only mandatory step in DML processing.

Each step in an execution plan has an ID number. The numbers in Figure 3-3 correspond to the Id column in the plan shown in Example 3-1. Initial spaces in the Operation column of the plan indicate hierarchical relationships. For example, if the name of an operation is preceded by two spaces, then this operation is a child of an operation preceded by one space. Operations preceded by one space are children of the SELECT statement itself.

How Oracle Database Processes DML

Most DML statements have a query component. In a query, execution of a cursor places the results of the query into a set of rows called the result set.


How Row Sets Are Fetched

Result set rows can be fetched either a row at a time or in groups.

In the fetch stage, the database selects rows and, if requested by the query, orders the rows. Each successive fetch retrieves another row of the result until the last row has been fetched.

In general, the database cannot determine for certain the number of rows to be retrieved by a query until the last row is fetched. Oracle Database retrieves the data in response to fetch calls, so that the more rows the database reads, the more work it performs. For some queries the database returns the first row as quickly as possible, whereas for others it creates the entire result set before returning the first row. 

Read Consistency

In general, a query retrieves data by using the Oracle Database read consistency mechanism, which guarantees that all data blocks read by a query are consistent to a single point in time.

Read consistency uses undo data to show past versions of data. For an example, suppose a query must read 100 data blocks in a full table scan. The query processes the first 10 blocks while DML in a different session modifies block 75. When the first session reaches block 75, it realizes the change and uses undo data to retrieve the old, unmodified version of the data and construct a noncurrent version of block 75 in memory.

Mohammed Abdul Hameed

Sr. Database Administrator at Netsoftmate IT Solutions, OCA, DBaaS,DATAWAREHOUSE,EXADATA, Performance Tuning,Blogger...

7 å¹´

Good one.

赞
回复

Thanks for sharing. Regards.

赞
回复
Ankit Aggarwal

Oracle Database Administrator with 14 years IT experience

7 å¹´

add-in 4 above..

  • 该图片无替代文字

要查看或添加评论,请登录

Ankit Aggarwal的更多文章

  • How to check RAC database status

    How to check RAC database status

    simply answer is Oracle provides the crs and srvctl command utility for checking on the external status of RAC…

  • Difference between the Checkpoint and SCN

    Difference between the Checkpoint and SCN

    Checkpoint ?Checkpoint in database is used to reduce the amount of the time for recovery. It is a background process in…

  • The difference between Cloning and Refreshing

    The difference between Cloning and Refreshing

    The difference between Cloning and Refreshing is that cloning process includes Oracle Home + database Clone; where as…

  • Oracle Histograms

    Oracle Histograms

    Gather stats for column level is called histogram. A histogram is a special type of column statistic that provides more…

    1 条评论
  • Oracle Memory Structure INSIDEs

    Oracle Memory Structure INSIDEs

    ***This is my pick-up for oracle memory. thanks in advance pls share your tips on Oracle Memory Structure insides*** 1.

  • Interview inception @lockdown4 India:

    Interview inception @lockdown4 India:

    - Don't illustrate something nearby, higher up concept to sound like an expert or related to the question Simply say I…

  • Lockdown .0 INDIA & logswitch per hour...cheers

    Lockdown .0 INDIA & logswitch per hour...cheers

    5 log switches per hour (maximum) & 3 Log switches per hour (ideal),recommended by oracle In a production environment…

  • Lockdown 3.0 India & Is it possible to use OLR without OCR in RAC?

    Lockdown 3.0 India & Is it possible to use OLR without OCR in RAC?

    Answer is No, interesting and thought provoking This is introduced in Oracle 11gR2 which will have all the resource…

  • Lockdown in INDIA & unlocking Oracle OCR & OLR meanwhile

    Lockdown in INDIA & unlocking Oracle OCR & OLR meanwhile

    ORACLE LOCAL REGISTRY(OLR) contains node-specific information required by OHASD . Every node has its own dedicated OLR…

  • introduction : compression in oracle

    introduction : compression in oracle

    Apart from reduced space consumption, the compressed data takes less time to go across the network, uses less space for…

社区洞察

其他会员也浏览了