Oracle 固定执行计划--sqlprofile

步骤一-------------------------创建测试表,根据DBA_OBJECTS创建,OBJECT_ID上有索引
Create table test as select * from dba_objects;
create index t_1 on test(test_id);
 
步骤二-------------------------查看SQL默认执行计划,走了索引
explain plan for select * from test where object_id= 172;
select * from table(dbms_xplan.display(null,null,'outline')); ----通过指定outline可以获取到系统为我们生成的hint
-----------------------------------------------
| Id  | Operation                   | Name    |
-----------------------------------------------
|   0 | SELECT STATEMENT            |         |
|   1 |  TABLE ACCESS BY INDEX ROWID| TEST |
|*  2 |   INDEX RANGE SCAN          | T_1     |
-----------------------------------------------
Outline Data
-------------
 
  /*+
      BEGIN_OUTLINE_DATA
      INDEX_RS_ASC(@"SEL$1" "TEST"@"SEL$1" ("TEST"."OBJECT_ID"))
      OUTLINE_LEAF(@"SEL$1")
      ALL_ROWS
      DB_VERSION('11.1.0.7')
      OPTIMIZER_FEATURES_ENABLE('11.1.0.7')
      IGNORE_OPTIM_EMBEDDED_HINTS
      END_OUTLINE_DATA
  */
 
步骤3-------------------------如果我们想让它走全表扫描,获取全表扫描HINT
explain plan for select /*+ full(test) */* from test where object_id= 172;-----------增加HINT
select * from table(dbms_xplan.display(null,null,'outline'));------可以看到全表扫描的hint已经为我们生成了,我们选取必要的hint就OK了,其他的可以不要
-------------------------------------
| Id  | Operation         | Name    |
-------------------------------------
|   0 | SELECT STATEMENT  |         |
|*  1 |  TABLE ACCESS FULL| TEST |
-------------------------------------
Outline Data
-------------
 
  /*+
      BEGIN_OUTLINE_DATA
     FULL(@"SEL$1" "TEST"@"SEL$1")
      OUTLINE_LEAF(@"SEL$1")
      ALL_ROWS
      DB_VERSION('11.1.0.7')
      OPTIMIZER_FEATURES_ENABLE('11.1.0.7')
      IGNORE_OPTIM_EMBEDDED_HINTS
      END_OUTLINE_DATA
  */
 
步骤4--------------------------使用sql profile
declare
  v_hints sys.sqlprof_attr;
begin
  v_hints := sys.sqlprof_attr('FULL(@"SEL$1" "TEST"@"SEL$1")'); ----------从上面Outline Data部分获取到的HINT
  dbms_sqltune.import_sql_profile('select * from test where object_id= 172', ----------SQL语句部分
                                  v_hints,
                                  'TEST', --------------------------------PROFILE 的名字
                                  force_match => true);
end;
/
 
步骤五-------------------------查看是否生效,已经生效了
explain plan for select * from test where object_id= 172;
select * from table(dbms_xplan.display(null,null,'outline'));
--------------------------------------
| Id  | Operation         | Name    |
--------------------------------------
|   0 | SELECT STATEMENT  |         |
|*  1 |  TABLE ACCESS FULL| WXH_TBD |
--------------------------------------
Note
-----
   - SQL profile "TEST" used for this statement


步骤六-------------------------删除sql_profile,执行计划用的是未改变slqprofile 之前的执行计划

SQL> BEGIN
  DBMS_SQLTUNE.DROP_SQL_PROFILE(name => 'TEST');
END;

你可能感兴趣的:(Oracle 固定执行计划--sqlprofile)