CREATE TABLE table_exec_plan_1
(
id1 NUMBER(5,0) NOT NULL
, id2 NUMBER(5,0) NOT NULL
, type1 NUMBER(1,0) NOT NULL
);
The following PL/SQL block populates the table with 50,000 rows, alternating the type1 column between values 1 and 2:
DECLARE
indexLoop NUMBER(5,0);
BEGIN
FOR indexLoop IN 1..50000 LOOP
IF MOD(indexLoop,2) = 0 THEN
INSERT INTO table_exec_plan_1 (id1, id2, type1) VALUES (indexLoop, DBMS_RANDOM.VALUE(1,50000), 1);
ELSE
INSERT INTO table_exec_plan_1 (id1, id2, type1) VALUES (indexLoop, DBMS_RANDOM.VALUE(1,50000), 2);
END IF;
END LOOP;
END;
/
COMMIT;
An index is created on the type1 column to demonstrate index usage in the execution plans:CREATE INDEX ix_table_exec_plan_1_type1 ON table_exec_plan_1 (type1) COMPUTE STATISTICS;The COMPUTE STATISTICS clause ensures that Oracle collects index statistics immediately, which helps the optimizer make better decisions about index usage.
dbms_xplan.display_cursor function displays actual execution plans for queries that have been executed and cached in the shared pool.
This function requires that runtime statistics were collected during query execution.SELECT /*+ gather_plan_statistics */ /* ora_sql_hint_exec_plan */ * FROM table_exec_plan_1 WHERE type1 = 1;The gather_plan_statistics hint instructs Oracle to collect detailed runtime statistics during query execution. The comment with ora_sql_hint_exec_plan serves as a unique identifier to locate this specific query in the shared pool.
v$sql view to find the SQL_ID and child number:select sql_id, child_number, sql_text from v$sql where sql_text like '%ora_sql_hint_exec_plan%';Query result:
+---------------+--------------+---------------------------------------------------------------------------------------------------------------+ | SQL_ID | CHILD_NUMBER | SQL_TEXT | +---------------+--------------+---------------------------------------------------------------------------------------------------------------+ | aqwka0b74ph07 | 0 | SELECT /*+ gather_plan_statistics */ /* ora_sql_hint_exec_plan */ * FROM table_exec_plan_1 WHERE type1 = 1 | +---------------+--------------+---------------------------------------------------------------------------------------------------------------+
SELECT * FROM table(dbms_xplan.display_cursor('__SQL_ID__', __CHILD_NUMBER__, 'ALLSTATS LAST'));
Replace "__SQL_ID__" with the correct value (in this example: "aqwka0b74ph07") and "__CHILD_NUMBER__" with the correct value (in this example: "0").
SQL_ID aqwka0b74ph07, child number 0
-------------------------------------
SELECT /*+ gather_plan_statistics */ /* ora_sql_hint_exec_plan */ * FROM table_exec_plan_1 WHERE type1 = 1
Plan hash value: 10875406
-----------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads |
-----------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 50 |00:00:00.49 | 3 | 4 |
| 1 | TABLE ACCESS BY INDEX ROWID| TABLE_EXEC_PLAN_1 | 1 | 24687 | 50 |00:00:00.49 | 3 | 4 |
|* 2 | INDEX RANGE SCAN | IX_TABLE_EXEC_PLAN_1_TYPE1 | 1 | 198 | 50 |00:00:00.01 | 2 | 4 |
-----------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("TYPE1"=1)
Note
-----
- dynamic sampling used for this statement (level=2)
The Predicate Information section shows the filter and access conditions applied at each step.----------------------------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads | ----------------------------------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | | 50 |00:00:00.49 | 3 | 4 | | 1 | TABLE ACCESS BY INDEX ROWID| TABLE_EXEC_PLAN_1 | 1 | 24687 | 50 |00:00:00.49 | 3 | 4 | |* 2 | INDEX RANGE SCAN | IX_TABLE_EXEC_PLAN_1_TYPE1 | 1 | 198 | 50 |00:00:00.01 | 2 | 4 | -----------------------------------------------------------------------------------------------------------------------------