| Lesson 4 | Executing Oracle SQL |
| Objective | Explain the process of SQL execution in Oracle AI Database 26ai. |
Executing SQL involves more than translating a statement into a plan. Oracle must understand the request, determine whether existing executable code can be reused, and perform the work needed to produce a result or change data. For a query, the application also fetches rows. Understanding these stages helps explain why a slow request might involve excessive parsing, an inefficient plan, or substantial work during fetching.
The previous lesson introduced SQL tuning tools that expose plans and runtime activity. This lesson explains the processing behind those measurements. The central distinction is between preparing a statement and executing it: a repeated execution does not necessarily require a new parse, and a parse does not necessarily require a new execution plan.
This diagram summarizes a SELECT preparation and execution path. The shared-pool check belongs to parsing, even though it appears as a separate box for clarity. An application that retains a valid prepared cursor can execute it again without requesting another parse. The diagram does not require every repeated query to begin at the top, and it does not describe every detail of DDL or transaction processing.
An application submits SQL through a database interface. A parse call opens or creates a cursor associated with statement-processing state. A cursor is a handle used to manage processing; it is not simply another name for the rows returned by a query.
During preparation, Oracle checks the statement's syntax and meaning and the access required to use its objects. A misspelled SQL keyword can fail a syntax check. A statement with valid grammar can still fail because a referenced table or column does not exist or the user cannot access the object. Resolving names and checking the relevant metadata are part of understanding what the statement means.
A parsing error returns to the application instead of leading to ordinary plan generation and execution. Successful parsing is not a guarantee of successful execution, however. Data conversion problems or other runtime conditions can still cause a statement to fail later.
The library cache is part of the shared pool. Oracle uses SQL lookup information to find existing statements that might be reusable. A hash-based lookup narrows the search, but a matching hash alone does not establish that the executable code can be shared. Oracle must consider the statement's meaning, resolved objects, environment, and other compatibility requirements.
For example, the same unqualified table name can resolve to different objects for different users. Identical SQL text therefore does not by itself prove identical meaning. Similarly, differences in the optimizer environment or bind metadata can prevent reuse of a particular child cursor.
SQL_ID identifies SQL text for lookup and diagnosis. It is distinct from a statement's memory address and from a plan hash value. Do not interpret one SQL_ID as a guarantee that only one execution plan exists.
| Situation | Processing consequence |
|---|---|
| Reexecute a valid prepared cursor | The application can avoid requesting a new parse, subject to the interface and cursor validity. |
| Parse and find reusable executable code | A soft parse avoids the optimization and row source generation needed for a hard parse. |
| Parse without reusable executable code | A hard parse builds new executable code. |
A soft parse still performs work. Reducing unnecessary parse calls can therefore be useful even when those calls would otherwise be soft parses. Conversely, reducing parsing overhead does not repair an execution plan that does too much work for the workload.
For a SELECT that requires a hard parse, the cost-based optimizer evaluates ways to obtain the requested result. It uses object metadata, available statistics, and the optimizer environment when considering access paths and operations such as joins, filtering, and sorting. The optimizer's cost is an estimate used to compare alternatives, not a measured duration in seconds.
Row source generation turns the selected plan into executable processing steps. A row source can retrieve rows from an object or consume rows produced by another operation. Those operations work together to implement the query. An index scan and a table scan are possible access choices; neither is automatically best for every query.
The resulting executable code can be reused when compatibility and validity allow it. Object changes, invalidation, or absence from the cache can make later preparation necessary. A stored plan is not a permanent promise that all future requests will avoid hard parsing.
Execution starts the work represented by the cursor. For a query, subsequent fetch calls request rows from its result. Applications commonly fetch batches rather than requesting one row per network round trip. Work can continue as rows are fetched, so the completion of an execute call does not necessarily mean that Oracle has already produced the entire result.
Some plans can return initial rows quickly and continue producing more as the application requests them. Other operations may need substantial input before returning their first row. A test that displays only the first page of a result is therefore different from one that fetches every row.
Choose measurements that match the application's goal. An interactive search may care about the first useful rows, while a batch report may need the entire result. Compare the same amount of work when evaluating a change. Close or release the cursor when it is no longer needed, following the client interface's lifecycle rules. Applications may deliberately retain prepared statements for subsequent executions.
INSERT, UPDATE, and DELETE change data rather than following the ordinary SELECT result-fetch path. Executing such a statement, or closing its cursor, is not the same as committing the transaction. COMMIT and ROLLBACK control transaction completion; a client's autocommit setting can cause additional commit behavior. Keep that setting separate from the database's parse and execute stages.
DDL also has processing and transaction behavior that differs from this simplified SELECT lifecycle. Do not apply every arrow in the diagram to every SQL statement category.
Consider an illustrative CUSTOMER table with a LAST_NAME column. This table must exist in your practice schema; it is not assumed to be an Oracle-supplied sample object. These examples differ only in their literal value:
SELECT last_name FROM customer WHERE last_name = 'Coulomb';
SELECT last_name FROM customer WHERE last_name = 'Gauss';
SELECT last_name FROM customer WHERE last_name = 'Tesla';
With the default CURSOR_SHARING=EXACT behavior, these are different statement texts. Their first use may require distinct hard parses when suitable code is unavailable. Later repetitions can reuse compatible cached code. The three texts do not create three separate server processes merely because the literals differ.
A bind variable lets an application keep the SQL text stable while supplying a different data value. The following SQL*Plus example declares a bind and executes the same query text twice:
VARIABLE p_last_name VARCHAR2(100)
EXECUTE :p_last_name := 'Coulomb';
SELECT last_name
FROM customer
WHERE last_name = :p_last_name;
EXECUTE :p_last_name := 'Gauss';
SELECT last_name
FROM customer
WHERE last_name = :p_last_name;
VARIABLE and EXECUTE here are SQL*Plus commands. In an application, use the database driver's parameter-binding or prepared-statement API. Choose a bind datatype appropriate to the column. The database receives the value separately from the query text.
The placeholder is :p_last_name, without quotation marks. Writing ':p_last_name' makes it a string literal containing those characters. Bind variables supply data values, not arbitrary SQL syntax or table names. The examples illustrate the syntax; they do not claim measured performance results for your database.
SELECT * FROM Customer;
SELECT * FROM customer;
For unquoted identifiers in the same naming context, these statements can refer to the same object and return the same data. Their SQL text nevertheless differs in case. Under exact matching, differences in case, spaces, or comments can prevent parent-cursor sharing. Quoted identifiers have their own case-sensitive naming rules.
Use consistent SQL text rather than assuming that semantically equivalent statements will share a cursor. Also avoid the opposite assumption: text equality is not sufficient to guarantee compatibility with a particular child cursor.
A parent cursor represents the statement text; child cursors hold executable versions for particular compatibility conditions. Differences in bind metadata, environment, or other requirements can lead to multiple children. Adaptive cursor sharing can also allow a bind-using statement to use different plans when different value ranges benefit from different access choices.
For example, a search value matching a few rows may favor different work from a value matching a large fraction of the table. Using binds supports sharing, but does not guarantee one plan or faster execution for every value. Evaluate representative values and complete workload behavior.
Stored procedures can centralize SQL and encourage consistent calls. They do not automatically fix inefficient statements or guarantee cursor reuse. Prefer well-designed parameterized SQL and appropriate prepared-statement reuse. Changing CURSOR_SHARING to FORCE or flushing the shared pool is not a routine first step in this introductory workflow.
When investigating a delay, first ask which stage needs attention. Repeated preparation suggests examining parse activity and statement reuse. Excessive database work calls for examining the executed plan and available runtime statistics. Fetch behavior matters when a client retrieves many rows or measures only a partial result.
The tools from Lesson 3 help separate these questions. SQL Trace and TKPROF expose parse, execute, and fetch activity. DBMS_XPLAN.DISPLAY_CURSOR can display an executed cursor's plan and collected statistics. EXPLAIN PLAN instead describes an estimated plan without executing the target query.
The next lesson examines the EXPLAIN PLAN statement and how to interpret its output. Keep the difference between estimates and measured execution in mind when using it.