1. Home
  2. Knowledge Base
  3. Oracle 26ai
  4. Oracle Database 23ai Real-Time SQL Plan Management

Oracle Database 23ai Real-Time SQL Plan Management

In an earlier post, we had outlined a new Oracle Database 23ai feature related to SQL Plan Management (which has been back ported to Oracle 19c ) is the new DBMS_SPM function called ADD_VERIFIED_SQL_PLAN_BASELINE.

Another new feature introduced in Oracle Database 23ai is Real-Time SQL Plan Management.

Real-Time SPM repairs the SQL performance regression caused by execution plan changes. Creates SQL plan baselines automatically without any manual intervention.

The database periodically captures SQL statement performance and execution plan data for the SQL executed by the application and these are stored in the automatic SQL tuning set (ASTS).

When a SQL statement is hard parsed, the optimizer references the Automatic SQL Tuning Sets to detect when a plan change has occurred. This happens via a foreground process.

The optimizer compares the performance of the new plan with the previous plan found in the ASTS. Both plans are captured and the best-performing plan becomes an accepted SQL plan baseline automatically.


The DBA_SQL_PLAN_BASELINES view has an additional column FOREGROUND_LAST_VERIFIED which is populated when Real-Time SQL Plan Management kicks in via the foreground process.

Let us look at this new Oracle Database 23ai feature via a simple example.

Create the test table and populate the table with data.

SQL> create table test_tab as select * from dba_objects;

Table created.

SQL> insert into test_tab select * from dba_objects;

72484 rows created.

SQL> insert into test_tab select * from dba_objects;

72484 rows created.

SQL> insert into test_tab select * from dba_objects;

72484 rows created.

SQL> commit;

Commit complete.

Execute the SQL statement: select /* TEST_23AI_SPM */ owner, object_name from test_tab where object_type=’TABLE’ and owner=’SCOTT’;

SQL>  select /* TEST_23AI_SPM */ owner, object_name from test_tab where object_type='TABLE' and owner='SCOTT';

OWNER OBJECT_NAME
------------------------------ ----------------------------------------
SCOTT MYOBJECTS_NEW
SCOTT MYOBJECTS_NEW_2
SCOTT MYOBJECTS_BLOCKCHAIN
...
...
SCOTT TEST_TAB
SCOTT MYOBJECTS_BLOCKCHAIN
SCOTT MYOBJECTS_NEW
SCOTT MYOBJECTS_NEW_2

15 rows selected.

Verify that the execution plan involves a FULL TABLE SCAN of the TEST_TAB table.

SQL> explain plan for
select /* TEST_23AI_SPM */ owner, object_name from test_tab where object_type='TABLE' and owner='SCOTT';

Explained.

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 3962208483

------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 69 | 3726 | 437 (1)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| TEST_TAB | 69 | 3726 | 437 (1)| 00:00:01 |
------------------------------------------------------------------------------

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

PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------------------------------------------------------------------

1 - filter("OBJECT_TYPE"='TABLE' AND "OWNER"='SCOTT')

13 rows selected.

Enable Real-Time SQL Plan Management via DBMS_SPM.CONFIGURE.

Query DBA_SQL_MANAGEMENT_CONFIG and verify that the parameter value is AUTO for the parameter AUTO_SPM_EVOLVE_TASK.

Note that the default value is ON.

SQL> exec dbms_spm.configure('AUTO_SPM_EVOLVE_TASK', 'AUTO')

PL/SQL procedure successfully completed.

SQL> SELECT parameter_value spm_status
FROM dba_sql_management_config
WHERE parameter_name = 'AUTO_SPM_EVOLVE_TASK';

SPM_STATUS
--------------------------------------------------------------------------------
AUTO

By default the task to populate the Automatic SQL Tuning Sets runs every 15 minutes.

For this example we are changing the execution to happen instead every 2 minutes.

Wait two minutes and check task has run successfully.

SQL> exec dbms_auto_task_admin.modify_autotask_setting('Auto STS Capture Task', 'INTERVAL', 120)

PL/SQL procedure successfully completed.


SQL> select task_name, interval,status, last_schedule_time
from dba_autotask_schedule_control
where task_name = 'Auto STS Capture Task';


