Oracle11g sql plan management

4,241 views
4,261 views

Published on

Oracle11g sql plan management

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,241
On SlideShare
0
From Embeds
0
Number of Embeds
2,212
Actions
Shares
0
Downloads
116
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • SQL plan management is a preventative mechanism that records and evaluates the execution plans of SQL statements over time. This mechanism can build a SQL plan baseline, which is a set of accepted plans for a SQL statement. The accepted plans have been proven to perform well.The SQL management base (SMB) is a part of the data dictionary that resides in the SYSAUX tablespace. It stores statement logs, plan histories, SQL plan baselines, and SQL profiles.SQL plan history is the set of plans, both accepted and not accepted, that the optimizer generates for a SQL statement over time. Because only accepted plans are in the SQL plan baseline, the plans in the baseline form a subset of the plan history. For example, after the optimizer generates the first acceptable plan for a SQL plan baseline, subsequent plans are part of the plan history but not part of the plan baseline.A SQL plan baseline contains one or more accepted plans, each of which contains the following information:-Set of hints-Plan hash value-Plan-related informationThe process of adding plans to a SQL plan baseline is plan evolution.
  • SQL> l 1 select ksppinm name, ksppstvl value, ksppdesc description 2 from x$ksppi x, x$ksppcv y 3 where (x.indx = y.indx) 4 and ( ksppinm like '%sql_plan%' or ksppdesc like '%sql_plan%' ) 5* order by nameSQL> /NAME VALUE DESCRIPTION---------------------------------------- ----- ----------------------------------------------------------------------_sql_plan_management_control 0 controls various internal SQL Plan Management algorithmsoptimizer_capture_sql_plan_baselines FALSE automatic capture of SQL plan baselines for repeatable statementsoptimizer_use_sql_plan_baselines TRUE use of SQL plan baselines for captured sql statementsSQL> descdba_sql_plan_baselines Name Null? Type ------------------------------------------------------------------------ -------- ------------------------------------------------- SIGNATURE NOT NULL NUMBER SQL_HANDLE NOT NULL VARCHAR2(30) SQL_TEXT NOT NULL CLOB PLAN_NAME NOT NULL VARCHAR2(30) CREATOR VARCHAR2(30) ORIGIN VARCHAR2(14) PARSING_SCHEMA_NAME VARCHAR2(30) DESCRIPTION VARCHAR2(500) VERSION VARCHAR2(64) CREATED NOT NULL TIMESTAMP(6) LAST_MODIFIED TIMESTAMP(6) LAST_EXECUTED TIMESTAMP(6) LAST_VERIFIED TIMESTAMP(6) ENABLED VARCHAR2(3) ACCEPTED VARCHAR2(3) FIXED VARCHAR2(3) AUTOPURGE VARCHAR2(3) OPTIMIZER_COST NUMBER MODULE VARCHAR2(48) ACTION VARCHAR2(32) EXECUTIONS NUMBER ELAPSED_TIME NUMBER CPU_TIME NUMBER BUFFER_GETS NUMBER DISK_READS NUMBER DIRECT_WRITES NUMBER ROWS_PROCESSED NUMBER FETCHES NUMBER END_OF_FETCH_COUNT NUMBERSQL> descdbms_spmFUNCTION ALTER_SQL_PLAN_BASELINE RETURNS BINARY_INTEGER Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- SQL_HANDLE VARCHAR2 IN DEFAULT PLAN_NAME VARCHAR2 IN DEFAULT ATTRIBUTE_NAME VARCHAR2 IN ATTRIBUTE_VALUE VARCHAR2 INPROCEDURE CONFIGURE Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- PARAMETER_NAME VARCHAR2 IN PARAMETER_VALUE NUMBER IN DEFAULTPROCEDURE CREATE_STGTAB_BASELINE Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- TABLE_NAME VARCHAR2 IN TABLE_OWNER VARCHAR2 IN DEFAULT TABLESPACE_NAME VARCHAR2 IN DEFAULTFUNCTION DROP_MIGRATED_STORED_OUTLINE RETURNS BINARY_INTEGERFUNCTION DROP_SQL_PLAN_BASELINE RETURNS BINARY_INTEGER Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- SQL_HANDLE VARCHAR2 IN DEFAULT PLAN_NAME VARCHAR2 IN DEFAULTFUNCTION EVOLVE_SQL_PLAN_BASELINE RETURNS CLOB Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- SQL_HANDLE VARCHAR2 IN DEFAULT PLAN_NAME VARCHAR2 IN DEFAULT TIME_LIMIT NUMBER(38) IN DEFAULT VERIFY VARCHAR2 IN DEFAULT COMMIT VARCHAR2 IN DEFAULTFUNCTION EVOLVE_SQL_PLAN_BASELINE RETURNS CLOB Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- PLAN_LIST NAME_LIST IN TIME_LIMIT NUMBER(38) IN DEFAULT VERIFY VARCHAR2 IN DEFAULT COMMIT VARCHAR2 IN DEFAULTFUNCTION LOAD_PLANS_FROM_CURSOR_CACHE RETURNS BINARY_INTEGER Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- ATTRIBUTE_NAME VARCHAR2 IN ATTRIBUTE_VALUE VARCHAR2 IN FIXED VARCHAR2 IN DEFAULT ENABLED VARCHAR2 IN DEFAULTFUNCTION LOAD_PLANS_FROM_CURSOR_CACHE RETURNS BINARY_INTEGER Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- SQL_ID VARCHAR2 IN PLAN_HASH_VALUE NUMBER IN DEFAULT FIXED VARCHAR2 IN DEFAULT ENABLED VARCHAR2 IN DEFAULTFUNCTION LOAD_PLANS_FROM_CURSOR_CACHE RETURNS BINARY_INTEGER Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- SQL_ID VARCHAR2 IN PLAN_HASH_VALUE NUMBER IN DEFAULT SQL_TEXT CLOB IN FIXED VARCHAR2 IN DEFAULT ENABLED VARCHAR2 IN DEFAULTFUNCTION LOAD_PLANS_FROM_CURSOR_CACHE RETURNS BINARY_INTEGER Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- SQL_ID VARCHAR2 IN PLAN_HASH_VALUE NUMBER IN DEFAULT SQL_HANDLE VARCHAR2 IN FIXED VARCHAR2 IN DEFAULT ENABLED VARCHAR2 IN DEFAULTFUNCTION LOAD_PLANS_FROM_SQLSET RETURNS BINARY_INTEGER Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- SQLSET_NAME VARCHAR2 IN SQLSET_OWNER VARCHAR2 IN DEFAULT BASIC_FILTER VARCHAR2 IN DEFAULT FIXED VARCHAR2 IN DEFAULT ENABLED VARCHAR2 IN DEFAULT COMMIT_ROWS NUMBER IN DEFAULTFUNCTION MIGRATE_STORED_OUTLINE RETURNS CLOB Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- ATTRIBUTE_NAME VARCHAR2 IN ATTRIBUTE_VALUE CLOB IN DEFAULT FIXED VARCHAR2 IN DEFAULTFUNCTION MIGRATE_STORED_OUTLINE RETURNS CLOB Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- OUTLN_LIST NAME_LIST IN FIXED VARCHAR2 IN DEFAULTFUNCTION PACK_STGTAB_BASELINE RETURNS NUMBER Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- TABLE_NAME VARCHAR2 IN TABLE_OWNER VARCHAR2 IN DEFAULT SQL_HANDLE VARCHAR2 IN DEFAULT PLAN_NAME VARCHAR2 IN DEFAULT SQL_TEXT CLOB IN DEFAULT CREATOR VARCHAR2 IN DEFAULT ORIGIN VARCHAR2 IN DEFAULT ENABLED VARCHAR2 IN DEFAULT ACCEPTED VARCHAR2 IN DEFAULT FIXED VARCHAR2 IN DEFAULT MODULE VARCHAR2 IN DEFAULT ACTION VARCHAR2 IN DEFAULTFUNCTION UNPACK_STGTAB_BASELINE RETURNS NUMBER Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- TABLE_NAME VARCHAR2 IN TABLE_OWNER VARCHAR2 IN DEFAULT SQL_HANDLE VARCHAR2 IN DEFAULT PLAN_NAME VARCHAR2 IN DEFAULT SQL_TEXT CLOB IN DEFAULT CREATOR VARCHAR2 IN DEFAULT ORIGIN VARCHAR2 IN DEFAULT ENABLED VARCHAR2 IN DEFAULT ACCEPTED VARCHAR2 IN DEFAULT FIXED VARCHAR2 IN DEFAULT MODULE VARCHAR2 IN DEFAULT ACTION VARCHAR2 IN DEFAULT
  • Capture (Load)Evolve (Verify)DisplayModifyDrop
  • Capturing Plans AutomaticallyWhen automatic plan capture is enabled, the database automatically creates and maintains the plan history for SQL statements using information provided by the optimizer. The plan history includes relevant information used by the optimizer to reproduce an execution plan, such as the SQL text, outline, bind variables, and compilation environment.The optimizer marks the initial plan generated for a SQL statement as accepted for use, and represents both the plan history and SQL plan baseline. The plan history includes all subsequent plans. During the SQL plan baseline evolution phase, the database adds plans to the baseline that have been verified not to cause performance regressions.Manually Load Existing PlansYou can create SQL plan baselines by manually loading existing plans for a set of SQL statements as plan baselines. The database does not verify manually loaded plans for performance, but adds them as accepted plans to existing or new SQL plan baselines. You can use manual plan loading with or as an alternative to automatic plan capture.
  • Set optimizer_capture_sql_plan_baselines = TRUECan be modified at system/session level immediatelyOnly repeatable (execute or parse twice) SQL plan is captured.For a SQL, the first captured Sql Plan is loaded as accepted plan. The later plans are loaded as non-accepted plan.Selecting SQL Plan BaselinesDuring the SQL plan baseline selection phase, Oracle Database detects plan changes based on the stored plan history, and selects plans to avoid potential performance regressions for a set of SQL statements.Each time the database compiles a SQL statement, the optimizer does the following:1. Uses a cost-based search method to build a best-cost plan2. Tries to find a matching plan in the SQL plan baseline3.Does either of the following depending on whether a match is found:* If found, then the optimizer proceeds using the matched plan* If not found, then the optimizer evaluates the cost of each accepted plan in the SQL plan baseline and selects the plan with the lowest costSQL> create table t1 (id number);Table created.SQL> insert into t1 select rownum from dual connect by level<10001;10000 rows created.SQL> commit;Commit complete.SQL> alter session set optimizer_capture_sql_plan_baselines = TRUE;Session altered.SQL> select count(*) from t1 where id=100; COUNT(*)---------- 1SQL> select sql_handle, plan_name, enabled, accepted, fixed from dba_sql_plan_baselines; no rows selectedSQL> select count(*) from t1 where id=100; COUNT(*)---------- 1SQL> select sql_handle, plan_name, enabled, accepted, fixed, SQL_TEXT from dba_sql_plan_baselines where sql_text like '%id=100%';SQL_HANDLE PLAN_NAME ENA ACC FIX------------------------------ ------------------------------ --- --- ---SQL_TEXT--------------------------------------------------------------------------------SYS_SQL_e1d5d446a7219dcb SQL_PLAN_f3pfn8umk37fb616acf47 YES YES NO select count(*) from t1 where id=100SQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb', format=>'basic +note')); PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: NO Accepted: YES Origin: AUTO-CAPTURE--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------20 rows selected.SQL> create index t1_idx1 on t1(id);Index created.SQL> exec dbms_stats.gather_table_stats(user,'T1');PL/SQL procedure successfully completed.SQL> select count(*) from t1 where id=100; COUNT(*)---------- 1SQL> select sql_handle, plan_name, enabled, accepted, fixed, SQL_TEXT from dba_sql_plan_baselines where sql_text like '%where id=100%';SQL_HANDLE PLAN_NAME ENA ACC FIX------------------------------ ------------------------------ --- --- ---SQL_TEXT--------------------------------------------------------------------------------SYS_SQL_e1d5d446a7219dcb SQL_PLAN_f3pfn8umk37fb425cd488 YES NO NO select count(*) from t1 where id=100 --this is the new one added as non-accepted planSYS_SQL_e1d5d446a7219dcb SQL_PLAN_f3pfn8umk37fb616acf47 YES YES NO select count(*) from t1 where id=100SQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb', PLAN_NAME=>'SQL_PLAN_f3pfn8umk37fb425cd488',format=>'basic +note')); PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb425cd488 Plan id: 1113379976Enabled: YES Fixed: NO Accepted: NO Origin: AUTO-CAPTURE--------------------------------------------------------------------------------Plan hash value: 3695297570-------------------------------------| Id | Operation | Name |-------------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | INDEX RANGE SCAN| T1_IDX1 | --the new plan use index-------------------------------------20 rows selected.SQL> set autotracetraceonlySQL> select count(*) from t1 where id=100;Execution Plan----------------------------------------------------------Plan hash value: 3724264953---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 7 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | TABLE ACCESS FULL| T1 | 1 | 4 | 7 (0)| 00:00:01 |---------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - filter("ID"=100)Note----- - SQL plan baseline "SQL_PLAN_f3pfn8umk37fb616acf47" used for this statement--The sql use the accepted plan, even new plan is better but it is non-accepted
  •  Oracle will store the sql history in physical table. If you run a Sql once and then restart instance, run it again. This Sql will be considered repeatable Sql and the second plan will be capturedTest example:SQL> select count(*) from t1 where id=500; COUNT(*)---------- 11 row selected.SQL> conn /as sysdbaConnected.SQL> shutdown immediateDatabase closed.Database dismounted.ORACLE instance shut down.SQL> startupORACLE instance started.Total System Global Area 456146944 bytesFixed Size 1337184 bytesVariable Size 318769312 bytesDatabase Buffers 130023424 bytesRedo Buffers 6017024 bytesDatabase mounted.Database opened.SQL> select sql_handle, plan_name, enabled, accepted, fixed,sql_text from dba_sql_plan_baselines where sql_text like '%id=500%' and sql_text not like '%dba_sql_plan_baselines%'; no rows selectedSQL> select count(*) from t1 where id=500; <--execute it again after startup, oracle captured it at once COUNT(*)---------- 11 row selected.SQL> select sql_handle, plan_name, enabled, accepted, fixed,sql_text from dba_sql_plan_baselines where sql_text like '%id=500%' and sql_text not like '%dba_sql_plan_baselines%'; SQL_HANDLE PLAN_NAME ENA ACC FIX------------------------------ ------------------------------ --- --- ---SQL_TEXT--------------------------------------------------------------------------------SYS_SQL_255210fc1bc2b1d0 SQL_PLAN_2anhhzhdw5cfh7258443d YES YES NOselect count(*) from t1 where id=5001 row selected.Compare to disable auto-capture, the first run overhead, are (1 SELECT + 1 INSERT) on system tables; the second run overhead are (3 SELECT + 5 INSERT); Overhead when first run (1 SELECT + 1 INSERT):SELECT signature FROM sqllog$ WHERE signature = :1INSERT INTO sqllog$ SELECT :1, 1+TRUNC(sqllog$_seq.nextval/8192) FROM dualOverhead when second run (3 SELECT + 5 INSERT):SELECT /*+ INDEX(sqlobj$ (name obj_type)) */ signature, categoryFROMsqlobj$ WHERE name = :1 AND obj_type = :2SELECT obj_type, plan_id, name, flags, last_executedFROMsqlobj$ WHERE signature = :1 AND category = :2SELECT signature, inuse_features, flagsFROMsql$ WHERE signature = :1INSERT INTO sqlobj$(signature, category, obj_type, plan_id, name, flags, last_executed) INSERT INTO sqlobj$auxdata (signature, category, obj_type, plan_id, description, creator, origin, version, created, last_modified, last_verified, parse_cpu_time, optimizer_cost, module, action, priority, optimizer_env, bind_data, parsing_schema_name, executions, elapsed_time, cpu_time, buffer_gets, disk_reads, direct_writes, rows_processed, fetches, end_of_fetch_count,task_id, task_exec_name, task_obj_id, task_fnd_id, task_rec_id, flags, spare1, spare2) INSERT INTO sqlobj$data (signature, category, obj_type, plan_id, comp_data, spare1, spare2) INSERT INTO sql$ (signature, inuse_features, flags, spare1, spare2) INSERT INTO sql$text (signature, sql_handle, sql_text) Test example:SQL> show parameter sql_planNAME TYPE VALUE------------------------------------ ----------- ------------------------------optimizer_capture_sql_plan_baselines boolean TRUEoptimizer_use_sql_plan_baselines boolean TRUESQL> select spid from v$process p, v$session s, v$mystat m where p.addr=s.paddr and s.sid=m.sid and rownum=1;SPID------------------------9951 row selected.SQL> alter session set events '10046 trace name context forever, level 12';Session altered.SQL> select count(*) from t1 where id=504; COUNT(*)---------- 11 row selected.SQL> alter session set events '10046 trace name context off';Session altered.SQL> alter system set optimizer_capture_sql_plan_baselines=false;System altered.SQL> show parameter sql_planNAME TYPE VALUE------------------------------------ ----------- ------------------------------optimizer_capture_sql_plan_baselines boolean FALSEoptimizer_use_sql_plan_baselines boolean TRUESQL> connalex/alexConnected.SQL> select spid from v$process p, v$session s, v$mystat m where p.addr=s.paddr and s.sid=m.sid and rownum=1;SPID------------------------10001 row selected.SQL> alter session set events '10046 trace name context forever, level 12';Session altered.SQL> select count(*) from t1 where id=504; COUNT(*)---------- 11 row selected.SQL> alter session set events '10046 trace name context off';Session altered.[oracle@linux2 trace]$ tkprof ora112_ora_995.trc 995.out --> auto-capture enable[oracle@linux2 trace]$ tkprof ora112_ora_1000.trc 1000.out --> without auto-capture[oracle@linux2 trace]$ diff 995.out 1000.out......--> these are first run overhead. oracle will need to store the sql< SQL ID: 2uzqkdqdtkn8p< Plan Hash: 1267113925< SELECT signature < FROM< sqllog$ WHERE signature = :1< < < call count cpu elapsed disk query current rows< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< Parse 1 0.00 0.00 0 0 0 0< Execute 1 0.00 0.00 0 0 0 0< Fetch 1 0.00 0.00 0 1 0 0< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< total 3 0.00 0.00 0 1 0 0< < Misses in library cache during parse: 0< Optimizer mode: CHOOSE< Parsing user id: SYS (recursive depth: 1)< < Rows Row Source Operation< ------- ---------------------------------------------------< 0 INDEX UNIQUE SCAN SQLLOG$_PKEY (cr=1 pr=0 pw=0 time=0 us cost=0 size=13 card=1)(object id 187)< < < Elapsed times include waiting on following events:< Event waited on Times Max. Wait Total Waited< ---------------------------------------- Waited ---------- ------------< SQL*Net message to client 1 0.00 0.00< SQL*Net message from client 1 5.47 5.47< ********************************************************************************< < SQL ID: 01zk56t60wvf1< Plan Hash: 1877030569< INSERT INTO sqllog$ SELECT :1, 1+TRUNC(sqllog$_seq.nextval/8192) FROM dual< < < call count cpu elapsed disk query current rows< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< Parse 1 0.00 0.00 0 0 0 0< Execute 1 0.02 0.02 0 0 3 1< Fetch 0 0.00 0.00 0 0 0 0< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< total 2 0.02 0.02 0 0 3 1< < Misses in library cache during parse: 0< Optimizer mode: CHOOSE< Parsing user id: SYS (recursive depth: 1)< < Rows Row Source Operation< ------- ---------------------------------------------------< 0 LOAD TABLE CONVENTIONAL (cr=0 pr=0 pw=0 time=0 us)< 1 SEQUENCE SQLLOG$_SEQ (cr=0 pr=0 pw=0 time=0 us)< 1 FAST DUAL (cr=0 pr=0 pw=0 time=0 us cost=2 size=0 card=1)< < ********************************************************************************......--> test the second run and compare. oracle will need to check the sql history and store the sql plan baselineSQL> select spid from v$process p, v$session s, v$mystat m where p.addr=s.paddr and s.sid=m.sid and rownum=1;SPID------------------------12881 row selected.SQL> select sql_handle, plan_name, enabled, accepted, fixed,sql_text from dba_sql_plan_baselines where sql_text like '%id=504%' and sql_text not like '%dba_sql_plan_baselines%'; no rows selectedSQL> show parameter sql_planNAME TYPE VALUE------------------------------------ ----------- ------------------------------optimizer_capture_sql_plan_baselines boolean TRUEoptimizer_use_sql_plan_baselines boolean TRUESQL> alter session set events '10046 trace name context forever, level 12';Session altered.SQL> select count(*) from t1 where id=504; COUNT(*)---------- 11 row selected.SQL> alter session set events '10046 trace name context off';Session altered.SQL> select sql_handle, plan_name, enabled, accepted, fixed,sql_text from dba_sql_plan_baselines where sql_text like '%id=504%' and sql_text not like '%dba_sql_plan_baselines%'; SQL_HANDLE PLAN_NAME ENA ACC FIX------------------------------ ------------------------------ --- --- ---SQL_TEXT--------------------------------------------------------------------------------SYS_SQL_3fa8bbd98c84c1b6 SQL_PLAN_3za5vv6689hdq7258443d YES YES NOselect count(*) from t1 where id=5041 row selected.[oracle@linux2 trace]$ tkprof ora112_ora_1288.trc 1288.out --> with auto-capture and stored it's plan as accepted.--> the second run overhead.[oracle@linux2 trace]$ diff 1288.out 1000.out...< ********************************************************************************< < SQL ID: 9zr2sbbp1d4fs< Plan Hash: 2462756431< SELECT /*+ INDEX(sqlobj$ (name obj_type)) */ signature, category < FROM< sqlobj$ < WHERE name = :1 < AND obj_type = :2< < < call count cpu elapsed disk query current rows< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< Parse 1 0.01 0.01 0 0 0 0< Execute 1 0.02 0.03 0 0 0 0< Fetch 1 0.00 0.00 0 1 0 0< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< total 3 0.04 0.05 0 1 0 0< < Misses in library cache during parse: 1< Misses in library cache during execute: 1< Optimizer mode: CHOOSE< Parsing user id: SYS (recursive depth: 1)< < Rows Row Source Operation< ------- ---------------------------------------------------< 0 INDEX RANGE SCAN I_SQLOBJ$NAME_TYPE (cr=1 pr=0 pw=0 time=0 us cost=0 size=60 card=1)(object id 206)< < ********************************************************************************< < SQL ID: 47y3mqvyhpkvs< Plan Hash: 1888265482< SELECT obj_type, plan_id, name, flags, last_executed< FROM< sqlobj$ < WHERE signature = :1 < AND category = :2< < < call count cpu elapsed disk query current rows< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< Parse 1 0.00 0.00 0 0 0 0< Execute 1 0.02 0.02 0 0 0 0< Fetch 1 0.00 0.00 0 1 0 0< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< total 3 0.03 0.04 0 1 0 0< < Misses in library cache during parse: 1< Misses in library cache during execute: 1< Optimizer mode: CHOOSE< Parsing user id: SYS (recursive depth: 1)< < Rows Row Source Operation< ------- ---------------------------------------------------< 0 INDEX RANGE SCAN SQLOBJ$_PKEY (cr=1 pr=0 pw=0 time=0 us cost=0 size=99 card=1)(object id 205)< < ********************************************************************************< < SQL ID: 69subccxd9b03< Plan Hash: 4264803741< SELECT signature, inuse_features, flags < FROM< sql$ < WHERE signature = :1< < < call count cpu elapsed disk query current rows< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< Parse 1 0.00 0.00 0 0 0 0< Execute 1 0.02 0.02 0 0 0 0< Fetch 1 0.00 0.00 0 1 0 0< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< total 3 0.03 0.03 0 1 0 0< < Misses in library cache during parse: 1< Misses in library cache during execute: 1< Optimizer mode: CHOOSE< Parsing user id: SYS (recursive depth: 1)< < Rows Row Source Operation< ------- ---------------------------------------------------< 0 TABLE ACCESS BY INDEX ROWID SQL$ (cr=1 pr=0 pw=0 time=0 us cost=0 size=39 card=1)< 0 INDEX UNIQUE SCAN I_SQL$_PKEY (cr=1 pr=0 pw=0 time=0 us cost=0 size=0 card=1)(object id 194)< < ********************************************************************************< < SQL ID: gjcmzjjxzkv0h< Plan Hash: 0< INSERT < INTO sqlobj$(signature, category, obj_type, plan_id, name, < flags, last_executed) < < VALUES< (:1, :2, :3, :4, :5, :6, :7)< < < call count cpu elapsed disk query current rows< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< Parse 1 0.01 0.01 0 0 0 0< Execute 1 0.06 0.06 0 0 5 1< Fetch 0 0.00 0.00 0 0 0 0< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< total 2 0.07 0.08 0 0 5 1< < Misses in library cache during parse: 1< Misses in library cache during execute: 1< Optimizer mode: CHOOSE< Parsing user id: SYS (recursive depth: 1)< < Rows Row Source Operation< ------- ---------------------------------------------------< 0 LOAD TABLE CONVENTIONAL (cr=0 pr=0 pw=0 time=0 us)< < ********************************************************************************< < SQL ID: 3amxp54556pxg< Plan Hash: 0< INSERT < INTO sqlobj$auxdata< (signature, category, obj_type, plan_id, < description, creator, origin, version, < created, last_modified, last_verified, parse_cpu_time, < optimizer_cost, module, action, priority, < optimizer_env, bind_data, parsing_schema_name, executions, < elapsed_time, cpu_time, buffer_gets, disk_reads, < direct_writes, rows_processed, fetches, end_of_fetch_count, < task_id, task_exec_name, task_obj_id, task_fnd_id, < task_rec_id, flags, spare1, spare2) < < VALUES< (:1, < :2, :3, :4, < :5, :6, :7, :8, < :9, :10, null, null, < :11, :12, :13, :14, < :15, :16, :17, :18, < :19, :20, :21, :22, < :23, :24, :25, :26, < :27, :28, :29, :30, < :31, 0, null, null)< < < call count cpu elapsed disk query current rows< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< Parse 1 0.00 0.01 0 0 0 0< Execute 1 0.09 0.11 0 1 5 1< Fetch 0 0.00 0.00 0 0 0 0< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< total 2 0.10 0.12 0 1 5 1< < Misses in library cache during parse: 1< Misses in library cache during execute: 1< Optimizer mode: CHOOSE< Parsing user id: SYS (recursive depth: 1)< < Rows Row Source Operation< ------- ---------------------------------------------------< 0 LOAD TABLE CONVENTIONAL (cr=1 pr=0 pw=0 time=0 us)< < ********************************************************************************< < SQL ID: 5p8a16q2029qs< Plan Hash: 0< INSERT < INTO sqlobj$data< (signature, category, obj_type, plan_id, comp_data, spare1, spare2) < < VALUES< (:1, :2, :3, :4, :5, null, null)< < < call count cpu elapsed disk query current rows< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< Parse 1 0.00 0.00 0 0 0 0< Execute 1 0.03 0.03 0 3 12 1< Fetch 0 0.00 0.00 0 0 0 0< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< total 2 0.03 0.04 0 3 12 1< < Misses in library cache during parse: 1< Misses in library cache during execute: 1< Optimizer mode: CHOOSE< Parsing user id: SYS (recursive depth: 1)< < Rows Row Source Operation< ------- ---------------------------------------------------< 0 LOAD TABLE CONVENTIONAL (cr=3 pr=0 pw=1 time=0 us)< < < Elapsed times include waiting on following events:< Event waited on Times Max. Wait Total Waited< ---------------------------------------- Waited ---------- ------------< Disk file operations I/O 1 0.00 0.00< direct path write 1 0.00 0.00< direct path sync 1 0.00 0.00< ********************************************************************************< < SQL ID: 9zshcv60xux34< Plan Hash: 0< INSERT INTO sql$ (signature, inuse_features, flags, spare1, spare2) < VALUES< (:1, :2, :3, null, null)< < < call count cpu elapsed disk query current rows< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< Parse 1 0.00 0.00 0 0 0 0< Execute 1 0.00 0.01 0 1 5 1< Fetch 0 0.00 0.00 0 0 0 0< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< total 2 0.01 0.01 0 1 5 1< < Misses in library cache during parse: 1< Misses in library cache during execute: 1< Optimizer mode: CHOOSE< Parsing user id: SYS (recursive depth: 1)< < Rows Row Source Operation< ------- ---------------------------------------------------< 0 LOAD TABLE CONVENTIONAL (cr=1 pr=0 pw=0 time=0 us)< < ********************************************************************************< < SQL ID: 21m8k7puzg06s< Plan Hash: 0< INSERT INTO sql$text (signature, sql_handle, sql_text) < VALUES< (:1, :2, :3)< < < call count cpu elapsed disk query current rows< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< Parse 1 0.00 0.00 0 0 0 0< Execute 1 0.00 0.00 0 2 7 1< Fetch 0 0.00 0.00 0 0 0 0< ------- ------ -------- ---------- ---------- ---------- ---------- ----------< total 2 0.01 0.01 0 2 7 1< < Misses in library cache during parse: 1< Misses in library cache during execute: 1< Optimizer mode: CHOOSE< Parsing user id: SYS (recursive depth: 1)< < Rows Row Source Operation< ------- ---------------------------------------------------< 0 LOAD TABLE CONVENTIONAL (cr=2 pr=0 pw=0 time=0 us)<  Oracle will capture the sql plan if the Sql text is the same regardless of whether the visited table are same or not. It is TEXT-base! --session ALEXSQL> show userUSER is "ALEX"SQL> set autotracetraceonlySQL> select count(*) from t1 where id=405;1 row selected.Execution Plan----------------------------------------------------------Plan hash value: 3724264953---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 2 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | TABLE ACCESS FULL| T1 | 1 | 4 | 2 (0)| 00:00:01 |---------------------------------------------------------------------------SQL> select sql_handle, plan_name, enabled, accepted, fixed,sql_text from dba_sql_plan_baselines where sql_text like '%id=405%';no rows selected--session WZENGSQL> show userUSER is "WZENG"SQL> create table t1 as select * from alex.t1;Table created.SQL> create index wzeng_t1_idx on t1(id);Index created.SQL> exec dbms_stats.gather_table_stats(user,'t1',cascade=>TRUE);PL/SQL procedure successfully completed.SQL> set autotracetraceonlySQL> select count(*) from t1 where id=405;Execution Plan----------------------------------------------------------Plan hash value: 2310175645----------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |--------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 1 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | INDEX RANGE SCAN| WZENG_T1_IDX | 1 | 4 | 1 (0)| 00:00:01 |--------------------------------------------------------------------------------SQL> set autotrace offSQL> select sql_handle, plan_name, enabled, accepted, fixed,sql_text from dba_sql_plan_baselines where sql_text like '%id=405%' and sql_text not like '%dba_sql_plan_baselines%';SQL_HANDLE PLAN_NAME ENA ACC FIX------------------------------ ------------------------------ --- --- ---SQL_TEXT--------------------------------------------------------------------------------SYS_SQL_76e206336fc3dcb9 SQL_PLAN_7dsh66drw7r5t7258443d YES YES NOselect count(*) from t1 where id=405--> oracle consider this sql is repeated as the sql text is the same, even the base table are from different schemaSQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_76e206336fc3dcb9',format=>'typical'));PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_76e206336fc3dcb9SQL text: select count(*) from t1 where id=405----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_7dsh66drw7r5t7258443d Plan id: 1918387261Enabled: YES Fixed: NO Accepted: YES Origin: AUTO-CAPTURE--------------------------------------------------------------------------------PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------------------------------Plan hash value: 2310175645----------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 1 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | INDEX RANGE SCAN| WZENG_T1_IDX | 1 | 4 | 1 (0)| 00:00:01 |----------------------------------------------------------------------------------Predicate Information (identified by operation id):PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 2 - access("ID"=405)25 rows selected.-->oracle use the later sql plan loaded into baseline--when run this sql again--session WZENGSQL> show userUSER is "WZENG"SQL> set autotracetraceonlySQL> select count(*) from t1 where id=405;Execution Plan----------------------------------------------------------Plan hash value: 2310175645----------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 1 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | INDEX RANGE SCAN| WZENG_T1_IDX | 1 | 4 | 1 (0)| 00:00:01 |----------------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("ID"=405)Note----- - SQL plan baseline "SQL_PLAN_7dsh66drw7r5t7258443d" used for this statement--> it used the SQL plan baseline, no wonder--session ALEXSQL> show userUSER is "ALEX"SQL> select count(*) from t1 where id=405;1 row selected.Execution Plan----------------------------------------------------------Plan hash value: 3724264953---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 2 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | TABLE ACCESS FULL| T1 | 1 | 4 | 2 (0)| 00:00:01 |-----------------------------------------------------------------------------> it didn't use the SQL plan baseline, no wonder, casue the index WZENG_T1_IDX didn't exist in ALEX.T1--> create the same index and see what happenedSQL> create index WZENG_T1_IDX on t1(id);Index created.SQL> select owner,index_name from dba_indexes where table_name='T1';OWNER INDEX_NAME------------------------------ ------------------------------WZENG WZENG_T1_IDXALEX WZENG_T1_IDX2 rows selected.SQL> set autotracetraceonlySQL> select count(*) from t1 where id=405;1 row selected.Execution Plan----------------------------------------------------------Plan hash value: 2310175645----------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 1 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | INDEX RANGE SCAN| WZENG_T1_IDX | 1 | 4 | 1 (0)| 00:00:01 |----------------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("ID"=405)Note----- - SQL plan baseline "SQL_PLAN_7dsh66drw7r5t7258443d" used for this statement--> it used the SQL plan baseline as the index with same name existing, even the sql plan is created from user WZENG.
  • Manually loaded plan is stored as accepted plan.STS can be created from AWR Snapshots/cursor cache/manual provided SQLOther variables: -- fixed ('NO') -- enabled ('YES')--Load From Cursor CacheSQL> alter session set optimizer_capture_sql_plan_baselines=false;Session altered.SQL> select count(*) from t1 where id=200; COUNT(*)---------- 1SQL> / COUNT(*)---------- 1SQL> select sql_handle, plan_name, enabled, accepted, fixed, SQL_TEXT from dba_sql_plan_baselines where sql_text like '%id=200%';no rows selected --don't loaded automaticallySQL> select sql_id,sql_text from v$sql where sql_text like '%where id=200%' and sql_text not like '%v$sql%';SQL_ID-------------SQL_TEXT--------------------------------------------------------------------------------9xgju1cn9bm73 --get SQL_IDselect count(*) from t1 where id=200SQL> variable cnt number; SQL> exec :cnt :=dbms_spm.load_plans_from_cursor_cache(sql_id=>'9xgju1cn9bm73');PL/SQL procedure successfully completed.SQL> select sql_handle, plan_name, enabled, accepted, fixed, SQL_TEXT from dba_sql_plan_baselines where sql_text like '%id=200%';SQL_HANDLE PLAN_NAME ENA ACC FIX------------------------------ ------------------------------ --- --- ---SQL_TEXT--------------------------------------------------------------------------------SYS_SQL_3ab671ad5c78894a SQL_PLAN_3pdmjppf7j2aa425cd488 YES YES NO --loaded as acceptedselect count(*) from t1 where id=200SQL> select count(*) from dba_sql_plan_baselines; COUNT(*)---------- 11SQL> exec :cnt :=dbms_spm.load_plans_from_cursor_cache(attribute_name=>'SQL_TEXT',attribute_value=>'%t1%');PL/SQL procedure successfully completed.SQL> print :cnt CNT---------- 30SQL> select count(*) from dba_sql_plan_baselines; COUNT(*)---------- 29 --why it didn't match 11+30 =41? Maybe 30 is the total match sqlSQL> select count(*) from v$sql where sql_text like '%t1%'; COUNT(*)---------- 36--Load Plans From SQL Tuning Sets(STS) DECLAREmy_plans PLS_INTEGER;BEGINmy_plans := DBMS_SPM.LOAD_PLANS_FROM_SQLSET( sqlset_name => 'tset1');END;/
  • --1. If the Sql has a Sql plan baseline, even set optimizer_capture_sql_plan_baselines to false, it will still automatically capture this sql's planSQL> show parameter sql_planNAME TYPE VALUE------------------------------------ ----------- ------------------------------optimizer_capture_sql_plan_baselines boolean FALSEoptimizer_use_sql_plan_baselines boolean TRUESQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note')); PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------20 rows selected.SQL> set autotracetraceonlySQL> select count(*) from t1 where id=100;Execution Plan----------------------------------------------------------Plan hash value: 3724264953---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 7 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | TABLE ACCESS FULL| T1 | 1 | 4 | 7 (0)| 00:00:01 |---------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - filter("ID"=100)Note----- - SQL plan baseline "SQL_PLAN_f3pfn8umk37fb616acf47" used for this statementSQL> set autotrace offSQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note')); PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb425cd488 Plan id: 1113379976Enabled: YES Fixed: NO Accepted: NO Origin: AUTO-CAPTURE--------------------------------------------------------------------------------Plan hash value: 3695297570-------------------------------------| Id | Operation | Name |-------------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | INDEX RANGE SCAN| T1_IDX1 |---------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------2.If the sql has a fixed Sql plan baseline, SQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note'));PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: YES Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------20 rows selected.SQL> set autotracetraceonlySQL> select count(*) from t1 where id=100;Execution Plan----------------------------------------------------------Plan hash value: 3724264953---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 7 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | TABLE ACCESS FULL| T1 | 1 | 4 | 7 (0)| 00:00:01 |---------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - filter("ID"=100)Note----- - SQL plan baseline "SQL_PLAN_f3pfn8umk37fb616acf47" used for this statementSQL> set autotrace offSQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note'));PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: YES Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------20 rows selected. --no new plan added.SQL> alter session set optimizer_capture_sql_plan_baselines =true;Session altered.SQL> set autotracetraceonlySQL> select count(*) from t1 where id=100;Execution Plan----------------------------------------------------------Plan hash value: 3724264953---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 7 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | TABLE ACCESS FULL| T1 | 1 | 4 | 7 (0)| 00:00:01 |---------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - filter("ID"=100)Note----- - SQL plan baseline "SQL_PLAN_f3pfn8umk37fb616acf47" used for this statementSQL> set autotrace offSQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note'));PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: YES Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------20 rows selected. –no plan added even optimizer_capture_sql_plan_baselines = TRUE--3. The behavior of auto-capture doesn't change when set optimizer_use_sql_plan_baselines to FALSE.SQL> alter session set optimizer_capture_sql_plan_baselines=true;Session altered.SQL> alter session set optimizer_use_sql_plan_baselines=false; Session altered. -->set optimizer_use_sql_plan_baselines to falseSQL> show parameter sql_planNAME TYPE VALUE------------------------------------ ----------- ------------------------------optimizer_capture_sql_plan_baselines boolean TRUEoptimizer_use_sql_plan_baselines boolean FALSESQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note')); PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: YES Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------20 rows selected.SQL> select count(*) from t1 where id=100; COUNT(*)---------- 1SQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note')); PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: YES Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-------------------------------------> the new plan didn't be captured when the baseline is fixed.20 rows selected.SQL> exec :cnt:=dbms_spm.alter_sql_plan_baseline(sql_handle=>'SYS_SQL_e1d5d446a7219dcb', PLAN_NAME=>'SQL_PLAN_f3pfn8umk37fb616acf47',attribute_name=>'fixed',attribute_value=>'NO');PL/SQL procedure successfully completed.--> change the baseline to non-fixed.SQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note')); PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------20 rows selected.SQL> select count(*) from t1 where id=100; COUNT(*)---------- 1SQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note')); PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb425cd488 Plan id: 1113379976Enabled: YES Fixed: NO Accepted: NO Origin: AUTO-CAPTURE--------------------------------------------------------------------------------Plan hash value: 3695297570-------------------------------------| Id | Operation | Name |-------------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | INDEX RANGE SCAN| T1_IDX1 |---------------------------------------> the new plan is captued.--------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------35 rows selected.
  • Use procedure: -- dbms_spm.evolve_sql_plan_baselineTo identify Sql plan baseline: -- no input, evolve all non-accepted plans -- sql_handle, evolve this sql's all non-accepted plans -- plan_name, evolve this plan only -- plan_list, evolve a list of sql planOther variables: -- verify ('YES'), execute the plan and compare, 'NO' means set ACCEPTED to YES directly -- commit ('YES'), update ACCEPTED status if have improvement. 'No' means don't update only report -- time_limit (minutes), applies only if verify = 'YES', maximum time this procedure can run.Syntax:DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE ( sql_handle IN VARCHAR2 := NULL, plan_name IN VARCHAR2 := NULL,time_limit IN INTEGER := DBMS_SPM.AUTO_LIMIT, verify IN VARCHAR2 := 'YES', commit IN VARCHAR2 := 'YES') RETURN CLOB;DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE (plan_list IN DBMS_SPM.NAME_LIST,time_limit IN INTEGER := DBMS_SPM.AUTO_LIMIT, verify IN VARCHAR2 := 'YES', commit IN VARCHAR2 := 'YES') RETURN CLOB;Example:SQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note')); PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb425cd488 Plan id: 1113379976Enabled: YES Fixed: YES Accepted: NO Origin: AUTO-CAPTURE--------------------------------------------------------------------------------Plan hash value: 3695297570-------------------------------------| Id | Operation | Name |-------------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | INDEX RANGE SCAN| T1_IDX1 |---------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------35 rows selected.SQL> var rpt clob;SQL> exec :rpt := dbms_spm.evolve_sql_plan_baseline(sql_handle => 'SYS_SQL_e1d5d446a7219dcb', PLAN_NAME=>'SQL_PLAN_f3pfn8umk37fb425cd488',verify=>'NO') ;PL/SQL procedure successfully completed.SQL> print :rptRPT--------------------------------------------------------------------------------------------------------------------------------------------------------------- Evolve SQL Plan Baseline Report-------------------------------------------------------------------------------Inputs:------- SQL_HANDLE = SYS_SQL_e1d5d446a7219dcb PLAN_NAME = SQL_PLAN_f3pfn8umk37fb425cd488 TIME_LIMIT = DBMS_SPM.AUTO_LIMIT VERIFY = NO COMMIT = YESPlan: SQL_PLAN_f3pfn8umk37fb425cd488------------------------------------ Plan was changed to an accepted plan.------------------------------------------------------------------------------- Report Summary-------------------------------------------------------------------------------Number of plans verified: 0Number of plans accepted: 1SQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note')); PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb425cd488 Plan id: 1113379976Enabled: YES Fixed: YES Accepted: YES Origin: AUTO-CAPTURE--------------------------------------------------------------------------------Plan hash value: 3695297570-------------------------------------| Id | Operation | Name |-------------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | INDEX RANGE SCAN| T1_IDX1 |---------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------35 rows selected.--evlove all non-accepted planSQL> exec :rpt := dbms_spm.evolve_sql_plan_baseline() ;PL/SQL procedure successfully completed.
  •  Oracle will run the non-accepted sql plan and the best accepted Sql plan, not all accepted Sql plan-- Here are 2 accepted sql plan, 1 non-accepted planSQL> select * from table(dbms_xplan.display_sql_plan_baseline(sql_handle=>'SYS_SQL_1a9420dc6aa256d8'));PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_1a9420dc6aa256d8SQL text: select count(*) from t1, t2 where t1.id=t2.id----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_1p510vjpa4pqsb3e28116 Plan id: 3017965846Enabled: YES Fixed: NO Accepted: NO Origin: AUTO-CAPTURE--------------------------------------------------------------------------------Plan hash value: 3975918519----------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 17 | 15 (7)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 17 | | ||* 2 | HASH JOIN | | 1634 | 27778 | 15 (7)| 00:00:01 || 3 | INDEX FAST FULL SCAN| T1_IDX1 | 10000 | 40000 | 7 (0)| 00:00:01 || 4 | TABLE ACCESS FULL | T2 | 1634 | 21242 | 7 (0)| 00:00:01 |----------------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("T1"."ID"="T2"."ID")--------------------------------------------------------------------------------Plan name: SQL_PLAN_1p510vjpa4pqsc6a45b88 Plan id: 3332660104Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 906334482----------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 17 | 15 (7)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 17 | | ||* 2 | HASH JOIN | | 1634 | 27778 | 15 (7)| 00:00:01 || 3 | TABLE ACCESS FULL| T1 | 10000 | 40000 | 7 (0)| 00:00:01 || 4 | TABLE ACCESS FULL| T2 | 1634 | 21242 | 7 (0)| 00:00:01 |----------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("T1"."ID"="T2"."ID")--------------------------------------------------------------------------------Plan name: SQL_PLAN_1p510vjpa4pqsf90a49c2 Plan id: 4178201026Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3994110048----------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 17 | 30 (4)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 17 | | ||* 2 | HASH JOIN | | 1634 | 27778 | 30 (4)| 00:00:01 || 3 | INDEX FAST FULL SCAN| T1_IDX1 | 10000 | 40000 | 7 (0)| 00:00:01 || 4 | INDEX FULL SCAN | T2_IDX1 | 1634 | 21242 | 22 (0)| 00:00:01 |----------------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("T1"."ID"="T2"."ID")71 rows selected.--let's evolve it and trace the sessionSQL> select spid from v$process p, v$session s where p.addr=s.paddr and s.sid=sys_context('userenv','sid');SPID------------------------54311 row selected.SQL> alter session set events '10046 trace name context forever, level 12'; Session altered.SQL> exec :rpt:=dbms_spm.evolve_sql_plan_baseline(sql_handle=>'SYS_SQL_1a9420dc6aa256d8');PL/SQL procedure successfully completed.SQL> alter session set events '10046 trace name context off';Session altered.SQL> print :rptRPT--------------------------------------------------------------------------------------------------------------------------------------------------------------- Evolve SQL Plan Baseline Report-------------------------------------------------------------------------------Inputs:------- SQL_HANDLE = SYS_SQL_1a9420dc6aa256d8 PLAN_NAME = TIME_LIMIT = DBMS_SPM.AUTO_LIMIT VERIFY = YES COMMIT = YESPlan: SQL_PLAN_1p510vjpa4pqsb3e28116------------------------------------ Plan was verified: Time used .16 seconds. Plan failed performance criterion: 1.04 times worse than baseline plan. Baseline Plan Test Plan Stats Ratio ------------- --------- ----------- Execution Status: COMPLETE COMPLETE Rows Processed: 1 1 Elapsed Time(ms): 3.158 3.221 .98 CPU Time(ms): 3.11 3.221 .97 Buffer Gets: 46 49 .94 Physical Read Requests: 0 0 Physical Write Requests: 0 0 Physical Read Bytes:0 0 Physical Write Bytes: 0 0 Executions: 1 1------------------------------------------------------------------------------- Report Summary-------------------------------------------------------------------------------Number of plans verified: 1Number of plans accepted: 0--check the trace file[oracle@linux2 trace]$ tkprof ora112_ora_5431.trc 5431.out[oracle@linux2 trace]$ view 5431.out********************************************************************************SQL ID: 9g76zjbf6txk9Plan Hash: 3332660104select count(*)from t1, t2 where t1.id=t2.idcall count cpu elapsed disk query current rows------- ------ -------- ---------- ---------- ---------- ---------- ----------Parse 1 0.00 0.00 0 0 0 0Execute 10 0.00 0.00 0 0 0 0Fetch 10 0.03 0.03 0 460 0 10------- ------ -------- ---------- ---------- ---------- ---------- ----------total 21 0.03 0.03 0 460 0 10Misses in library cache during parse: 1Optimizer mode: ALL_ROWSParsing user id: 66 (recursive depth: 1)********************************************************************************SQL ID: 9g76zjbf6txk9Plan Hash: 3017965846select count(*)from t1, t2 where t1.id=t2.idcall count cpu elapsed disk query current rows------- ------ -------- ---------- ---------- ---------- ---------- ----------Parse 1 0.00 0.00 0 0 0 0Execute 10 0.00 0.00 0 0 0 0Fetch 10 0.03 0.03 0 490 0 10------- ------ -------- ---------- ---------- ---------- ---------- ----------total 21 0.03 0.03 0 490 0 10Misses in library cache during parse: 1Optimizer mode: ALL_ROWSParsing user id: 66 (recursive depth: 1)********************************************************************************SQL> select * from table(dbms_xplan.display_cursor(sql_id=>'9g76zjbf6txk9',CURSOR_CHILD_NO=>0));PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------------------------------SQL_ID 9g76zjbf6txk9, child number 0-------------------------------------/* SQL Analyze(1,0) */ select count(*) from t1, t2 where t1.id=t2.idPlan hash value: 3332660104----------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | 15 (100)| || 1 | SORT AGGREGATE | | 1 | 17 | | ||* 2 | HASH JOIN | | 1634 | 27778 | 15 (7)| 00:00:01 || 3 | TABLE ACCESS FULL| T1 | 10000 | 40000 | 7 (0)| 00:00:01 || 4 | TABLE ACCESS FULL| T2 | 1634 | 21242 | 7 (0)| 00:00:01 |----------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("T1"."ID"="T2"."ID")21 rows selected.SQL> select * from table(dbms_xplan.display_cursor(sql_id=>'9g76zjbf6txk9',CURSOR_CHILD_NO=>1));PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------------------------------SQL_ID 9g76zjbf6txk9, child number 1-------------------------------------/* SQL Analyze(1,0) */ select count(*) from t1, t2 where t1.id=t2.idPlan hash value: 3017965846----------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | 15 (100)| || 1 | SORT AGGREGATE | | 1 | 17 | | ||* 2 | HASH JOIN | | 1634 | 27778 | 15 (7)| 00:00:01 || 3 | INDEX FAST FULL SCAN| T1_IDX1 | 10000 | 40000 | 7 (0)| 00:00:01 || 4 | TABLE ACCESS FULL | T2 | 1634 | 21242 | 7 (0)| 00:00:01 |----------------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("T1"."ID"="T2"."ID")21 rows selected.--oracle executed the non-accepted plan and the lowest cost accepted plan. Do not run the high cost accepted plan. This is reasonable. When evolve Sql plan with bind variable, oracle will use the bind variable value when the plan was captured.SQL> select spid from v$processp,v$session s where p.addr=s.paddr and s.sid=sys_context('userenv','sid');SPID------------------------13361 row selected.SQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_aa75155372e8747f',format=>'typical'));PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_aa75155372e8747fSQL text: select count(*) from t1 where id=:id----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_anx8padtfhx3z425cd488 Plan id: 1113379976Enabled: YES Fixed: NO Accepted: NO Origin: AUTO-CAPTURE--------------------------------------------------------------------------------Plan hash value: 3695297570-----------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |-----------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 1 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | INDEX RANGE SCAN| T1_IDX1 | 1 | 4 | 1 (0)| 00:00:01 |-----------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("ID"=TO_NUMBER(:ID))--------------------------------------------------------------------------------Plan name: SQL_PLAN_anx8padtfhx3z616acf47 Plan id: 1634389831Enabled: YES Fixed: NO Accepted: YES Origin: AUTO-CAPTURE--------------------------------------------------------------------------------Plan hash value: 3724264953---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 7 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | TABLE ACCESS FULL| T1 | 1 | 4 | 7 (0)| 00:00:01 |---------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - filter("ID"=TO_NUMBER(:ID))45 rows selected.SQL> exec :id:=999;PL/SQL procedure successfully completed.SQL> var rpt clobSQL> alter session set events '10046 trace name context forever,level 12';Session altered.SQL> exec :rpt := dbms_spm.evolve_sql_plan_baseline(sql_handle => 'SYS_SQL_aa75155372e8747f') ;PL/SQL procedure successfully completed.SQL> alter session set events '10046 trace name context off';Session altered.SQL> set long 9999SQL> print :rptRPT--------------------------------------------------------------------------------------------------------------------------------------------------------------- Evolve SQL Plan Baseline Report-------------------------------------------------------------------------------Inputs:------- SQL_HANDLE = SYS_SQL_aa75155372e8747f PLAN_NAME = TIME_LIMIT = DBMS_SPM.AUTO_LIMIT VERIFY = YES COMMIT = YESPlan: SQL_PLAN_anx8padtfhx3z425cd488------------------------------------ Plan was verified: Time used .23 seconds. Plan passed performance criterion: 11.34 times better than baseline plan. Plan was changed to an accepted plan. Baseline Plan Test Plan Stats Ratio ------------- --------- ----------- Execution Status: COMPLETE COMPLETE Rows Processed: 1 1 Elapsed Time(ms): 2.976 .593 5.02 CPU Time(ms): 2.999 .555 5.4 Buffer Gets: 23 2 11.5 Physical Read Requests: 0 0 Physical Write Requests: 00 Physical Read Bytes: 0 0 Physical Write Bytes: 0 0 Executions: 1 1------------------------------------------------------------------------------- Report Summary-------------------------------------------------------------------------------Number of plans verified: 1Number of plans accepted: 1 <-- this plan is accepted.[oracle@linux2 trace]$ tkprof ora112_ora_1336.trc 1336.out-->in 1336.out, each plan executed 10 times********************************************************************************SQL ID: 2rcg7mtr7pb05Plan Hash: 0select count(*)from t1 where id=:idcall count cpu elapsed disk query current rows------- ------ -------- ---------- ---------- ---------- ---------- ----------Parse 1 0.00 0.00 0 0 0 0Execute 0 0.00 0.00 0 0 0 0Fetch 0 0.00 0.00 0 0 0 0------- ------ -------- ---------- ---------- ---------- ---------- ----------total 1 0.00 0.00 0 0 0 0Misses in library cache during parse: 1Optimizer mode: ALL_ROWSParsing user id: 66 (recursive depth: 1)********************************************************************************SQL ID: dyqpm6rnkjuksPlan Hash: 0select count(*)from t1 where id=:idcall count cpu elapsed disk query current rows------- ------ -------- ---------- ---------- ---------- ---------- ----------Parse 1 0.00 0.00 0 0 0 0Execute 0 0.00 0.00 0 0 0 0Fetch 0 0.00 0.00 0 0 0 0------- ------ -------- ---------- ---------- ---------- ---------- ----------total 1 0.00 0.00 0 0 0 0Misses in library cache during parse: 1Optimizer mode: ALL_ROWSParsing user id: 66 (recursive depth: 2)********************************************************************************SQL ID: 2rcg7mtr7pb05Plan Hash: 1634389831select count(*) from t1 where id=:idcall count cpu elapsed disk query current rows------- ------ -------- ---------- ---------- ---------- ---------- ----------Parse 1 0.00 0.00 0 0 0 0Execute 10 0.01 0.01 0 0 0 0Fetch 10 0.00 0.00 0 230 0 10------- ------ -------- ---------- ---------- ---------- ---------- ----------total 21 0.01 0.01 0 230 0 10Misses in library cache during parse: 1Misses in library cache during execute: 1Optimizer mode: ALL_ROWSParsing user id: 66 (recursive depth: 1)********************************************************************************SQL ID: 2rcg7mtr7pb05Plan Hash: 1113379976select count(*)from t1 where id=:idcall count cpu elapsed disk query current rows------- ------ -------- ---------- ---------- ---------- ---------- ----------Parse 1 0.00 0.00 0 0 0 0Execute 10 0.00 0.00 0 0 0 0Fetch 10 0.00 0.00 0 20 0 10------- ------ -------- ---------- ---------- ---------- ---------- ----------total 21 0.00 0.00 0 20 0 10Misses in library cache during parse: 1Misses in library cache during execute: 1Optimizer mode: ALL_ROWSParsing user id: 66 (recursive depth: 1)********************************************************************************in ora112_ora_1336.trc, the bind variable is 602, which is the bind value when the plan was captured as non-accepted plan.=====================PARSING IN CURSOR #5 len=59 dep=1 uid=66 oct=3 lid=66 tim=1258481193917203 hv=1853533189 ad='36483edc' sqlid='2rcg7mtr7pb05'/* SQL Analyze(1,0) */ select count(*) from t1 where id=:idEND OF STMTPARSE #5:c=0,e=107,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=1,plh=0,tim=1258481193917201BINDS #5: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0519f3ac bln=22 avl=03 flg=05 value=602EXEC #5:c=3000,e=2978,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=1,plh=1634389831,tim=1258481193922008FETCH #5:c=0,e=294,p=0,cr=23,cu=0,mis=0,r=1,dep=1,og=1,plh=1634389831,tim=1258481193922383BINDS #5: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0519f3ac bln=22 avl=03 flg=05 value=602EXEC #5:c=999,e=1043,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1634389831,tim=1258481193924088FETCH #5:c=1000,e=1127,p=0,cr=23,cu=0,mis=0,r=1,dep=1,og=1,plh=1634389831,tim=1258481193926154BINDS #5: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0519f3ac bln=22 avl=03 flg=05 value=602EXEC #5:c=1000,e=1034,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1634389831,tim=1258481193928065FETCH #5:c=0,e=222,p=0,cr=23,cu=0,mis=0,r=1,dep=1,og=1,plh=1634389831,tim=1258481193929282BINDS #5: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0519f3ac bln=22 avl=03 flg=05 value=602EXEC #5:c=1000,e=1032,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1634389831,tim=1258481193931064FETCH #5:c=1000,e=1062,p=0,cr=23,cu=0,mis=0,r=1,dep=1,og=1,plh=1634389831,tim=1258481193933088BINDS #5: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0519f3ac bln=22 avl=03 flg=05 value=602EXEC #5:c=1000,e=1027,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1634389831,tim=1258481193935058FETCH #5:c=0,e=221,p=0,cr=23,cu=0,mis=0,r=1,dep=1,og=1,plh=1634389831,tim=1258481193935319BINDS #5: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0519f3ac bln=22 avl=03 flg=05 value=602EXEC #5:c=1000,e=1032,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1634389831,tim=1258481193937062FETCH #5:c=0,e=218,p=0,cr=23,cu=0,mis=0,r=1,dep=1,og=1,plh=1634389831,tim=1258481193938245BINDS #5: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0519f3ac bln=22 avl=03 flg=05 value=602EXEC #5:c=2000,e=1963,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1634389831,tim=1258481193941015FETCH #5:c=0,e=224,p=0,cr=23,cu=0,mis=0,r=1,dep=1,og=1,plh=1634389831,tim=1258481193941278BINDS #5: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0519f3ac bln=22 avl=03 flg=05 value=602EXEC #5:c=1000,e=1025,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1634389831,tim=1258481193943055FETCH #5:c=0,e=218,p=0,cr=23,cu=0,mis=0,r=1,dep=1,og=1,plh=1634389831,tim=1258481193943312BINDS #5: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0519f3ac bln=22 avl=03 flg=05 value=602EXEC #5:c=1000,e=1073,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1634389831,tim=1258481193945104FETCH #5:c=0,e=221,p=0,cr=23,cu=0,mis=0,r=1,dep=1,og=1,plh=1634389831,tim=1258481193946240BINDS #5: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0519f3ac bln=22 avl=03 flg=05 value=602EXEC #5:c=1000,e=1031,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1634389831,tim=1258481193948062FETCH #5:c=0,e=219,p=0,cr=23,cu=0,mis=0,r=1,dep=1,og=1,plh=1634389831,tim=1258481193949245CLOSE #5:c=0,e=6,dep=1,type=0,tim=1258481193950037=====================...=====================PARSING IN CURSOR #19 len=59 dep=1 uid=66 oct=3 lid=66 tim=1258481193961637 hv=1853533189 ad='36483edc' sqlid='2rcg7mtr7pb05'/* SQL Analyze(1,0) */ select count(*) from t1 where id=:idEND OF STMTPARSE #19:c=0,e=121,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=1,plh=0,tim=1258481193961635BINDS #19: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0578769c bln=22 avl=03 flg=05 value=602EXEC #19:c=3000,e=2412,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=1,plh=1113379976,tim=1258481193964509FETCH #19:c=0,e=20,p=0,cr=2,cu=0,mis=0,r=1,dep=1,og=1,plh=1113379976,tim=1258481193964578BINDS #19: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0578769c bln=22 avl=03 flg=05 value=602EXEC #19:c=0,e=469,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1113379976,tim=1258481193965091FETCH #19:c=0,e=8,p=0,cr=2,cu=0,mis=0,r=1,dep=1,og=1,plh=1113379976,tim=1258481193965137BINDS #19: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0578769c bln=22 avl=03 flg=05 value=602EXEC #19:c=0,e=100,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1113379976,tim=1258481193965587FETCH #19:c=0,e=396,p=0,cr=2,cu=0,mis=0,r=1,dep=1,og=1,plh=1113379976,tim=1258481193966021BINDS #19: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0578769c bln=22 avl=03 flg=05 value=602EXEC #19:c=1000,e=413,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1113379976,tim=1258481193966476FETCH #19:c=0,e=7,p=0,cr=2,cu=0,mis=0,r=1,dep=1,og=1,plh=1113379976,tim=1258481193966520BINDS #19: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0578769c bln=22 avl=03 flg=05 value=602EXEC #19:c=0,e=466,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1113379976,tim=1258481193967027FETCH #19:c=0,e=8,p=0,cr=2,cu=0,mis=0,r=1,dep=1,og=1,plh=1113379976,tim=1258481193967073BINDS #19: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0578769c bln=22 avl=03 flg=05 value=602EXEC #19:c=999,e=413,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1113379976,tim=1258481193967525FETCH #19:c=0,e=8,p=0,cr=2,cu=0,mis=0,r=1,dep=1,og=1,plh=1113379976,tim=1258481193967571BINDS #19: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0578769c bln=22 avl=03 flg=05 value=602EXEC #19:c=0,e=466,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1113379976,tim=1258481193968077FETCH #19:c=0,e=11,p=0,cr=2,cu=0,mis=0,r=1,dep=1,og=1,plh=1113379976,tim=1258481193968126BINDS #19: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0578769c bln=22 avl=03 flg=05 value=602EXEC #19:c=0,e=102,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1113379976,tim=1258481193968580FETCH #19:c=0,e=7,p=0,cr=2,cu=0,mis=0,r=1,dep=1,og=1,plh=1113379976,tim=1258481193968625BINDS #19: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0578769c bln=22 avl=03 flg=05 value=602EXEC #19:c=1000,e=424,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1113379976,tim=1258481193969478FETCH #19:c=0,e=8,p=0,cr=2,cu=0,mis=0,r=1,dep=1,og=1,plh=1113379976,tim=1258481193969524BINDS #19: Bind#0oacdty=02 mxl=22(03) mxlc=00 mal=00 scl=00 pre=00oacflg=11 fl2=0000 frm=00 csi=00 siz=24 off=0kxsbbbfp=0578769c bln=22 avl=03 flg=05 value=602EXEC #19:c=0,e=469,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,plh=1113379976,tim=1258481193970033FETCH #19:c=0,e=8,p=0,cr=2,cu=0,mis=0,r=1,dep=1,og=1,plh=1113379976,tim=1258481193970090CLOSE #19:c=0,e=5,dep=1,type=0,tim=1258481193970137=====================
  • Use procedure: -- dbms_xplan.display_sql_plan_baselineTo identified plan baselines: -- sql_handle, display all plans of this sql -- plan_name, only display this sql plan -- sql_handle and plan_nameView: dba_sql_plan_baselinesselect sql_handle, plan_name, enabled, accepted, fixed from dba_sql_plan_baselines; 1. Display all plans of a SQL statement identified by the SQL handle 'SYS_SQL_2ba93fab2186a29d' using TYPICAL formatSQL> select sql_handle, plan_name, enabled, accepted, fixed from dba_sql_plan_baselines where sql_handle='SYS_SQL_2ba93fab2186a29d';SQL_HANDLE PLAN_NAME ENA ACC FIX------------------------------ ------------------------------ --- --- ---SYS_SQL_2ba93fab2186a29d SQL_PLAN_2ra9zpchsd8nx425cd488 YES YES NOSQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_2ba93fab2186a29d', format=>'TYPICAL'));PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_2ba93fab2186a29dSQL text: select count(*) from t1 where id=0----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_2ra9zpchsd8nx425cd488 Plan id: 1113379976Enabled: YES Fixed: NO Accepted: YES Origin: AUTO-CAPTURE--------------------------------------------------------------------------------PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------Plan hash value: 3695297570-----------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |-----------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 1 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | INDEX RANGE SCAN| T1_IDX1 | 1 | 4 | 1 (0)| 00:00:01 |-----------------------------------------------------------------------------Predicate Information (identified by operation id):PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------- 2 - access("ID"=0)25 rows selected.SQL> select sql_handle, plan_name, enabled, accepted, fixed from dba_sql_plan_baselines where sql_handle='SYS_SQL_2ba93fab2186a29d';SQL_HANDLE PLAN_NAME ENA ACC FIX------------------------------ ------------------------------ --- --- ---SYS_SQL_2ba93fab2186a29d SQL_PLAN_2ra9zpchsd8nx425cd488 YES YES NO2. Display all plans of one or more SQL statements containing the string 'from t1' using BASIC formatSQL> SELECT p.* 2 FROM (SELECT DISTINCT sql_handle FROM dba_sql_plan_baselines 3 WHERE sql_text like '%from t1%') spb, 4 TABLE(DBMS_XPLAN.DISPLAY_SQL_PLAN_BASELINE(spb.sql_handle, NULL, 5 'BASIC')) p;PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_2ba93fab2186a29dSQL text: select count(*) from t1 where id=0----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_2ra9zpchsd8nx425cd488 Plan id: 1113379976Enabled: YES Fixed: NO Accepted: YES Origin: AUTO-CAPTURE--------------------------------------------------------------------------------Plan hash value: 3695297570-------------------------------------| Id | Operation | Name |-------------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | INDEX RANGE SCAN| T1_IDX1 |---------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e208a16bb98b6a04SQL text: select count(*) from t1----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f4251dfwsquh4616acf47 Plan id: 1634389831Enabled: YES Fixed: NO Accepted: YES Origin: AUTO-CAPTURE--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------40 rows selected.
  • -- Trace itSQL> select spid from v$process where addr =(select paddr from v$session where sid=(select sid from v$mystat where rownum=1));SPID------------------------25914SQL> alter session set events '10046 trace name context forever, level 12';Session altered.SQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note'));PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: YES Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------20 rows selected.SQL> alter session set events '10046 trace name context off';Session altered.[oracle@linux2 trace]$ tkprof ora112_ora_25914.trc 25914.out--these sqls are identified as related********************************************************************************SQL ID: 874rw7r5rvnr4Plan Hash: 761652880SELECT (xmltype(od.comp_data).extract('/outline_data')).getClobVal() FROM sys.sqlobj$data od, sys.sqlobj$ so WHERE od.signature = :sig AND od.category = :cat AND od.obj_type = :obj_type AND so.name = :plan_name AND so.signature = od.signature AND so.category = od.category AND so.obj_type = od.obj_type AND so.plan_id = od.plan_id********************************************************************************SQL ID: a0v4v0zg0ca4hPlan Hash: 4118387986SELECT /*+ opt_param('parallel_execution_enabled', 'false') */ ID, POSITION, DEPTH, OPERATION, OPTIONS, OBJECT_NAME, CARDINALITY, BYTES, TEMP_SPACE, COST, IO_COST, CPU_COST, TIME, PARTITION_START, PARTITION_STOP, OBJECT_NODE, OTHER_TAG, DISTRIBUTION, PROJECTION, ACCESS_PREDICATES, FILTER_PREDICATES, NULL, QBLOCK_NAME, OBJECT_ALIAS, NVL(OTHER_XML, REMARKS), NULL SQL_PROFILE, NULL SQL_PLAN_BASELINE, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULLFROM TABLE(:B1 ) ********************************************************************************SQL ID: g3f3cw3zy5aatPlan Hash: 992138068SELECT PLAN_TABLE_OUTPUTFROM TABLE(CAST(DBMS_XPLAN.PREPARE_RECORDS(:B1 , :B2 ) AS SYS.DBMS_XPLAN_TYPE_TABLE))********************************************************************************SQL ID: cf19zu91tn7mjPlan Hash: 1388734953SELECT /*+ opt_param('parallel_execution_enabled', 'false') */ extractvalue(xmlval, '/*/info[@type = "sql_profile"]'), extractvalue(xmlval, '/*/info[@type = "sql_patch"]'), extractvalue(xmlval, '/*/info[@type = "baseline"]'), extractvalue(xmlval, '/*/info[@type = "outline"]'), extractvalue(xmlval, '/*/info[@type = "dynamic_sampling"]'), extractvalue(xmlval, '/*/info[@type = "dop"]'), extractvalue(xmlval, '/*/info[@type = "dop_reason"]'), extractvalue(xmlval, '/*/info[@type = "queuing_reason"]'), extractvalue(xmlval, '/*/info[@type = "row_shipping"]') , extractvalue(xmlval, '/*/info[@type = "index_size"]'), extractvalue(xmlval, '/*/info[@type = "result_checksum"]'), extractvalue(xmlval, '/*/info[@type = "cardinality_feedback"]'), extractvalue(xmlval,'/*/info[@type = "plan_hash"]')from (select xmltype(:v_other_xml) xmlval from dual)--this one will query sys.SQLOBJ$AUXDATA from the original trace fileSQL> desc sys.SQLOBJ$DATA Name Null? Type ----------------------------------------- -------- ---------------------------- SIGNATURE NOT NULL NUMBER CATEGORY NOT NULL VARCHAR2(30) OBJ_TYPE NOT NULL NUMBER PLAN_ID NOT NULL NUMBER COMP_DATA NOT NULL CLOB SPARE1 NUMBER SPARE2 CLOB SQL> desc sys.SQLOBJ$ Name Null? Type ----------------------------------------- -------- ---------------------------- SIGNATURE NOT NULL NUMBER CATEGORY NOT NULL VARCHAR2(30) OBJ_TYPE NOT NULL NUMBER PLAN_ID NOT NULL NUMBER NAME NOT NULL VARCHAR2(30) FLAGS NOT NULL NUMBER LAST_EXECUTED TIMESTAMP(6) SPARE1 NUMBER SPARE2 CLOB SQL> desc sys.SQLOBJ$AUXDATA Name Null? Type ----------------------------------------- -------- ---------------------------- SIGNATURE NOT NULL NUMBER CATEGORY NOT NULL VARCHAR2(30) OBJ_TYPE NOT NULL NUMBER PLAN_ID NOT NULL NUMBER DESCRIPTION VARCHAR2(500) CREATOR VARCHAR2(30) ORIGIN NOT NULL NUMBER VERSION VARCHAR2(64) CREATED NOT NULL TIMESTAMP(6) LAST_MODIFIED TIMESTAMP(6) LAST_VERIFIED TIMESTAMP(6) PARSE_CPU_TIME NUMBER OPTIMIZER_COST NUMBER MODULE VARCHAR2(48) ACTION VARCHAR2(32) PRIORITY NUMBER OPTIMIZER_ENV RAW(2000) BIND_DATA RAW(2000) PARSING_SCHEMA_NAME VARCHAR2(30) EXECUTIONS NUMBER ELAPSED_TIME NUMBER CPU_TIME NUMBER BUFFER_GETS NUMBER DISK_READS NUMBER DIRECT_WRITES NUMBER ROWS_PROCESSED NUMBER FETCHES NUMBER END_OF_FETCH_COUNT NUMBER TASK_ID NUMBER TASK_EXEC_NAME VARCHAR2(30) TASK_OBJ_ID NUMBER TASK_FND_ID NUMBER TASK_REC_ID NUMBER FLAGS NUMBER SPARE1 NUMBER SPARE2 CLOB
  • Use procedure: dbms_spm.alter_sql_plan_baselineTo identified plan baselines: -- sql_handle or plan_name or both Set attribute: -- enabled, set it to 'NO' if don't want optimizer use it -- fixed, set it to 'YES' if want optimizer always use itDBMS_SPM.ALTER_SQL_PLAN_BASELINE ( sql_handle IN VARCHAR2 := NULL, plan_name IN VARCHAR2 := NULL,attribute_name IN VARCHAR2,attribute_value IN VARCHAR2) RETURN PLS_INTEGER;SQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note')); PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------20 rows selected.SQL> exec :cnt:=dbms_spm.alter_sql_plan_baseline(sql_handle=>'SYS_SQL_e1d5d446a7219dcb', PLAN_NAME=>'SQL_PLAN_f3pfn8umk37fb616acf47',attribute_name=>'fixed',attribute_value=>'YES');PL/SQL procedure successfully completed.SQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note'));PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: YES Accepted: YES Origin: MANUAL-LOAD -- it is fixed --------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------20 rows selected.
  • --For a non-accepted plan, even modify it to fixed plan, it will not be used.SQL> exec :cnt:=dbms_spm.alter_sql_plan_baseline(sql_handle=>'SYS_SQL_e1d5d446a7219dcb', PLAN_NAME=>'SQL_PLAN_f3pfn8umk37fb425cd488',attribute_name=>'fixed',attribute_value=>'YES');PL/SQL procedure successfully completed.SQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note')); PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb425cd488 Plan id: 1113379976Enabled: YES Fixed: YES Accepted: NO Origin: AUTO-CAPTURE--------------------------------------------------------------------------------Plan hash value: 3695297570-------------------------------------| Id | Operation | Name |-------------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | INDEX RANGE SCAN| T1_IDX1 |---------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------35 rows selected.SQL> show parameter sql_planNAME TYPE VALUE------------------------------------ ----------- ------------------------------optimizer_capture_sql_plan_baselines boolean FALSEoptimizer_use_sql_plan_baselines boolean TRUESQL> set autotracetraceonlySQL> select count(*) from t1 where id=100;Execution Plan----------------------------------------------------------Plan hash value: 3724264953---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 7 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | TABLE ACCESS FULL| T1 | 1 | 4 | 7 (0)| 00:00:01 |---------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - filter("ID"=100)Note----- - SQL plan baseline "SQL_PLAN_f3pfn8umk37fb616acf47" used for this statement
  • Use procedure: -- dbms_spm.drop_sql_plan_baselineTo identified plan baselines: -- sql_handle or plan_name or both DBMS_SPM.DROP_SQL_PLAN_BASELINE ( sql_handle IN VARCHAR2 := NULL, plan_name IN VARCHAR2 := NULL)RETURN PLS_INTEGER;SQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note')); PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb425cd488 Plan id: 1113379976Enabled: YES Fixed: NO Accepted: YES Origin: AUTO-CAPTURE--------------------------------------------------------------------------------Plan hash value: 3695297570-------------------------------------| Id | Operation | Name |-------------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | INDEX RANGE SCAN| T1_IDX1 |---------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------35 rows selected.SQL> exec :cnt:= dbms_spm.drop_sql_plan_baseline(sql_handle=>'SYS_SQL_e1d5d446a7219dcb', PLAN_NAME=>'SQL_PLAN_f3pfn8umk37fb425cd488');PL/SQL procedure successfully completed.SQL> print :cnt CNT---------- 1SQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note')); PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953-----------------------------------| Id | Operation | Name |-----------------------------------| 0 | SELECT STATEMENT | || 1 | SORT AGGREGATE | || 2 | TABLE ACCESS FULL| T1 |-----------------------------------20 rows selected.
  • SQL Plan SelectionIf you are using automatic plan capture, the first time that a SQL statement is recognized as repeatable, its best-cost plan is added to the corresponding SQL plan baseline. That plan is then used to execute the statement. The optimizer uses a comparative plan selection policy when a plan baseline exists for a SQL statement and the initialization parameter OPTIMIZER_USE_SQL_PLAN_BASELINES is set to TRUE (default value). Each time a SQL statement is compiled, the optimizer first uses the traditional cost-based search method to build a best-cost plan. Then it tries to find a matching plan in the SQL plan baseline. If a match is found, it proceeds as usual. If no match is found, it first adds the new plan to the plan history, then costs each of the accepted plans in the SQL plan baseline, and picks the one with the lowest cost. The accepted plans are reproduced using the outline that is stored with each of them. So the effect of having a SQL plan baseline for a SQL statement is that the optimizer always selects one of the accepted plans in that SQL plan baseline. With SQL Plan Management, the optimizer can produce a plan that could be either a best-cost plan or a baseline plan. This information is dumped in the other_xml column of the plan_table upon explainplan.In addition, you can use the new dbms_xplain.display_sql_plan_baseline function to display one or more execution plans for the specified sql_handle of a plan baseline. If plan_name is also specified, the corresponding execution plan is displayed.
  • --1.Statistics change influence the choose of accepted planSQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'TYPICAL'));PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb425cd488 Plan id: 1113379976Enabled: YES Fixed: NO Accepted: YES Origin: AUTO-CAPTURE--------------------------------------------------------------------------------Plan hash value: 3695297570-----------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |-----------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 1 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | INDEX RANGE SCAN| T1_IDX1 | 1 | 4 | 1 (0)| 00:00:01 |-----------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("ID"=100)--------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 7 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | TABLE ACCESS FULL| T1 | 1 | 4 | 7 (0)| 00:00:01 |---------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - filter("ID"=100)45 rows selected.SQL> set autotracetraceonlySQL> select count(*) from t1 where id=100;Execution Plan----------------------------------------------------------Plan hash value: 3695297570-----------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |-----------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 1 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | INDEX RANGE SCAN| T1_IDX1 | 1 | 4 | 1 (0)| 00:00:01 |-----------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("ID"=100)Note----- - SQL plan baseline "SQL_PLAN_f3pfn8umk37fb425cd488" used for this statementStatistics---------------------------------------------------------- 12 recursive calls 15 db block gets 11 consistent gets 0 physical reads 3076 redo size 422 bytes sent via SQL*Net to client 415 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed--> Before change statistics, it uses plan SQL_PLAN_f3pfn8umk37fb425cd488 SQL> select column_name,HISTOGRAM from dba_tab_columns where table_name='T1';COLUMN_NAME HISTOGRAM------------------------------ ---------------ID NONESQL> select BLEVEL,LEAF_BLOCKS,DISTINCT_KEYS,AVG_LEAF_BLOCKS_PER_KEY,NUM_ROWS from dba_indexes where index_name='T1_IDX1'; BLEVEL LEAF_BLOCKS DISTINCT_KEYS AVG_LEAF_BLOCKS_PER_KEY NUM_ROWS---------- ----------- ------------- ----------------------- ---------- 1 21 10000 1 10000SQL> exec DBMS_STATS.SET_INDEX_STATS (ownname=>user,indname=>'T1_IDX1',numdist=>1,numrows=>1000,numlblks=>1000,avglblk=>1000,avgdblk=>1000,clstfct=>1000,indlevel=>3);PL/SQL procedure successfully completed.SQL> select BLEVEL,LEAF_BLOCKS,DISTINCT_KEYS,AVG_LEAF_BLOCKS_PER_KEY,NUM_ROWS,AVG_LEAF_BLOCKS_PER_KEY,AVG_DATA_BLOCKS_PER_KEY from dba_indexes where index_name='T1_IDX1'; BLEVEL LEAF_BLOCKS DISTINCT_KEYS AVG_LEAF_BLOCKS_PER_KEY NUM_ROWS AVG_LEAF_BLOCKS_PER_KEY AVG_DATA_BLOCKS_PER_KEY---------- ----------- ------------- ----------------------- --------------------------------- ----------------------- 3 1000 1 1000 1000 1000 1000SQL> select NUM_ROWS,BLOCKS from dba_tables where table_name='T1'; NUM_ROWS BLOCKS---------- ---------- 10000 20SQL> exec DBMS_STATS.SET_TABLE_STATS (ownname=>user,tabname=>'T1',numrows=>1,numblks=>1,no_invalidate=>FALSE);PL/SQL procedure successfully completed.SQL> select NUM_ROWS,BLOCKS from dba_tables where table_name='T1'; NUM_ROWS BLOCKS---------- ---------- 1 1--after change statistics, the cost is changedSQL> select * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'TYPICAL'));PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_e1d5d446a7219dcbSQL text: select count(*) from t1 where id=100----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb425cd488 Plan id: 1113379976Enabled: YES Fixed: NO Accepted: YES Origin: AUTO-CAPTURE--------------------------------------------------------------------------------Plan hash value: 3695297570-----------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |-----------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 4 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | INDEX RANGE SCAN| T1_IDX1 | 1 | 4 | 4 (0)| 00:00:01 |-----------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("ID"=100)--------------------------------------------------------------------------------Plan name: SQL_PLAN_f3pfn8umk37fb616acf47 Plan id: 1634389831Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3724264953---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 2 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | TABLE ACCESS FULL| T1 | 1 | 4 | 2 (0)| 00:00:01 |---------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - filter("ID"=100)45 rows selected.SQL> set autotracetraceonlySQL> select count(*) from t1 where id=100;Execution Plan----------------------------------------------------------Plan hash value: 3724264953---------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 4 | 2 (0)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 4 | | ||* 2 | TABLE ACCESS FULL| T1 | 1 | 4 | 2 (0)| 00:00:01 |---------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - filter("ID"=100)Note----- - SQL plan baseline "SQL_PLAN_f3pfn8umk37fb616acf47" used for this statementStatistics---------------------------------------------------------- 1 recursive calls 0 db block gets 23 consistent gets 0 physical reads 0 redo size 422 bytes sent via SQL*Net to client 415 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed--> After change statistics, it uses plan SQL_PLAN_f3pfn8umk37fb616acf47, different from previous SQL_PLAN_f3pfn8umk37fb425cd488
  • Maintenance retentionMigrate stored outline to Sql plan baselinesExport/Import Sql plan baselinesImplement function like 'oraolexchange'
  • USE SPM to capture the hinted execution plan and associate it with the non-hinted SQL statement.1. Run the non-hinted SQL2. Find its SQL_ID from V$SQL3. Using the SQL_ID create a SQL plan baseline for the statement. 4. Find SQL_HANDLE & PLAN_NAME--Example:SQL> select * from table(dbms_xplan.display_sql_plan_baseline(sql_handle=>'SYS_SQL_1a9420dc6aa256d8')); PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_1a9420dc6aa256d8SQL text: select count(*) from t1, t2 where t1.id=t2.id----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_1p510vjpa4pqsb3e28116 Plan id: 3017965846Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3975918519----------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 17 | 15 (7)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 17 | | ||* 2 | HASH JOIN | | 1634 | 27778 | 15 (7)| 00:00:01 || 3 | INDEX FAST FULL SCAN| T1_IDX1 | 10000 | 40000 | 7 (0)| 00:00:01 || 4 | TABLE ACCESS FULL | T2 | 1634 | 21242 | 7 (0)| 00:00:01 |----------------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("T1"."ID"="T2"."ID")27 rows selected.--create a new plan with hintSQL> select/*+ full(t1) */ count(*) from t1, t2 where t1.id=t2.id; COUNT(*)---------- 100001 row selected.SQL> select * from table(dbms_xplan.display_cursor);PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------------------------------SQL_ID 3v17y0gy90rtw, child number 0-------------------------------------select/*+ full(t1) */ count(*) from t1, t2 where t1.id=t2.idPlan hash value: 906334482----------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | 15 (100)| || 1 | SORT AGGREGATE | | 1 | 17 | | ||* 2 | HASH JOIN | | 10000 | 166K| 15 (7)| 00:00:01 || 3 | TABLE ACCESS FULL| T1 | 10000 | 40000 | 7 (0)| 00:00:01 || 4 | TABLE ACCESS FULL| T2 | 10000 | 126K| 7 (0)| 00:00:01 |----------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("T1"."ID"="T2"."ID")Note----- - dynamic sampling used for this statement (level=2)25 rows selected.--load the new plan to the original sqlSQL> exec :cnt := dbms_spm.load_plans_from_cursor_cache(sql_handle=>'SYS_SQL_1a9420dc6aa256d8',sql_id=>'3v17y0gy90rtw',plan_hash_value=>906334482); PL/SQL procedure successfully completed.SQL> select * from table(dbms_xplan.display_sql_plan_baseline(sql_handle=>'SYS_SQL_1a9420dc6aa256d8')); PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------SQL handle: SYS_SQL_1a9420dc6aa256d8SQL text: select count(*) from t1, t2 where t1.id=t2.id----------------------------------------------------------------------------------------------------------------------------------------------------------------Plan name: SQL_PLAN_1p510vjpa4pqsb3e28116 Plan id: 3017965846Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 3975918519----------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 17 | 15 (7)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 17 | | ||* 2 | HASH JOIN | | 1634 | 27778 | 15 (7)| 00:00:01 || 3 | INDEX FAST FULL SCAN| T1_IDX1 | 10000 | 40000 | 7 (0)| 00:00:01 || 4 | TABLE ACCESS FULL | T2 | 1634 | 21242 | 7 (0)| 00:00:01 |----------------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("T1"."ID"="T2"."ID")--------------------------------------------------------------------------------Plan name: SQL_PLAN_1p510vjpa4pqsc6a45b88 Plan id: 3332660104Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD--------------------------------------------------------------------------------Plan hash value: 906334482----------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 17 | 15 (7)| 00:00:01 || 1 | SORT AGGREGATE | | 1 | 17 | | ||* 2 | HASH JOIN | | 1634 | 27778 | 15 (7)| 00:00:01 || 3 | TABLE ACCESS FULL| T1 | 10000 | 40000 | 7 (0)| 00:00:01 || 4 | TABLE ACCESS FULL| T2 | 1634 | 21242 | 7 (0)| 00:00:01 |----------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("T1"."ID"="T2"."ID")49 rows selected.
  • Reference:http://download.oracle.com/docs/cd/E11882_01/server.112/e10821/optplanmgmt.htm#BABEAFGGReference sql used in test:--evolveexec :rpt := dbms_spm.evolve_sql_plan_baseline(sql_handle => 'SYS_SQL_e1d5d446a7219dcb') ;--modifyexec :cnt:=dbms_spm.alter_sql_plan_baseline(sql_handle=>'SYS_SQL_e1d5d446a7219dcb', PLAN_NAME=>'SQL_PLAN_f3pfn8umk37fb425cd488',attribute_name=>'fixed',attribute_value=>'YES');--displayselect * from table( dbms_xplan.display_sql_plan_baseline( sql_handle=>'SYS_SQL_e1d5d446a7219dcb',format=>'basic +note'));select sql_handle, plan_name, enabled, accepted, fixed,sql_text from dba_sql_plan_baselines where sql_text like '%id=504%' and sql_text not like '%dba_sql_plan_baselines%'; --dropexec :cnt:= dbms_spm.drop_sql_plan_baseline(sql_handle=>'SYS_SQL_e1d5d446a7219dcb', PLAN_NAME=>'SQL_PLAN_f3pfn8umk37fb425cd488');--othersselect spid from v$process p, v$session s, v$mystat m where p.addr=s.paddr and s.sid=m.sid and rownum=1;alter session set events '10046 trace name context forever, level 12';alter session set events '10046 trace name context off';
  • Oracle11g sql plan management

    1. 1. Oracle11g SQL Plan Management 2009-11-16Alex Zeng
    2. 2. Agenda What is SQL Plan Management?  SQL Plan Management concept  Parameters, Packages and Views involved Why use SQL Plan Management?  When it helps  When it can NOT help How to use SPM?  Capture (Load)  Evolve (Verify)  Display  Modify and Drop  How Oracle choose plans Misc. Q/A
    3. 3. Agenda What is SQL Plan Management?  SQL Plan Management concept  Parameters, Packages and Views involved Why use SQL Plan Management?  When it helps  When it can NOT help How to use SPM?  Capture (Load)  Evolve (Verify)  Display  Modify and Drop  How Oracle choose plans Misc. Q/A
    4. 4. SQL Plan Management (SPM) Concept SQL Plan Management Parameters Packages Automatically capture Views SQL Plan Repeatable Base SQL Sql Profiles Automatic SQL Tuning task SQL Plan History Not Accepted Plans SQL PlanManually Load BaselineSQL Fixed Manually Accepted verify Plan Accepted Plans Plans
    5. 5. Parameters, Packages and Viewsinvolved • optimizer_capture_sql_plan_baselines (FALSE) Parameters • optimizer_use_sql_plan_baselines (TRUE) • _sql_plan_management_control (0) • DBMS_SPM: Load, Evolve, Alter, Drop, etc. Packages • DBMS_XPLAN.DISPLAY_SQL_PLAN_BASELINE Views • DBA_SQL_PLAN_BASELINES
    6. 6. Agenda What is SQL Plan Management?  SQL Plan Management concept  Parameters, Packages and Views involved Why use SQL Plan Management?  When it helps  When it can NOT help How to use SPM?  Capture (Load)  Evolve (Verify)  Display  Modify and Drop  How Oracle choose plans Misc. Q/A
    7. 7. Why use SQL Plan Management? • Compare to • Manually reactive tuning load, evolve technique, such • Automatically as load, evolve outline, hint, SQ • Can disable L profile plan Proactive Total tuning controllable Stable SQL Plan based plan • Only CBO is • Compare to hint uncertain. based • Do NOT change technique, SQL when system, session, o Profile etc. bject or statistics changes
    8. 8. Why use SQL Plan Management? -contd. When it helps √ Use new optimizer version stabilize SQL plan √ Change optimizer statistics or optimizer parameters √ Change schema or metadata definitions √ Change system settings √ Create SQL profile
    9. 9. Why use SQL Plan Management? -contd. When it can NOT help An event has caused irreversible execution plan changes Example: dropping an index used by the SQL Plan
    10. 10. Agenda What is SQL Plan Management?  SQL Plan Management concept  Parameters, Packages and Views involved Why use SQL Plan Management?  When it helps  When it can NOT help How to use SPM?  Capture (Load)  Evolve (Verify)  Display  Modify and Drop  How Oracle choose plans Misc. Q/A
    11. 11. How to use SPM Evolve (Verify) Capture Display (Load) Drop Modify
    12. 12. Capture (Load) OPTIMIZER_CAPTURE_SQL_PLAN_BASELINES=TRUE dbms_spm Plan history Plan history GB 1 HJ HJ Manually GB Auto Load HJCapture HJ GB HJ 2 HJ GB HJ 3 HJ Staging Cursor table cache Plan history GB 4 HJ HJ DBA
    13. 13. Automatically CaptureSQL> Alter session set optimizer_capture_sql_plan_baselines = TRUE
    14. 14. Automatically Capture - whatsbehind? Physical storage: Oracle will store the SQL history in physical table. If you run a SQL once and then restart instance, run it again. This SQL will be considered repeatable SQL and the second plan will be captured Overhead: Compare to disable auto-capture, the first run overhead, are (1 SELECT + 1 INSERT) on system tables; the second run overhead are (3 SELECT + 5 INSERT); TEXT-baseOracle will capture the SQL plan if the SQL text is the same regardless of whether the visited table are same or not.
    15. 15. Manually load • LOAD_PLANS_FROM_CURSOR_CACH E, Input options to identify SQLS: Load From Cursor • sql_id, (sql_text), (plan_hash_value) • sql_id, sql_handle , (plan_hash_value) Cache • sql_text, parsing_schema_name, module, action Load From SQL Tuning • LOAD_PLANS_FROM_SQLSET, Input value: Sets(STS) • sqlset_name • First Load plans in AWR to STS Load From AWR • Then use DBMS_SPM. snapshots LOAD_PLANS_FROM_SQLSETexec :cnt :=DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE(sql_id=>9xgjm73);
    16. 16. Manually load contd. -- Behavior withdifferent setting If a SQL has only non-fixed baseline, new plan will *ALWAYS* be captured, no matter optimizer_capture_sql_plan_baselines is FALSE or TRUE, but execute the accepted plan. If a SQL has a fixed plan baseline, new plan will *NEVER* be capture automatically, no matter optimizer_capture_sql_plan_baselines is TRUE or FALSE, also execute the fixed plan. The behavior of auto-capture doesnt change when set optimizer_use_sql_plan_baselines to FALSE.
    17. 17. Agenda What is SQL Plan Management?  SQL Plan Management concept  Parameters, Packages and Views involved Why use SQL Plan Management?  When it helps  When it can NOT help How to use SPM?  Capture (Load)  Evolve (Verify)  Display  Modify and Drop  How Oracle choose plans Misc. Q/A
    18. 18. Evolve (verify) Procedure • EVOLVE_SQL_PLAN_BASELINE • no input: evolve all non-accepted plansInput variables • sql_handle: evolve this sqls all non-acceptedto identify SQL plans • plan_name: evolve this plan only Plan • plan_list, evolve a list of sql plan • verify (YES)Other variables • commit (YES) • time_limit (minutes)SQL> exec :rpt := DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE() ;
    19. 19. Evolve (verify) contd. – How oracleevolve? If verified is set to YES(default), Oracle will execute non-accepted SQL plan and the best accepted SQL plan, then compare the real cost. When evolve SQL plan with bind variables, oracle will use the bind variable value when the plan was captured. In my test, each plan executed 10 times 
    20. 20. Agenda What is SQL Plan Management?  SQL Plan Management concept  Parameters, Packages and Views involved Why use SQL Plan Management?  When it helps  When it can NOT help How to use SPM?  Capture (Load)  Evolve (Verify)  Display  Modify and Drop  How Oracle choose plans Misc. Q/A
    21. 21. Display • DBA_SQL_PLAN_BASELINES View • select sql_handle, plan_name, enabled, accepted, fixed from dba_sql_plan_baselines; • DBMS_XPLAN.DISPLAY_SQL_PLAN_BASELINE • Select * from Procedure table(DBMS_XPLAN.DISPLAY_SQL_PLAN_BASELINE( <sql_handle>); • sql_handle, display all plans of this SQLInput variables to • plan_name, only display this SQL planidentify SQL Plan • sql_handle and plan_nameSQL> select p.* from (select distinct sql_handle from DBA_SQL_PLAN_BASELINES wheresql_text like %from t1%)spb, table(DBMS_XPLAN.DISPLAY_SQL_PLAN_BASELINE(spb.sql_handle, NULL, BASIC)) p;
    22. 22. Display contd. - Whats behind? The plan baselines are stored in 3 tables in tablespace SYSAUX sys.SQLOBJ$DATA sys.SQLOBJ$ sys.SQLOBJ$AUXDATA
    23. 23. Modify Procedure • ALTER_SQL_PLAN_BASELINE • sql_handleInput variables to • plan_nameidentify SQL Plan • sql_handle and plan_name • enabled, set it to NO if dont want optimizer use itModify attributes • fixed, set it to YES if want optimizer choose it as a prioritySQL> exec :cnt:=DBMS_SPM.ALTER_SQL_PLAN_BASELINE(sql_handle=>SYS_SQL_e1d5d446a7219dcb, PLAN_NAME=>SQL_PLAN_f3pfn8umk37fb616acf47,attribute_name=>fixed,attribute_value=>YES);
    24. 24. Modify cont. It can NOT modify accepted status. Disable or drop it if dont want to use it. Call DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE with verify to NO if want to change it to accepted directly For a non-accepted plan, even modify it to fixed plan, it will not be used.
    25. 25. DropProcedure • DROP_SQL_PLAN_BASELINE • sql_handle Input • plan_name variables • sql_handle and plan_nameSQL> exec :cnt:= DBMS_SPM.DROP_SQL_PLAN_BASELINE(sql_handle=>SYS_SQL_e1d5d446a7219dcb, PLAN_NAME=>SQL_PLAN_f3pfn8umk37fb425cd488);
    26. 26. Agenda What is SQL Plan Management?  SQL Plan Management concept  Parameters, Packages and Views involved Why use SQL Plan Management?  When it helps  When it can NOT help How to use SPM?  Capture (Load)  Evolve (Verify)  Display  Modify and Drop  How Oracle choose plans Misc. Q/A
    27. 27. How Oracle choose plans Yes No GB optimizer_use_sql_ Any fixed Plan? Plan part HJ plan_baselines=true? Yes of history? No HJ Yes Plan history Yes Select fixed baseline plan Plan No with lowest best-cost baseline GB GB GB GB Yes HJ HJ … HJ HJ HJ HJ HJ Plan part HJ of baseline? No No Select baseline plan with lowest best-cost GB GB GB GB Yes No HJ HJ > HJ HJ HJ HJ HJ HJ
    28. 28. How Oracle choose plans – contd. One Sql can have multi-accepted plans. One Sql can have multi-fixed plans. Statistics will influence the choice when choose SQL Plan between accepted plans. So don’t have too many accepted plan if dislike nightmare.
    29. 29. Agenda What is SQL Plan Management?  SQL Plan Management concept  Parameters, Packages and Views involved Why use SQL Plan Management?  When it helps  When it can NOT help How to use SPM?  Capture (Load)  Evolve (Verify)  Display  Modify and Drop  How Oracle choose plans Misc. Q/A
    30. 30. Misc. Migrate stored outline to SQL plan baselines Export/Import Maintenance SQL plan retention baselines Implement function like ora olexchange
    31. 31. Maintenance retention Stored in SYSAUX, space maximum usage default 10%SQL> exec DBMS_SPM.CONFIGURE(space_budget_percent,30); Retention period default 53 weeks, using LAST_EXECUTED timestamp in DBA_SQL_PLAN_BASELINES to get expired plansSQL> exec DBMS_SPM.CONFIGURE( plan_retention_weeks,105); Check value:SQL> select * from DBA_SQL_MANAGEMENT_CONFIG;
    32. 32. Migrate stored outline to Sql planbaselines Use DBMS_SPM.MIGRATE_STORED_OUTLINESQL> exec :rpt:= DBMS_SPM.MIGRATE_STORED_OUTLINE( attribute_name => category, attribute_value => good_plan, fixed => YES ); After verify, drop migrated outlineSQL>exec :cnt:=DBMS_SPM.DROP_MIGRATED_STORED_OUTLINE (); (11.2 new)
    33. 33. Export/Import Sql plan baselines Create stage tableSQL> exec DBMS_SPM.CREATE_STGTAB_BASELINE(table_name => TMP1, tablespace_name=>CR_DATA); Pack baselines into stage tableSQL> exec :cnt:=DBMS_SPM.PACK_STGTAB_BASELINE(table_name => TMP1); Copy it to target db and unpack itSQL> exec :cnt:=DBMS_SPM.UNPACK_STGTAB_BASELINE(table_name => TMP1);
    34. 34. Bind hinted SQL plan to non-hintedone Oracle supported Process • select count(*) from t1, t2 where t1.id=t2.idRun SQL • select SQL_ID from V$SQL where sql_text like select count(*) GET from t1, t2 where t1.id=t2.id SQL_ID • sql_id chj6q8z7ykbyy • Using the SQL_ID create a SQL plan baseline • exec :cnt Create :=dbms_spm.load_plans_from_cursor_cache(sql_id=>chj6q8z7ykbaseline byy); • Find SQL_HANDLE & PLAN_NAME • SQL> SELECT sql_handle, plan_name, enabled FROMFind it in dba_sql_plan_baselines where sql_text like %t1.id=t2.id%;baseline • SQL_HANDLE: SYS_SQL_1a9420dc6aa256d8 • PLAN_NAME: SQL_PLAN_1p510vjpa4pqsb3e28116 YES
    35. 35. Bind hinted SQL plan to non-hintedone -contd. • SQL> exec :cnt :=DBMS_SPM.ALTER_SQL_PLAN_BASELINE( SQL_HANDLE => SYS_SQL_1a9420dc6aa256d8, PLAN_NAME Disable => SQL_PLAN_1p510vjpa4pqsb3e28116, ATTRIBUTE_NAME Baseline => enabled, ATTRIBUTE_VALUE => NO); • select/*+ full(t1) */ count(*) from t1, t2 whereRun hinted t1.id=t2.id; SQL • select * from table(dbms_xplan.display_cursor); -- OR Get NEW • select sql_id, plan_hash_value from V$SQL where sql_text SQL_ID like %/*+full(t1) %; andPLAN_HAS • SQL_ID: 3v17y0gy90rtw Plan hash value: 906334482H_VALUE
    36. 36. Bind hinted SQL plan to non-hintedone -contd. • exec :cnt :=DBMS_SPM.LOAD_PLANS_FROM _CURSOR_CACHE new SQL_ID and (sql_handle=>SYS_SQL_1a9420dc6aa256d8PLAN_HASH_VAL , sql_id=>3v17y0gy90rtw, plan_ UE WORKS hash_value=>906334482); BOTH with old WORKS SQL_HANDLE • exec :cnt := DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE new SQL_ID and (sql_text=> select count(*) from t1, t2 wherePLAN_HASH_VAL t1.id=t2.id, sql_id=>3v17y0gy90rtw, plan_hash_value=> UE 906334482); with old SQL_TEXT
    37. 37. Agenda What is SQL Plan Management?  SQL Plan Management concept  Parameters, Packages and Views involved Why use SQL Plan Management?  When it helps  When it can NOT help How to use SPM?  Capture (Load)  Evolve (Verify)  Display  Modify and Drop  How Oracle choose plans Misc. Q/A
    38. 38. Q/A

    ×