1. In a SQL*Plus session run the non-hinted SQL statement to begin the SQL plan baseline capture
SQL> variable b1 varchar2(30)
SQL> exec :b1 := 'Software/Other';
PL/SQL procedure successfully completed.
SQL>
SQL> set autotrace on explain
SQL> select prod_name, sum(amount_sold)
2 from sh.sales s, sh.products p
3 where s.prod_id=p.prod_id
4 and prod_category = :b1
5 group by prod_name;
PROD_NAME SUM(AMOUNT_SOLD)
-------------------------------------------------- ----------------
OraMusic CD-R, Pack of 10 711741.8
CD-R with Jewel Cases, pACK OF 12 170405.76
O/S Documentation Set - French 590575.79
O/S Documentation Set - Italian 296648.58
Keyboard Wrist Rest 348408.98
CD-R Mini Discs 384553.62
3 1/2" Bulk diskettes, Box of 100 370204.56
External 101-key keyboard 456568.7
CD-RW, High Speed Pack of 5 230233.35
DVD-R Disc with Jewel Case, 4.7 GB 577420.62
DVD-RW Discs, 4.7GB, Pack of 3 312036.54
CD-R, Professional Grade, Pack of 10 170270.13
Unix/Windows 1-user pack 3543725.89
Mouse Pad 306708.12
Laptop carrying case 623881.12
O/S Documentation Set - English 1314478.74
O/S Documentation Set - Kanji 509073.63
Music CD-R 301848.2
CD-RW, High Speed, Pack of 10 106468.41
DVD-R Discs, 4.7GB, Pack of 5 904537.2
1.44MB External 3.5" Diskette 219618.47
O/S Documentation Set - German 604081.91
O/S Documentation Set - Spanish 406840.96
3 1/2" Bulk diskettes, Box of 50 254128.79
DVD-RAM Jewel Case, Double-Sided, 9.4G 120395.81
25 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 3584564314
------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 784 | 527 (3)| 00:00:01 | | |
| 1 | HASH GROUP BY | | 14 | 784 | 527 (3)| 00:00:01 | | |
|* 2 | HASH JOIN | | 183K| 9M| 522 (2)| 00:00:01 | | |
| 3 | TABLE ACCESS BY INDEX ROWID BATCHED| PRODUCTS | 14 | 658 | 3 (0)| 00:00:01 | | |
|* 4 | INDEX RANGE SCAN | PRODUCTS_PROD_CAT_IX | 14 | | 1 (0)| 00:00:01 | | |
| 5 | PARTITION RANGE ALL | | 918K| 8075K| 517 (2)| 00:00:01 | 1 | 28 |
| 6 | TABLE ACCESS FULL | SALES | 918K| 8075K| 517 (2)| 00:00:01 | 1 | 28 |
------------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("S"."PROD_ID"="P"."PROD_ID")
4 - access("PROD_CATEGORY"=:B1)
Note
-----
- this is an adaptive plan
2. Find SQL_ID for the statement in V$SQL
SQL> set autotrace off
SQL> select SQL_ID, SQL_FULLTEXT from v$SQL where upper(SQL_TEXT) like '%SELECT PROD_NAME, SUM(%';
SQL_ID SQL_FULLTEXT
------------- --------------------------------------------------------------------------------
gkkbdz8hqd17n select prod_name, sum(amount_sold)
from sh.sales s, sh.products p
where s.prod_i
ayv56nq2qp4qq select SQL_ID, SQL_FULLTEXT from v$SQL where upper(SQL_TEXT) like '%SELECT PROD_
85dbpayqtyhns EXPLAIN PLAN SET STATEMENT_ID='PLUS1185966' FOR select prod_name, sum(amount_sol
3. Create a SQL plan baseline for the statement
SQL> variable cnt number;
SQL> execute :cnt :=dbms_spm.load_plans_from_cursor_cache(sql_id=>'gkkbdz8hqd17n');
PL/SQL procedure successfully completed.
4. The plan that was captured is incorrect and will need to be disabled. The SQL_HANDLE & PLAN_NAME are required. These can found by looking in DBA_SQL_PLAN_BASELINE and using DBMS_SPM.ALTER_SQL_PLAN_BASELINE to disable the bad plan
SQL> select sql_handle, sql_text, plan_name, enabled from dba_sql_plan_baselines
2 where created > sysdate - 1/ 24;^K
SQL_HANDLE
--------------------------------------------------------------------------------------------------------------------------------
SQL_TEXT
----------------------------------------------------------------
PLAN_NAME
--------------------------------------------------------------------------------------------------------------------------------
ENA
---
SQL_48206f252e8ac92a
select prod_name, sum(amount_sold)
from sh.sales s, sh.products p
where s.prod_i
SQL_PLAN_4h83g4nr8pk9a6ff7b491
YES
SQL> variable cnt number;
SQL> begin
2 :cnt := DBMS_SPM.ALTER_SQL_PLAN_BASELINE(
3 SQL_HANDLE=>'SQL_48206f252e8ac92a',
4 PLAN_NAME=>'SQL_PLAN_4h83g4nr8pk9a6ff7b491',
5 ATTRIBUTE_NAME=>'enabled',
6 ATTRIBUTE_VALUE=>'NO');
7 end;
8 /
PL/SQL procedure successfully completed.
5. Modify the SQL statement using hints & execute it
SQL> set autotrace on explain
SQL> select /*+ full(p) */ prod_name, sum(amount_sold)
2 from sh.sales s, sh.products p
3 where s.prod_id=p.prod_id
4 and prod_category = :b1
5 group by prod_name;
PROD_NAME SUM(AMOUNT_SOLD)
-------------------------------------------------- ----------------
OraMusic CD-R, Pack of 10 711741.8
CD-R with Jewel Cases, pACK OF 12 170405.76
O/S Documentation Set - French 590575.79
O/S Documentation Set - Italian 296648.58
Keyboard Wrist Rest 348408.98
CD-R Mini Discs 384553.62
3 1/2" Bulk diskettes, Box of 100 370204.56
External 101-key keyboard 456568.7
CD-RW, High Speed Pack of 5 230233.35
DVD-R Disc with Jewel Case, 4.7 GB 577420.62
DVD-RW Discs, 4.7GB, Pack of 3 312036.54
CD-R, Professional Grade, Pack of 10 170270.13
Unix/Windows 1-user pack 3543725.89
Mouse Pad 306708.12
Laptop carrying case 623881.12
O/S Documentation Set - English 1314478.74
O/S Documentation Set - Kanji 509073.63
Music CD-R 301848.2
CD-RW, High Speed, Pack of 10 106468.41
DVD-R Discs, 4.7GB, Pack of 5 904537.2
1.44MB External 3.5" Diskette 219618.47
O/S Documentation Set - German 604081.91
O/S Documentation Set - Spanish 406840.96
3 1/2" Bulk diskettes, Box of 50 254128.79
DVD-RAM Jewel Case, Double-Sided, 9.4G 120395.81
25 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 3535171836
--------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
--------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 784 | 527 (3)| 00:00:01 | | |
| 1 | HASH GROUP BY | | 14 | 784 | 527 (3)| 00:00:01 | | |
|* 2 | HASH JOIN | | 183K| 9M| 522 (2)| 00:00:01 | | |
|* 3 | TABLE ACCESS FULL | PRODUCTS | 14 | 658 | 3 (0)| 00:00:01 | | |
| 4 | PARTITION RANGE ALL| | 918K| 8075K| 517 (2)| 00:00:01 | 1 | 28 |
| 5 | TABLE ACCESS FULL | SALES | 918K| 8075K| 517 (2)| 00:00:01 | 1 | 28 |
--------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("S"."PROD_ID"="P"."PROD_ID")
3 - filter("PROD_CATEGORY"=:B1)
Note
-----
- this is an adaptive plan
6 Find new SQL_ID and PLAN_HASH_VALUE for the hinted SQL stmt
SQL> set autotrace off
SQL> select SQL_ID, PLAN_HASH_VALUE, SQL_FULLTEXT from v$SQL where upper(SQL_TEXT) like '%SELECT%FULL% PROD_NAME, SUM(%';
SQL_ID PLAN_HASH_VALUE SQL_FULLTEXT
------------- --------------- --------------------------------------------------------------------------------
585hmwunrt2q7 3535171836 EXPLAIN PLAN SET STATEMENT_ID='PLUS1185966' FOR select /*+ full(p) */ prod_name,
ayv56nq2qp4qq 903671040 select SQL_ID, SQL_FULLTEXT from v$SQL where upper(SQL_TEXT) like '%SELECT PROD_
986n7n6b096cp 903671040 select SQL_ID, PLAN_HASH_VALUE, SQL_FULLTEXT from v$SQL where upper(SQL_TEXT) li
cqudpw3gaxduu 3535171836 select /*+ full(p) */ prod_name, sum(amount_sold)
from sh.sales s, sh.products p
gp6pddrxu30mn 3584564314 EXPLAIN PLAN SET STATEMENT_ID='PLUS1185966' FOR select /* + full(p) */ prod_name
6 rows selected.
7. Create new accepted plan for original SQL stmt by associating the modified plan to the original statement's SQL HANDLE
SQL> begin
2 :cnt:= dbms_spm.LOAD_PLANS_FROM_CURSOR_CACHE(
3 SQL_ID=>'cqudpw3gaxduu',
4 PLAN_HASH_VALUE=>3535171836,
5 SQL_HANDLE=>'SQL_48206f252e8ac92a');
6 end;
7 /
PL/SQL procedure successfully completed.
SQL> select sql_handle, sql_text, plan_name, enabled from dba_sql_plan_baselines
2 where created > sysdate - 1/ 24;
SQL_HANDLE
--------------------------------------------------------------------------------------------------------------------------------
SQL_TEXT
----------------------------------------------------------------
PLAN_NAME
--------------------------------------------------------------------------------------------------------------------------------
ENA
---
SQL_48206f252e8ac92a
select prod_name, sum(amount_sold)
from sh.sales s, sh.products p
where s.prod_i
SQL_PLAN_4h83g4nr8pk9a42949306
YES
SQL_48206f252e8ac92a
select prod_name, sum(amount_sold)
from sh.sales s, sh.products p
where s.prod_i
SQL_PLAN_4h83g4nr8pk9a6ff7b491
NO
Hope this helps. Thanks for Nigel Bayliss (Optimizer Product Manager) for his assistance with this.