Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6747
  • 博文数量: 4
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 40
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-03 23:48
文章分类

全部博文(4)

文章存档

2015年(1)

2014年(3)

我的朋友
最近访客

分类: Oracle

2014-12-27 23:43:17

This test is about fixing execution plan by using sql profile. 

By default, the sql is using index unique scan. By adding sql profile, the sql will be forced to use full table 

step 1:  create test table and index

  1. SQL> conn cloudapp/cloudapp
  2. Connected.
  3. SQL> create table t as select * from dba_objects where object_id is not null;

  4. Table created.

  5. SQL> alter table t add constraint pk_obj_id primary key(object_id);

  6. Table altered.

  7. SQL> exec dbms_stats.gather_table_stats(OWNNAME=>'CLOUDAPP',TABNAME=>'T',cascade => true);

  8. PL/SQL procedure successfully completed.

step2: Show the original execution plan, by default, it uses index unique scan

  1. SQL> var x number;
  2. SQL> exec :x := 28;
  3. SQL> select * from t where object_id=:x;
  4. ..
  5. SQL> select * from table(dbms_xplan.display_cursor('9r2gykqt5j1ss',0,'outline'));

  6. PLAN_TABLE_OUTPUT
  7. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  8. SQL_ID 9r2gykqt5j1ss, child number 0
  9. -------------------------------------
  10. select * from t where object_id=:x

  11. Plan hash value: 3004845637

  12. -----------------------------------------------------------------------------------------
  13. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
  14. -----------------------------------------------------------------------------------------
  15. | 0 | SELECT STATEMENT | | | | 2 (100)| |
  16. | 1 | TABLE ACCESS BY INDEX ROWID| T | 1 | 98 | 2 (0)| 00:00:01 |
  17. |* 2 | INDEX UNIQUE SCAN | PK_OBJ_ID | 1 | | 1 (0)| 00:00:01 |
  18. -----------------------------------------------------------------------------------------

  19. Outline Data
  20. -------------

  21.   /*+
  22.       BEGIN_OUTLINE_DATA
  23.       IGNORE_OPTIM_EMBEDDED_HINTS
  24.       OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
  25.       DB_VERSION('11.2.0.4')
  26.       ALL_ROWS
  27.       OUTLINE_LEAF(@"SEL$1")
  28.       INDEX_RS_ASC(@"SEL$1" "T"@"SEL$1" ("T"."OBJECT_ID"))
  29.       END_OUTLINE_DATA
  30.   */

  31. Predicate Information (identified by operation id):
  32. ---------------------------------------------------

  33.    2 - access("OBJECT_ID"=:X)


  34. 33 rows selected.

step3: add hint to use full table scan and fetch new outline

  1. SQL> select /*+ full(t) */ * from t where object_id=:x;
  2. ..
  3. SQL> select * from table(dbms_xplan.display_cursor('f0v2h6jrjrmaf',0,'outline'));

  4. PLAN_TABLE_OUTPUT
  5. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  6. SQL_ID f0v2h6jrjrmaf, child number 0
  7. -------------------------------------
  8. select /*+ full(t) */ * from t where object_id=:x

  9. Plan hash value: 1601196873

  10. --------------------------------------------------------------------------
  11. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
  12. --------------------------------------------------------------------------
  13. | 0 | SELECT STATEMENT | | | | 351 (100)| |
  14. |* 1 | TABLE ACCESS FULL| T | 1 | 98 | 351 (1)| 00:00:05 |
  15. --------------------------------------------------------------------------

  16. Outline Data
  17. -------------

  18.   /*+
  19.       BEGIN_OUTLINE_DATA
  20.       IGNORE_OPTIM_EMBEDDED_HINTS
  21.       OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
  22.       DB_VERSION('11.2.0.4')
  23.       ALL_ROWS
  24.       OUTLINE_LEAF(@"SEL$1")
  25.       FULL(@"SEL$1" "T"@"SEL$1")
  26.       END_OUTLINE_DATA
  27.   */

  28. Predicate Information (identified by operation id):
  29. ---------------------------------------------------

  30.    1 - filter("OBJECT_ID"=:X)


  31. 32 rows selected.

step 4: create sql profile to force the sql using full table scan

  1. SQL> declare
  2. v_hints sys.sqlprof_attr;
  3. begin
  4. v_hints := sys.sqlprof_attr('FULL(@"SEL$1" "T"@"SEL$1")');
  5. dbms_sqltune.import_sql_profile(
  6. SQL_TEXT => 'select * from t where object_id=:x',
  7. PROFILE => v_hints,
  8. NAME => 'PROF_T',
  9. force_match => true);
  10. end;
  11. /

  12. PL/SQL procedure successfully completed.
Privilege needs to be granted, if error ORA-38171 happens.

  1. SQL> grant advisor to cloudapp;

  2. Grant succeeded.

  3. SQL> grant administer sql management object to cloudapp;

  4. Grant succeeded.

step 5: now the sql profile is taking effect
  1. SQL> select * from t where object_id=:x;
  2. SQL> select * from table(dbms_xplan.display_cursor('9r2gykqt5j1ss',0));

  3. PLAN_TABLE_OUTPUT
  4. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  5. SQL_ID 9r2gykqt5j1ss, child number 0
  6. -------------------------------------
  7. select * from t where object_id=:x

  8. Plan hash value: 1601196873

  9. --------------------------------------------------------------------------
  10. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
  11. --------------------------------------------------------------------------
  12. | 0 | SELECT STATEMENT | | | | 351 (100)| |
  13. |* 1 | TABLE ACCESS FULL| T | 1 | 98 | 351 (1)| 00:00:05 |
  14. --------------------------------------------------------------------------

  15. Predicate Information (identified by operation id):
  16. ---------------------------------------------------

  17.    1 - filter("OBJECT_ID"=:X)

  18. Note
  19. -----
  20.    - SQL profile PROF_T used for this statement


  21. 22 rows selected.




阅读(418) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:change sid name

给主人留下些什么吧!~~