OPTIMIZER USES SQL PROFILE WITH BAD PLAN EVEN THOUGH GOOD PLAN IS AVAILABLE
Optimizer even though had goof statistics, still chosen bad plan because of the profile.
SQL> EXEC dbms_stats.gather_database_stats();
PL/SQL procedure successfully completed.
SQL> SELECT name,line from dspm WHERE type=’PROCEDURE’ and owner =’MDSYS’;
45 rows selected.
Execution Plan
———————————————————-
Plan hash value: 3837617297
————————————————————————–
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
————————————————————————–
| 0 | SELECT STATEMENT | | 2515 | 85510 | 1570 (1)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| DSPM | 2515 | 85510 | 1570 (1)| 00:00:01 |
————————————————————————–
Predicate Information (identified by operation id):
—————————————————
1 – filter(“TYPE”=’PROCEDURE’ AND “OWNER”=’MDSYS’)
Note
—–
– SQL profile “coe_3bjw18gb6jthj_3837617297” used for this statement
The only way to influence the plan is to use index hints to go for index scans
SQL> set lines 200 pages 1000
SQL> SELECT /*+INDEX(dspm dtype)*/ name,line from dspm WHERE type=’PROCEDURE’ and owner =’MDSYS’;
45 rows selected.
Execution Plan
———————————————————-
Plan hash value: 2382681726
———————————————————————————————
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
———————————————————————————————
| 0 | SELECT STATEMENT | | 45 | 1530 | 405 (0)| 00:00:01 |
|* 1 | TABLE ACCESS BY INDEX ROWID BATCHED| DSPM | 45 | 1530 | 405 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | DTYPE | 15536 | | 44 (0)| 00:00:01 |
———————————————————————————————
Predicate Information (identified by operation id):
—————————————————
1 – filter(“OWNER”=’MDSYS’)
2 – access(“TYPE”=’PROCEDURE’)
Note
—–
– dynamic statistics used: dynamic sampling (level=AUTO)
SQL> SELECT sql_id,elapsed_time_total,plan_hash_value,sql_profile FROM dba_hist_sqlstat WHERE plan_hash_value in (‘3837617297′,’2382681726’) order by 2 desc;
SQL_ID ELAPSED_TIME_TOTAL PLAN_HASH_VALUE SQL_PROFILE
————- —————— ————— —————————————————————-
8x2s277rk1b0a 651207 3837617297
8x2s277rk1b0a 332166 3837617297
6bx1tjnfah91k 270681 3837617297
6bx1tjnfah91k 243248 3837617297
3bjw18gb6jthj 154033 3837617297 coe_3bjw18gb6jthj_3837617297
6bx1tjnfah91k 92445 2382681726
3bjw18gb6jthj 82910 3837617297
3t9upwfb4s9pp 45258 3837617297 coe_d46sq1upy11vr_3837617297
3t9upwfb4s9pp 41735 3837617297
3t9upwfb4s9pp 36449 3837617297
57f1y1z9zyt9k 29769 3837617297
d46sq1upy11vr 28266 3837617297 coe_d46sq1upy11vr_3837617297
d46sq1upy11vr 21207 3837617297
b6kqbg298h1j1 17603 3837617297
3t9upwfb4s9pp 17378 3837617297 coe_d46sq1upy11vr_3837617297
3gjtxtu6r5q93 8791 3837617297
b6kqbg298h1j1 6257 2382681726
17 rows selected.
Finally, dynamic sampling with level 11 also helped to use the good plan.
***************************************
BASE STATISTICAL INFORMATION
***********************
Table Stats::
Table: DSPM Alias: DSPM
#Rows: 291053 SSZ: 0 LGR: 0 #Blks: 5778 AvgRowLen: 114.00 NEB: 0 ChainCnt: 0.00 ScanRate: 0.00 SPC: 0 RFL: 0 RNF: 0 CBK: 0 CHR: 0 KQDFLG: 1
#IMCUs: 0 IMCRowCnt: 0 IMCJournalRowCnt: 0 #IMCBlocks: 0 IMCQuotient: 0.000000
Index Stats::
Index: DTYPE Col#: 3
LVLS: 2 #LB: 786 #DK: 9 LB/K: 87.00 DB/K: 750.00 CLUF: 6754.00 NRW: 291053.00 SSZ: 0.00 LGR: 0.00 CBK: 0.00 GQL: 0.00 CHR: 0.00 KQDFLG: 8192 BSZ: 1
KKEISFLG: 1
try to generate single-table filter predicates from ORs for query block SEL$1 (#0)
finally: "DSPM"."TYPE"='PROCEDURE' AND "DSPM"."OWNER"='MDSYS'
SQL> set lines 200 pages 1000
SQL> SELECT /*+ dynamic_sampling(dspm 11) */ name,line from dspm WHERE type='PROCEDURE' and owner ='MDSYS';
Execution Plan
----------------------------------------------------------
Plan hash value: 2382681726
---------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 45 | 6885 | 28 (0)| 00:00:01 |
|* 1 | TABLE ACCESS BY INDEX ROWID BATCHED| DSPM | 45 | 6885 | 28 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | DTYPE | 15536 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OWNER"='MDSYS')
2 - access("TYPE"='PROCEDURE')
Hint Report (identified by operation id / Query Block Name / Object Alias):
Total hints for statement: 1 (U - Unused (1))
---------------------------------------------------------------------------
1 - SEL$1 / DSPM@SEL$1
U - dynamic_sampling(dspm 11)
Note
-----
- dynamic statistics used: dynamic sampling (level=AUTO)
The contents or outline hints of the sql profile can be further scrapped from dba_sqltune_plans view.
SQL> set long 1000000000
SQL> SELECT other_xml FROM dba_sqltune_plans WHERE plan_hash_value=3837617297;
OTHER_XML
--------------------------------------------------------------------------------
<other_xml><info type="has_user_tab">yes</info><info type="db_version">19.0.0.0<
/info><info type="parse_schema"><![CDATA["SYS"]]></info><info type="dynamic_samp
ling" note="y">2</info><info type="plan_hash_full">3029714510</info><info type="
plan_hash">3837617297</info><info type="plan_hash_2">3029714510</info><stats typ
e="compilation"><stat name="bg">2032</stat></stats><qb_registry><q o="2" f="y"><
n><![CDATA[SEL$1]]></n><f><h><t><![CDATA[DSPM]]></t><s><![CDATA[SEL$1]]></s></h>
</f></q></qb_registry><outline_data><hint><![CDATA[FULL(@"SEL$1" "DSPM"@"SEL$1")
]]></hint><hint><![CDATA[OUTLINE_LEAF(@"SEL$1")]]></hint><hint><![CDATA[ALL_ROWS
]]></hint><hint><![CDATA[OPT_PARAM('result_cache_mode' 'FORCE')]]></hint><hint><
![CDATA[DB_VERSION('19.1.0')]]></hint><hint><![CDATA[OPTIMIZER_FEATURES_ENABLE('
19.1.0')]]></hint><hint><![CDATA[IGNORE_OPTIM_EMBEDDED_HINTS]]></hint></outline_
data></other_xml>
<other_xml><info type="has_user_tab">yes</info><info type="db_version">19.0.0.0<
/info><info type="parse_schema"><![CDATA["SYS"]]></info><info type="dynamic_samp
ling" note="y">2</info><info type="plan_hash_full">3029714510</info><info type="
plan_hash">3837617297</info><info type="plan_hash_2">3029714510</info><info type
="baseline_repro_fail" note="y">yes</info><stats type="compilation"><stat name="
bg">11509</stat></stats><qb_registry><q o="2" f="y"><n><![CDATA[SEL$1]]></n><f><
h><t><![CDATA[DSPM]]></t><s><![CDATA[SEL$1]]></s></h></f></q></qb_registry><outl
ine_data><hint><![CDATA[FULL(@"SEL$1" "DSPM"@"SEL$1")]]></hint><hint><![CDATA[OU
TLINE_LEAF(@"SEL$1")]]></hint><hint><![CDATA[ALL_ROWS]]></hint><hint><![CDATA[DB
_VERSION('19.1.0')]]></hint><hint><![CDATA[OPTIMIZER_FEATURES_ENABLE('19.1.0')]]
></hint><hint><![CDATA[IGNORE_OPTIM_EMBEDDED_HINTS]]></hint></outline_data></oth
er_xml>
<other_xml><info type="has_user_tab">yes</info><info type="db_version">19.0.0.0<
/info><info type="parse_schema"><![CDATA["SYS"]]></info><info type="dynamic_samp
ling" note="y">2</info><info type="plan_hash_full">3029714510</info><info type="
plan_hash">3837617297</info><info type="plan_hash_2">3029714510</info><stats typ
e="compilation"><stat name="bg">68</stat></stats><qb_registry><q o="2" f="y"><n>
<![CDATA[SEL$1]]></n><f><h><t><![CDATA[DSPM]]></t><s><![CDATA[SEL$1]]></s></h></
f></q></qb_registry><outline_data><hint><![CDATA[FULL(@"SEL$1" "DSPM"@"SEL$1")]]
></hint><hint><![CDATA[OUTLINE_LEAF(@"SEL$1")]]></hint><hint><![CDATA[ALL_ROWS]]
></hint><hint><![CDATA[DB_VERSION('19.1.0')]]></hint><hint><![CDATA[OPTIMIZER_FE
ATURES_ENABLE('19.1.0')]]></hint><hint><![CDATA[IGNORE_OPTIM_EMBEDDED_HINTS]]></
hint></outline_data><hint_usage><q><n><![CDATA[SEL$1]]></n><t><f><![CDATA["DSPM"
@"SEL$1"]]></f><h o="OU"><x><![CDATA[FULL(@"SEL$1" "DSPM"@"SEL$1")]]></x></h></t
></q><s><h o="OU"><x><![CDATA[ALL_ROWS]]></x></h><h o="OU"><x><![CDATA[DB_VERSIO
N('19.1.0')]]></x></h><h o="OU"><x><![CDATA[OPTIMIZER_FEATURES_ENABLE('19.1.0')]
]></x></h><h o="OU"><x><![CDATA[IGNORE_OPTIM_EMBEDDED_HINTS]]></x></h></s></hint
_usage></other_xml>
6 rows selected.