ITPub博客

首页 > Linux操作系统 > Linux操作系统 > oracle 11g sql plan baseline(5)baseline的问题和补充

oracle 11g sql plan baseline(5)baseline的问题和补充

Linux操作系统 作者:宕机熟手 时间:2013-11-05 17:08:01 0 删除 编辑


补充sql plan baseline的 其他知识及其问题

SQL> desc dba_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                                 NUMBER


有一个问题 就是用sqltext生成的签名,如果我有2个schma 下面都有t4这个表,相同的sql语句查询不同的schema下相同名字的对象(前提sql text一样,那么baselines会共享,这也许不是我们
想要的)

实验user sys建立t4 table
SQL> show user;
USER is "SYS"
SQL> create table t4 (a int);

Table created.

SQL> declare
  2  begin
  3  for i in 1..1000 loop
  4  insert into t4 values(i);
  5  end loop;
  6  commit;
  7  end;
  8  /

PL/SQL procedure successfully completed.

SQL> execute dbms_stats.gather_table_stats('SYS','T4');

PL/SQL procedure successfully completed.

SQL> select * from t4 where a=4;

         A
----------
         4

SQL> select * from table(dbms_xplan.display_cursor);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID  3d72zm2kf8pus, child number 0
-------------------------------------
select * from t4 where a=4

Plan hash value: 2560505625

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |       |       |     2 (100)|          |
|*  1 |  TABLE ACCESS FULL| T4   |     1 |     3 |     2   (0)| 00:00:01 |

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("A"=4)


18 rows selected.

SQL>  DECLARE
  2        ret PLS_INTEGER;
  3      BEGIN
  4        ret := dbms_spm.load_plans_from_cursor_cache(sql_id          => '&sql_id',
  5                                                     plan_hash_value => NULL);
  6        dbms_output.put_line(ret || ' SQL plan baseline(s) created');
  7      END;
  8    /