SQL>

TASK_NAME INTERVAL STATUS LAST_SCHEDULE_TIME
---------------------------------------------------------------- ---------- ---------- --------------------------------
Auto STS Capture Task 120 SUCCEEDED 09-JAN-25 04.14.49.879 AM +00:00

We can see that the ASTS has the SQL statement which we had executed earlier.


SQL> select substr(sql_text,1,100) txt,executions,plan_hash_value
from dba_sqlset_statements
where sqlset_name = 'SYS_AUTO_STS'
and sql_text like '%TEST_23AI_SPM%'
;

TXT EXECUTIONS PLAN_HASH_VALUE
-------------------------------------------------------------------------------- ---------- ---------------
select /* TEST_23AI_SPM */ owner, object_name from test_tab where object_type='T 2 3962208483

We now create an index on the TEST_TAB table and execute the same SQL statement which we had executed earlier (which was performing a FULL TABLE SCAN).

SQL> create index test_tab_ind on test_tab (object_type);

Index created.

SQL> alter system flush shared_pool;

System altered.

SQL> select /* TEST_23AI_SPM */ owner, object_name from test_tab where object_type='TABLE' and owner='SCOTT';

OWNER OBJECT_NAME
------------------------------ ----------------------------------------
SCOTT TEST_TAB
SCOTT MYOBJECTS_BLOCKCHAIN
SCOTT MYOBJECTS_NEW
...
...
SCOTT MYOBJECTS_NEW
SCOTT MYOBJECTS_NEW_2
SCOTT TEST_TAB

15 rows selected.

There is now a new execution plan which involves performing an INDEX RANGE SCAN.

Further, we can now a new SQL Plan Baseline has also been automatically created via the new Real-Time SQL Plan Management foreground process.


SQL> explain plan for
select /* TEST_23AI_SPM */ owner, object_name from test_tab where object_type='TABLE' and owner='SCOTT';

Explained.

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------------------
Plan hash value: 2332153113

----------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 63 | 3402 | 336 (0)| 00:00:01 |
|* 1 | TABLE ACCESS BY INDEX ROWID BATCHED| TEST_TAB | 63 | 3402 | 336 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | TEST_TAB_IND | 6081 | | 20 (0)| 00:00:01 |
----------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------------------------------------
1 - filter("OWNER"='SCOTT')
2 - access("OBJECT_TYPE"='TABLE')

Note
-----
- SQL plan baseline "SQL_PLAN_9pd130pzbjzmua39854f0" used for this statement

19 rows selected.

We can see that DBA_SQL_PLAN_BASELINES shows that there are two plans for the same SQL_HANDLE, but only one of those plans has both ENABLED as well as ACCEPTED values as YES.

SQL> SELECT sql_handle, plan_name, enabled, accepted
FROM dba_sql_plan_baselines
WHERE sql_text like 'select /* TEST_23AI_SPM */ owner, object_name%';

SQL_HANDLE PLAN_NAME ENA ACC
------------------------------ -------------------------------------------------------------------------------------
SQL_9ab423057eb8fe7a SQL_PLAN_9pd130pzbjzmu94cd39d6 YES NO
SQL_9ab423057eb8fe7a SQL_PLAN_9pd130pzbjzmua39854f0 YES YES

To verify Real-Time SQL Management has occurred, we can see that the FOREGROUND_LAST_VERIFIED column has a timestamp value for the plan which has both the ENABLED and ACCEPTED columns with a YES value.

SQL> col plan_name format a60
SQL> select plan_name,foreground_last_verified,enabled, accepted
FROM dba_sql_plan_baselines
WHERE sql_text like 'select /* TEST_23AI_SPM */ owner, object_name%';

PLAN_NAME FOREGROUND_LAST_VERIFIED ENA ACC
------------------------------------------------------------ --------------------------------------------------------------------------- --- ---
SQL_PLAN_9pd130pzbjzmu94cd39d6 YES NO
SQL_PLAN_9pd130pzbjzmua39854f0 09-JAN-25 04.21.13.000000 AM YES YES
Updated on February 10, 2025

Was this article helpful?

Related Articles

Leave a Comment