Stages of SQL Processing
Figure 7-3 depicts the general stages of SQL processing: parsing, optimization, row source generation, and execution. Depending on the statement, the database may omit some of these steps.
SQL Parsing
As shown in Figure 7-3, the first stage of SQL processing is parsing. This stage involves separating the pieces of a SQL statement into a data structure that can be processed by other routines. 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 PGA.
During the parse call, the database performs the following checks:
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 (see "Locks and Deadlocks").
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. Thus, 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
.
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. 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:
-
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. The database always perform 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 (see "Latches"). Latch contention increases statement execution time and decreases concurrency.
-
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 the amount of 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.
Figure 7-4 is a simplified representation of a shared pool check of an UPDATE
statement in a dedicated server architecture.
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 case, the environment is the totality of session settings that can affect execution plan generation, such as the work area size or optimizer settings. Consider the following series of SQL statements executed by a single user:
ALTER SYSTEM FLUSH SHARED_POOL; SELECT * FROM my_table; ALTER SESSION SET OPTIMIZER_MODE=FIRST_ROWS; SELECT * FROM my_table; ALTER SESSION SET SQL_TRACE=TRUE; SELECT * FROM my_table;
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
As explained in "Overview of the Optimizer", query optimization is the process of choosing the most efficient means of executing a SQL statement. The database optimizes queries based on statistics collected about the actual data being accessed. The optimizer uses the number of rows, the size of the data set, and other factors to generate possible execution plans, assigning a numeric cost to each plan. The database uses the plan with the lowest cost.
The database must perform a hard parse at least once for every unique DML statement and performs optimization during this parse. DDL is never optimized unless it includes a DML component such as a subquery that requires optimization.
See Also:
Oracle Database Performance Tuning Guide for detailed information about the query optimizerSQL Row Source Generation
The row source generator is software that receives the optimal execution plan from the optimizer and produces an iterative plan, called the query plan, that is usable by the rest of the database. The iterative plan is a binary program that, when executed by the SQL virtual machine, produces the result set.
The query plan takes the form of a combination of steps. Each step returns a row set. The rows in this set are either used by the next step or, in the last step, are returned 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
Example 7-6 shows the execution plan of a SELECT
statement when AUTOTRACE
is enabled. The statement selects the last name, job title, and department name for all employees whose last names begin with the letter A
. The execution plan for this statement is the output of the row source generator.
SELECT e.last_name, j.job_title, d.department_name FROM hr.employees e, hr.departments d, hr.jobs j WHERE e.department_id = d.department_id AND e.job_id = j.job_id AND e.last_name LIKE 'A%' ; 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.
Figure 7-5 is an execution tree, also called a parse tree, that shows the flow of row sources from one step to another. In general, the order of the steps in execution is the reverse of the order in the plan, so you read the plan from the bottom up. Initial spaces in the Operation
column 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.
In Figure 7-5, each node of the tree acts as a row source, which means that each step of the execution plan either retrieves rows from the database or accepts rows from one or more row sources as input. The SQL engine executes each row source as follows:
-
Steps indicated by the black boxes physically retrieve data from an object in the database. These steps are the access paths, or techniques for retrieving data from the database.
-
Step 6 uses a full table scan to retrieve all rows from the
departments
table. -
Step 5 uses a full table scan to retrieve all rows from the
jobs
table. -
Step 4 scans the
emp_name_ix
index in order, looking for each key that begins with the letterA
and retrieving the corresponding rowid (see "Index Range Scan"). For example, the rowid corresponding toAtkinson
isAAAPzRAAFAAAABSAAe
. -
Step 3 retrieves from the
employees
table the rows whose rowids were returned by Step 4. For example, the database uses rowidAAAPzRAAFAAAABSAAe
to retrieve the row forAtkinson
.
-
-
Steps indicated by the clear boxes operate on row sources.
-
Step 2 performs a hash join, accepting row sources from Steps 3 and 5, joining each row from the Step 5 row source to its corresponding row in Step 3, and returning the resulting rows to Step 1.
For example, the row for employee
Atkinson
is associated with the job nameStock Clerk
. -
Step 1 performs another hash join, accepting row sources from Steps 2 and 6, joining each row from the Step 6 source to its corresponding row in Step 2, and returning the result to the client.
For example, the row for employee
Atkinson
is associated with the department namedShipping
.
-
In some execution plans the steps are iterative and in others sequential. The plan shown in Example 7-6 is iterative because the SQL engine moves from index to table to client and then repeats the steps.
During execution, the database reads the data from disk into memory if the data is not in memory. The database also takes out any locks and latches necessary to ensure data integrity and logs any changes made during the SQL execution. The final stage of processing a SQL statement is closing the cursor.
See Also:
Oracle Database Performance Tuning Guide for detailed information about execution plans and theEXPLAIN PLAN
statementHow 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.
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. This mechanism, which uses undo data to show past versions of data, guarantees that all data blocks read by a query are consistent to a single point in time.
For an example of read consistency, 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.
See Also:
"Multiversion Read Consistency"Data Changes
DML statements that must change data use the read consistency mechanism to retrieve only the data that matched the search criteria when the modification began. Afterward, these statements retrieve the data blocks as they exist in their current state and make the required modifications. The database must perform other actions related to the modification of the data such as generating redo and undo data.
See Also:
"Overview of the Online Redo Log"How Oracle Database Processes DDL
Oracle Database processes DDL differently from DML. For example, when you create a table, the database does not optimize the CREATE TABLE
statement. Instead, Oracle Database parses the DDL statement and carries out the command.
The database process DDL differently because it is a means of defining an object in the data dictionary. Typically, Oracle Database must parse and execute many recursive SQL statements to execute a DDL command. Suppose you create a table as follows:
CREATE TABLE mytable (mycolumn INTEGER);
Typically, the database would run dozens of recursive statements to execute the preceding statement. The recursive SQL would perform actions such as the following:
-
Issue a
COMMIT
before executing theCREATE TABLE
statement -
Verify that user privileges are sufficient to create the table
-
Determine which tablespace the table should reside in
-
Ensure that the tablespace quota has not been exceeded
-
Ensure that no object in the schema has the same name
-
Insert rows that define the table into the data dictionary
-
Issue a
COMMIT
if the DDL statement succeeded or aROLLBACK
if it did not
参考至:http://docs.oracle.com/cd/E11882_01/server.112/e40540/sqllangu.htm#CNCPT88923
如有错误,欢迎指正
邮箱:czmcj@163.com
相关推荐
Pro Oracle SQL also helps you minimize parsing overhead, read execution plans, test for correct results, and exert control over SQL execution in your database. You’ll learn when to create indexes, ...
在Oracle数据库管理中,了解SQL语句的执行性能和频率对于系统优化至关重要。本文将详细介绍如何查看Oracle数据库中执行最慢和查询次数最多的SQL语句,以帮助DBA(数据库管理员)识别潜在的性能瓶颈。 首先,我们来...
SELECT PARSING_USER_ID, EXECUTIONS, SORTS, COMMAND_TYPE, DISK_READS, SQL_TEXT FROM v$sqlarea ORDER BY DISK_READS DESC ) WHERE ROWNUM ; ``` 上述查询可以找出磁盘读取次数最多的前10条SQL语句。这有...
### Oracle SQL性能优化知识点 #### 一、基础知识 ##### SQL语句处理过程 1. **创建游标(Create a Cursor)**:这是SQL语句处理的第一步,主要是为了在内存中为SQL语句创建一个存储空间,以便后续步骤可以访问和...
3. **解析次数**:`PARSING_USER_ID` 和 `PARSING_SCHEMA_ID` 分别记录了SQL语句的解析用户和模式,而`COUNT(*)` 列则显示了SQL语句被解析的总次数。 4. **执行信息**:`EXECUTIONS` 列显示SQL语句的执行次数,`...
1. **兼容性广泛**:mo-sql-parsing库可能支持多种SQL方言,包括但不限于标准SQL、MySQL、PostgreSQL、Oracle等,使得它能广泛应用于各种数据库系统。 2. **高性能解析**:9.294.22344版本可能已经过性能优化,能...
数据库监控 MySQL 及 Oracle 数据库监控是指对数据库的运行状态和性能进行监控和优化,以确保...MySQL、Oracle 和 SQL Server 都提供了慢查询监控和统计慢 SQL 的功能,帮助数据库管理员更好地管理和优化数据库。
### Oracle如何查看很耗时的SQL 在Oracle数据库管理中,优化SQL查询性能是一项重要的工作。当遇到系统响应缓慢的情况时,通常需要定位是哪些具体的SQL语句导致了这种现象。本文将详细介绍如何通过Oracle提供的工具...
例如,可以查询V$SQL的SQL_TEXT列获取SQL文本,通过PARSING_SCHEMA_NAME和SQL_ID找出问题SQL的来源。 3. **监控会话性能**:V$SESSION视图提供了关于当前活跃会话的信息,包括用户、操作系统进程、等待事件等。通过...
当一个SQL语句被提交给Oracle时,Oracle会经历一系列复杂的步骤来处理这个请求,主要包括以下阶段: 1. **解析(Parsing)**: 这个阶段主要涉及以下几个步骤: - **语法检查**: 验证SQL语句的语法是否正确。 - **...
select parsing_user_id, executions, sorts, command_type, disk_reads, sql_text from v$sqlarea order by disk_reads desc ) where rownum ; ``` 5. **未提交事务查询** 通过以下查询可以找到阻塞其他...
SELECT PARSING_USER_ID, EXECUTIONS, SORTS, COMMAND_TYPE, DISK_READS, sql_text FROM v$sqlarea ORDER BY disk_reads DESC ) WHERE ROWNUM ; ``` 这段SQL语句将返回按照磁盘读取次数降序排列的前10条SQL语句...
### SQL内部解析(Parsing in Oracle SQL) 在Oracle数据库中,SQL语句的解析是执行过程中的关键步骤之一。当一个SQL语句被提交时,Oracle会首先检查其是否已经在库缓存(Library Cache)中存在一个解析过的版本。...
使用绑定变量可以减少硬解析(Hard Parsing),提高SQL语句的执行效率。绑定变量使得相同或相似的SQL语句能够共享相同的执行计划。 **2.4 合理使用索引** 索引是提高查询性能的有效手段之一。合理地创建和维护索引...
SELECT PARSING_USER_ID, EXECUTIONS, SORTS, COMMAND_TYPE, DISK_READS, sql_text FROM v$sqlarea ORDER BY disk_reads DESC ) WHERE ROWNUM; ``` 对于I/O密集型的会话,下面的查询可以帮助定位: ```sql ...
- SQL/XML:Oracle提供了一个SQL/XML标准的实现,它允许将SQL和XML集成在一起,从而可以使用SQL语句来操作XML数据。 - 索引和搜索:Oracle XML DB允许对XML文档内容建立索引,这提高了查询性能,同时也提供了全文...
SELECT PARSING_USER_ID, EXECUTIONS, SORTS, COMMAND_TYPE, DISK_READS, sql_text FROM v$sqlarea ORDER BY disk_reads DESC ) WHERE ROWNUM ; ``` 这将显示执行次数最多、磁盘读取量最大的前10个SQL。 7. ...
FROM (select PARSING_USER_ID, EXECUTIONS, SORTS, COMMAND_TYPE, DISK_READS, sql_text FROM v$sqlarea ORDER BY disk_reads DESC) WHERE ROWNUM ; ``` 这段SQL用于查找执行次数多且磁盘读取量大的SQL语句,...
1. **解析(Parsing)**:SQL语句被提交至Oracle时,首先会被解析器检查语法是否正确。 2. **绑定(Binding)**:如果语句包含变量,则变量值会被绑定到SQL语句中。 3. **优化(Optimization)**:Oracle优化器会...