Enter value for sql_id: 3d72zm2kf8pus
old   4:       ret := dbms_spm.load_plans_from_cursor_cache(sql_id          => '&sql_id',
new   4:       ret := dbms_spm.load_plans_from_cursor_cache(sql_id          => '3d72zm2kf8pus',

PL/SQL procedure successfully completed.


SQL> select * from t4 where a=4;

         A
----------
         4

SQL> select * from table(dbms_xplan.display_cursor);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID  3d72zm2kf8pus, child number 1
-------------------------------------
select * from t4 where a=4

Plan hash value: 2560505625

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |       |       |     2 (100)|          |
|*  1 |  TABLE ACCESS FULL| T4   |     1 |     3 |     2   (0)| 00:00:01 |

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("A"=4)

Note
-----
   - SQL plan baseline SYS_SQL_PLAN_3a0873a3cda915e5 used for this statement


22 rows selected.

上面的问题是 我们sys schema下制作了一个基于t4的 sqlplan baselines


现在在schema xh下
SQL> conn xh/a831115
Connected.
SQL> drop table t4;
drop table t4
           *
ERROR at line 1:
ORA-00942: table or view does not exist


SQL> create table t4 (a int);

Table created.

SQL> declare
  2  begin
  3  for i in 1..500 loop
  4  insert into t4 values(i);
  5  end loop;
  6  commit;
  7  end;
  8  /

PL/SQL procedure successfully completed.

SQL> create index t4_id on t4(a);

Index created.

SQL> show user
USER is "XH"
SQL> execute dbms_stats.gather_table_stats('XH','T4');

PL/SQL procedure successfully completed.

SQL>  SELECT sql_handle, sql_text, enabled, accepted
  2      FROM dba_sql_plan_baselines
  3     WHERE created > systimestamp - to_dsinterval('0 00:15:00');

SQL_HANDLE
------------------------------
SQL_TEXT
--------------------------------------------------------------------------------
ENA ACC
--- ---
SYS_SQL_0b32b5423a0873a3   ~~~~~~~~~~~~~~~~~~~~由于xh的plan
select * from t4 where a=4
YES NO

SYS_SQL_0b32b5423a0873a3
select * from t4 where a=4
YES YES

SQL_HANDLE
------------------------------
SQL_TEXT
--------------------------------------------------------------------------------
ENA ACC
--- ---

 

SQL> select * from t4 where a=4;

         A
----------
         4

SQL> select * from table(dbms_xplan.display_cursor);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID  3d72zm2kf8pus, child number 2
-------------------------------------
select * from t4 where a=4

Plan hash value: 2560505625

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |       |       |     3 (100)|          |
|*  1 |  TABLE ACCESS FULL| T4   |     1 |     3 |     3   (0)| 00:00:01 |

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("A"=4)

Note
-----
   - SQL plan baseline SYS_SQL_PLAN_3a0873a3cda915e5 used for this statement


22 rows selected.
可以看到 由于sql text一样 但查询的对象不一样 ,还是使用了为sys.t4 sql_text统计信息制作的sql plan baselines

 

 

SQL> alter system set optimizer_use_sql_plan_baselines=false;关闭使用sql plan baseline

System altered.

SQL> select * from t4 where a=4;

         A
----------
         4

SQL> select * from table(dbms_xplan.display_cursor);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID  3d72zm2kf8pus, child number 0
-------------------------------------
select * from t4 where a=4

Plan hash value: 3313391582

--------------------------------------------------------------------------
| Id  | Operation        | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT |       |       |       |     1 (100)|          |
|*  1 |  INDEX RANGE SCAN| T4_ID |     1 |     3 |     1   (0)| 00:00:01 |

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("A"=4)


18 rows selected.
可以看到 查询xh.t4 (虽然sql text相同,但走index是很好的)

 

 

保留期,如果一个sql plan baseline autopurge属性为yes(default)在保留期中一直未使用,且过了保留期 将被清除

SQL> select parameter_value from dba_sql_management_config where parameter_name='PLAN_RETENTION_WEEKS';

PARAMETER_VALUE
---------------
             53      ~~~默认保留期是53天

 

SQL> execute dbms_spm.configure(parameter_name=>'PLAN_RETENTION_WEEKS',parameter_value=>10);

PL/SQL procedure successfully completed.

SQL> select parameter_value from dba_sql_management_config where parameter_name='PLAN_RETENTION_WEEKS';

PARAMETER_VALUE
---------------
             10~~~~~~~~修改为10天


sql plan baseline默认存储在sysaux,占sysaux10%,超过后alert中回有告警
SQL> select parameter_value from dba_sql_management_config where parameter_name='SPACE_BUDGET_PERCENT';

PARAMETER_VALUE
---------------
             10~~~default 10%

SQL> execute dbms_spm.configure(parameter_name=>'SPACE_BUDGET_PERCENT',parameter_value=>5);

PL/SQL procedure successfully completed.

 

SQL> select parameter_value from dba_sql_management_config where parameter_name='SPACE_BUDGET_PERCENT';

PARAMETER_VALUE
---------------
              5~~~~手动修改占sysaux 百分比

SQL> execute dbms_spm.configure(parameter_name=>'SPACE_BUDGET_PERCENT',parameter_value=>null);

PL/SQL procedure successfully completed.~~~~参数值为努null,就设置为default值

SQL> select parameter_value from dba_sql_management_config where parameter_name='SPACE_BUDGET_PERCENT';

PARAMETER_VALUE
---------------
             10


这是执行dbms_spm所要的系统权限
SQL> select * from session_privs where privilege like '%ADMINISTER SQL MAN%';

PRIVILEGE
----------------------------------------
ADMINISTER SQL MANAGEMENT OBJECT

 

 

*************

删除outlines

SQL> DECLARE
  2    ret PLS_INTEGER;
  3  BEGIN
  4    FOR c IN (SELECT DISTINCT sql_handle
  5              FROM dba_sql_plan_baselines
  6              WHERE creator = user
  7              AND created > systimestamp - to_dsinterval('0 00:15:00'))
  8    LOOP
    ret := dbms_spm.drop_sql_plan_baseline(c.sql_handle);
  9   10    END LOOP;
 11  END;
 12  /

PL/SQL procedure successfully completed.

 

 

 


********************
显示sql plan base line
先建立一个base line

SQL> SQL> create table t1 (a int);

Table created.

SQL> insert into t1 values(1);

1 row created.

SQL> commit;

Commit complete.

SQL> ALTER SESSION SET optimizer_capture_sql_plan_baselines = TRUE;

Session altered.

SQL> select count(*) from t1;

  COUNT(*)
----------
         1

SQL> select count(*) from t1;

  COUNT(*)
----------
         1

SQL> ALTER SESSION SET optimizer_capture_sql_plan_baselines = FALSE;

Session altered.

SQL> select count(*) from t1;

  COUNT(*)
----------
         1

SQL> select * from table(dbms_xplan.display_cursor);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID  9babjv8yq8ru3, child number 0

BEGIN DBMS_OUTPUT.GET_LINES(:LINES, :NUMLINES); END;

NOTE: cannot fetch plan for SQL_ID: 9babjv8yq8ru3, CHILD_NUMBER: 0
      Please verify value of SQL_ID and CHILD_NUMBER;
      It could also be that the plan is no longer in cursor cache (check v$sql_p
lan)

 

8 rows selected.

SQL> select count(*) from t1;

  COUNT(*)
----------
         1

SQL> select * from table(dbms_xplan.display_cursor);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID  9babjv8yq8ru3, child number 0

BEGIN DBMS_OUTPUT.GET_LINES(:LINES, :NUMLINES); END;

NOTE: cannot fetch plan for SQL_ID: 9babjv8yq8ru3, CHILD_NUMBER: 0
      Please verify value of SQL_ID and CHILD_NUMBER;
      It could also be that the plan is no longer in cursor cache (check v$sql_p
lan)

 

8 rows selected.

SQL> set serveroutput off
SQL> select count(*) from t1;

  COUNT(*)
----------
         1

SQL> select * from table(dbms_xplan.display_cursor);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID  5bc0v4my7dvr5, child number 1
-------------------------------------
select count(*) from t1

Plan hash value: 3724264953

-------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Cost (%CPU)| Time     |
-------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |       |     3 (100)|          |
|   1 |  SORT AGGREGATE    |      |     1 |            |          |

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
|   2 |   TABLE ACCESS FULL| T1   |     1 |     3   (0)| 00:00:01 |
-------------------------------------------------------------------

Note
-----
   - dynamic sampling used for this statement
   - SQL plan baseline SYS_SQL_PLAN_b98b6a04616acf47 used for this statement


19 rows selected.

#看viwe 能看基本信息dba_sql_plan_baselines
SQL> set linesize 1000
SQL> select * from dba_sql_plan_baselines where plan_name='SYS_SQL_PLAN_b98b6a04616acf47';

 SIGNATURE SQL_HANDLE                     SQL_TEXT                                                                         PLAN_NAME                      CREATOR      ORIGIN  PARSING_SCHEMA_NAME            DESCRIPTION                                                                                                                        VERSION                                                           CREATED                                                                     LAST_MODIFIED
---------- ------------------------------ -------------------------------------------------------------------------------- ------------------------------ ------------------------------ -------------- ------------------------------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------- --------------------------------------------------------------------------- ---------------------------------------------------------------------------
LAST_EXECUTED                                                               LAST_VERIFIED                                                               ENA ACC FIX AUT OPTIMIZER_COST MODULE                                          ACTION                           EXECUTIONS ELAPSED_TIME   CPU_TIME BUFFER_GETS DISK_READS DIRECT_WRITES ROWS_PROCESSED    FETCHES END_OF_FETCH_COUNT
--------------------------------------------------------------------------- --------------------------------------------------------------------------- --- --- --- --- -------------- ------------------------------------------------ -------------------------------- ---------- ------------ ---------- ----------- ---------- ------------- -------------- ---------- ------------------
1.6287E+19 SYS_SQL_e208a16bb98b6a04       select count(*) from t1                                                          SYS_SQL_PLAN_b98b6a04616acf47  XH           AUTO-CAPTURE    XH                                                                                                                                                        11.1.0.6.0                                                        23-FEB-10 10.57.21.000000 AM                                                23-FEB-10 10.57.21.000000 AM
23-FEB-10 10.57.27.000000 AM                                                                                                                            YES YES NO  YES             3 SQL*Plus                                                                                   0            0          0           0          0             0             0          0                  0

 

#用函数看更详细
SQL> select * from table(dbms_xplan.display_sql_plan_baseline(sql_handle=>'SYS_SQL_e208a16bb98b6a04'));

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------
SQL handle: SYS_SQL_e208a16bb98b6a04
SQL text: select count(*) from t1
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
Plan name: SYS_SQL_PLAN_b98b6a04616acf47
Enabled: YES     Fixed: NO      Accepted: YES     Origin: AUTO-CAPTURE
--------------------------------------------------------------------------------


PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 3724264953

-------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Cost (%CPU)| Time     |
-------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |     2   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE    |      |     1 |            |          |
|   2 |   TABLE ACCESS FULL| T1   |    82 |     2   (0)| 00:00:01 |
-------------------------------------------------------------------

20 rows selected.
#查看baseline的 所用到的hints
SQL> SELECT extractValue(value(h),'.') AS hint
  2   FROM sys.sqlobj$data od, sys.sqlobj$ so,
  3   table(xmlsequence(extract(xmltype(od.comp_data),'/outline_data/hint'))) h
  4   WHERE so.name = 'SYS_SQL_PLAN_8fb2691f3fdbb376'
  5   AND so.signature = od.signature
  6   AND so.category = od.category
  7   AND so.obj_type = od.obj_type
  8   AND so.plan_id = od.plan_id;

HINT
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FULL(@"SEL$1" "T"@"SEL$1")
OUTLINE_LEAF(@"SEL$1")
ALL_ROWS
DB_VERSION('11.1.0.6')
OPTIMIZER_FEATURES_ENABLE('11.1.0.6')
IGNORE_OPTIM_××DED_HINTS

6 rows selected.

SQL>


SQL> col occupant_name for a15
SQL> col move_procedure for a20
SQL> select occupant_name ,move_procedure from v$sysaux_occupants;

OCCUPANT_NAME   MOVE_PROCEDURE
--------------- --------------------
LOGMNR          SYS.DBMS_LOGMNR_D.SE
                T_TABLESPACE

LOGSTDBY        SYS.DBMS_LOGSTDBY.SE
                T_TABLESPACE

SMON_SCN_TIME
PL/SCOPE
STREAMS
XDB             XDB.DBMS_XDB.MOVEXDB
                _TABLESPACE

OCCUPANT_NAME   MOVE_PROCEDURE
--------------- --------------------

AO              DBMS_AW.MOVE_AWMETA
XSOQHIST        DBMS_XSOQ.OlapiMoveP
                roc

XSAMD           DBMS_AMD.Move_OLAP_C
                atalog

SM/AWR
SM/ADVISOR
SM/OPTSTAT

OCCUPANT_NAME   MOVE_PROCEDURE
--------------- --------------------
SM/OTHER
STATSPACK
SDO             MDSYS.MOVE_SDO
WM              DBMS_WM.move_proc
ORDIM
ORDIM/PLUGINS
ORDIM/SQLMM
EM              emd_maintenance.move
                _em_tblspc

TEXT            DRI_MOVE_CTXSYS

OCCUPANT_NAME   MOVE_PROCEDURE
--------------- --------------------
ULTRASEARCH     MOVE_WK
ULTRASEARCH_DEM MOVE_WK
O_USER

EXPRESSION_FILT
ER

EM_MONITORING_U
SER

TSM

OCCUPANT_NAME   MOVE_PROCEDURE
--------------- --------------------
SQL_MANAGEMENT_                      ~~~~~~~~~~~~~~~~没有工具可以将sql plan baseline存储在sysaux之外
BASE

AUTO_TASK
JOB_SCHEDULER

29 rows selected.


查到了存在sysaux中的组件

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29079814/viewspace-775734/,如需转载,请注明出处,否则将追究法律责任。

下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2013-11-05

  • 博文量
    5
  • 访问量
    8935