[20130109]SPM與sql profile.txt

lfree發表於2013-01-09
[20130109]SPM與sql profile.txt

SPM是11G有的新特性,而sql profile在10g早已經存在。如果在11G,兩者都使用的話,執行會選擇那個呢?
自己做一個測試來說明情況:


1.建立測試環境:
SQL> select * from v$version where rownum<=1;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

SQL> create table t as select rownum id ,lpad('x',100,'x') name  from dual connect by level<=10000;
Table created.

SQL> create unique index i_t_id on t(id);
SQL> exec dbms_stats.gather_table_stats(USER,'T',cascade => true);
SQL> variable a number ;
SQL> exec :a := 100;
SQL> select /*+ full(t) */ * from t where id = :a ;

2.我在執行計劃中使用full提示,執行計劃一定選擇全表掃描:

我建立sql profile,方法如下:

DECLARE
  ret_val VARCHAR2(4000);
BEGIN
  ret_val := DBMS_SQLTUNE.CREATE_TUNING_TASK(
                sql_id          => '89u57qt97ftca',
                plan_hash_value => NULL,
                scope       => 'COMPREHENSIVE',
                time_limit  => 1800,
                task_name   => 'test',
                description => 'study');
  Dbms_Sqltune.EXECUTE_TUNING_TASK('test');
END;
select Dbms_Sqltune.REPORT_TUNING_TASK('test', 'TEXT', 'all') report from dual

--提示執行如下:
execute dbms_sqltune.accept_sql_profile(task_name => 'test', task_owner=> 'SCOTT', replace => TRUE);

--這樣執行就是使用索引。先暫時禁用sql profile。

BEGIN
  DBMS_SQLTUNE.ALTER_SQL_PROFILE (
   name             => 'SYS_SQLPROF_013c1cf4e9920000',
   attribute_name   => 'STATUS',
   value            => 'DISABLED');
END;

3.建立sql baseline:
SQL> alter session set optimizer_capture_sql_plan_baselines=true ;
SQL> select /*+ full(t) */ * from t where id = :a ;
SQL> select /*+ full(t) */ * from t where id = :a ;
SQL> alter session set optimizer_capture_sql_plan_baselines=false ;

SQL>  select sql_handle, plan_name, sql_text,enabled, accepted,fixed,origin,signature from dba_sql_plan_baselines ;

SQL_HANDLE                     PLAN_NAME                      SQL_TEXT                                                 ENA ACC FIX ORIGIN                       SIGNATURE
------------------------------ ------------------------------ -------------------------------------------------------- --- --- --- -------------- -----------------------
SYS_SQL_443d558815eb01e6       SQL_PLAN_48gapj0ayq0g694ecae5c select /*+ full(t) */ * from t where id = :a             YES YES NO  AUTO-CAPTURE       4917180411130085862

--可以發現已經建立了sql baseline。使能sql profile。

BEGIN
  DBMS_SQLTUNE.ALTER_SQL_PROFILE (
   name             => 'SYS_SQLPROF_013c1cf4e9920000',
   attribute_name   => 'STATUS',
   value            => 'ENABLED');
END;

4.兩者都存在的情況下,會使用那個呢?
SQL> select /*+ full(t) */ * from t where id = :a ;

        ID NAME
---------- --------------------------------------------------
       100 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
           xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

SQL> @dpc ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  89u57qt97ftca, child number 0
-------------------------------------
select /*+ full(t) */ * from t where id = :a
Plan hash value: 1601196873
--------------------------------------------------------
| Id  | Operation         | Name | E-Rows | Cost (%CPU)|
--------------------------------------------------------
|   0 | SELECT STATEMENT  |      |        |    47 (100)|
|*  1 |  TABLE ACCESS FULL| T    |      1 |    47   (0)|
--------------------------------------------------------
Peeked Binds (identified by position):
--------------------------------------
   1 - (NUMBER): 100
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("ID"=:A)
Note
-----
   - SQL profile SYS_SQLPROF_013c1cf4e9920000 used for this statement
   - SQL plan baseline SQL_PLAN_48gapj0ayq0g694ecae5c used for this statement
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or
       * parameter 'statistics_level' is set to 'ALL', at session or system level
31 rows selected.

--可以從Note中發現,提示兩者都使用,但是實際上執行計劃使用的是全表掃描。也就是講在兩者都存在的情況下,SPM優先。

5.如果關閉SPM,看看:
SQL>  select sql_handle, plan_name, sql_text,enabled, accepted,fixed,origin,signature from dba_sql_plan_baselines ;

SQL_HANDLE                     PLAN_NAME                      SQL_TEXT                                                 ENA ACC FIX ORIGIN                       SIGNATURE
------------------------------ ------------------------------ -------------------------------------------------------- --- --- --- -------------- -----------------------
SYS_SQL_443d558815eb01e6       SQL_PLAN_48gapj0ayq0g694ecae5c select /*+ full(t) */ * from t where id = :a             YES YES NO  AUTO-CAPTURE       4917180411130085862
SYS_SQL_443d558815eb01e6       SQL_PLAN_48gapj0ayq0g6e3e62b6b select /*+ full(t) */ * from t where id = :a             YES NO  NO  AUTO-CAPTURE       4917180411130085862

variable v_basenum number;
exec :v_basenum := dbms_spm.alter_sql_plan_baseline(sql_handle=>'SYS_SQL_443d558815eb01e6',plan_name=>'SQL_PLAN_48gapj0ayq0g694ecae5c',attribute_name=>'ENABLED',attribute_value=>'NO');

SQL>  select sql_handle, plan_name, sql_text,enabled, accepted,fixed,origin,signature from dba_sql_plan_baselines ;

SQL_HANDLE                     PLAN_NAME                      SQL_TEXT                                                 ENA ACC FIX ORIGIN                       SIGNATURE
------------------------------ ------------------------------ -------------------------------------------------------- --- --- --- -------------- -----------------------
SYS_SQL_443d558815eb01e6       SQL_PLAN_48gapj0ayq0g694ecae5c select /*+ full(t) */ * from t where id = :a             NO  YES NO  AUTO-CAPTURE       4917180411130085862
SYS_SQL_443d558815eb01e6       SQL_PLAN_48gapj0ayq0g6e3e62b6b select /*+ full(t) */ * from t where id = :a             YES NO  NO  AUTO-CAPTURE       4917180411130085862

SQL> select /*+ full(t) */ * from t where id = :a ;

        ID NAME
---------- --------------------------------------------------
       100 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
           xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

SQL> @dpc ''
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------
SQL_ID  89u57qt97ftca, child number 0
-------------------------------------
select /*+ full(t) */ * from t where id = :a
Plan hash value: 1149237570
--------------------------------------------------------------------
| Id  | Operation                   | Name   | E-Rows | Cost (%CPU)|
--------------------------------------------------------------------
|   0 | SELECT STATEMENT            |        |        |     2 (100)|
|   1 |  TABLE ACCESS BY INDEX ROWID| T      |      1 |     2   (0)|
|*  2 |   INDEX UNIQUE SCAN         | I_T_ID |      1 |     1   (0)|
--------------------------------------------------------------------
Peeked Binds (identified by position):
--------------------------------------
   1 - (NUMBER): 100
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("ID"=:A)
Note
-----
   - SQL profile SYS_SQLPROF_013c1cf4e9920000 used for this statement
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or
       * parameter 'statistics_level' is set to 'ALL', at session or system level

--再次證明兩者存在的情況下SPM優先,而不管那個執行效率高。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-752344/,如需轉載,請註明出處,否則將追究法律責任。

相關文章