Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions optimizer/spm_fix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
This demo shows you how you can use SQL plan management (SPM) to fix a SQL statement that is experiencing a performance regression caused by a sub-optimal plan.

SPM will search for historic plans, choose the best one and enforce it with a SQL plan baseline.

This demonstration is intended for use in Oracle Database 18c onwards. The primary script is "spm.sql", which demonstrates how to use SPM to find and implement a better plan.

Create the user, create the tables and then run "example.sql". It works as follows:

- Tables T1 and T2 have data skew
- Q1 is a query that joins T1 and T2
- Histograms tell the optimizer about the skew so Q1 performs well
- We drop the histograms and this induces a poor plan for Q1
- SPM is initiated and it finds the previous good plan
- The good plan is tested (automatically) by SPM and a SQL plan baseline is created
- Q1 now uses the good plan

```
$ sqlplus / as sysdba [or connect to PDB ADMIN]
SQL> @@user
SQL> connect spmdemo/spmdemo
--
-- Create test tables
--
SQL> @@tab
--
-- Review/execute the following script
--
SQL> @@example
```

Note that AWR is accessed. Check the Oracle Database License Guide for details.

The test creates two tables T1 and T2 - use a test database

DISCLAIMER:
<br/>-- These scripts are provided for educational purposes only.
<br/>-- They are NOT supported by Oracle World Wide Technical Support.
<br/>-- The scripts have been tested and they appear to work as intended.
<br/>-- You should always run scripts on a test instance.


15 changes: 15 additions & 0 deletions optimizer/spm_fix/drop.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
DECLARE
l_plans_dropped PLS_INTEGER;
BEGIN

FOR REC IN (SELECT DISTINCT SQL_HANDLE FROM DBA_SQL_PLAN_BASELINES )
-- WHERE CREATOR = 'SPMDEMO')
LOOP
L_PLANS_DROPPED := DBMS_SPM.DROP_SQL_PLAN_BASELINE (
sql_handle => rec.sql_handle,
PLAN_NAME => NULL);
END LOOP;

END;
/

3 changes: 3 additions & 0 deletions optimizer/spm_fix/droph.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set echo on
exec dbms_stats.delete_column_stats(user,'t1','d',no_invalidate=>false,col_stat_type=>'HISTOGRAM');
exec dbms_stats.delete_column_stats(user,'t2','d',no_invalidate=>false,col_stat_type=>'HISTOGRAM');
Loading