Skip to content

Commit ffae511

Browse files
authored
Merge pull request oracle-samples#65 from nigelbayliss/master
Using SPM to repair regressed SQL execution plans
2 parents b16e8e7 + 4afaf7b commit ffae511

24 files changed

+1378
-0
lines changed

optimizer/spm_fix/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
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.
2+
3+
SPM will search for historic plans, choose the best one and enforce it with a SQL plan baseline.
4+
5+
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.
6+
7+
Create the user, create the tables and then run "example.sql". It works as follows:
8+
9+
- Tables T1 and T2 have data skew
10+
- Q1 is a query that joins T1 and T2
11+
- Histograms tell the optimizer about the skew so Q1 performs well
12+
- We drop the histograms and this induces a poor plan for Q1
13+
- SPM is initiated and it finds the previous good plan
14+
- The good plan is tested (automatically) by SPM and a SQL plan baseline is created
15+
- Q1 now uses the good plan
16+
17+
```
18+
$ sqlplus / as sysdba [or connect to PDB ADMIN]
19+
SQL> @@user
20+
SQL> connect spmdemo/spmdemo
21+
--
22+
-- Create test tables
23+
--
24+
SQL> @@tab
25+
--
26+
-- Review/execute the following script
27+
--
28+
SQL> @@example
29+
```
30+
31+
Note that AWR is accessed. Check the Oracle Database License Guide for details.
32+
33+
The test creates two tables T1 and T2 - use a test database
34+
35+
DISCLAIMER:
36+
<br/>-- These scripts are provided for educational purposes only.
37+
<br/>-- They are NOT supported by Oracle World Wide Technical Support.
38+
<br/>-- The scripts have been tested and they appear to work as intended.
39+
<br/>-- You should always run scripts on a test instance.
40+
41+

optimizer/spm_fix/drop.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
DECLARE
2+
l_plans_dropped PLS_INTEGER;
3+
BEGIN
4+
5+
FOR REC IN (SELECT DISTINCT SQL_HANDLE FROM DBA_SQL_PLAN_BASELINES )
6+
-- WHERE CREATOR = 'SPMDEMO')
7+
LOOP
8+
L_PLANS_DROPPED := DBMS_SPM.DROP_SQL_PLAN_BASELINE (
9+
sql_handle => rec.sql_handle,
10+
PLAN_NAME => NULL);
11+
END LOOP;
12+
13+
END;
14+
/
15+

optimizer/spm_fix/droph.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
set echo on
2+
exec dbms_stats.delete_column_stats(user,'t1','d',no_invalidate=>false,col_stat_type=>'HISTOGRAM');
3+
exec dbms_stats.delete_column_stats(user,'t2','d',no_invalidate=>false,col_stat_type=>'HISTOGRAM');

0 commit comments

Comments
 (0)