SPM適用的場景和示例

raysuen發表於2017-06-15
##spm開啟自動捕獲

SQL> show parameter sql_plan

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
optimizer_capture_sql_plan_baselines boolean     FALSE
optimizer_use_sql_plan_baselines     boolean     TRUE
將optimizer_capture_sql_plan_baselines設為為true,開啟自動捕獲


##SPM批次載入
#1 cursor cache中載入
exec DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE('sql_id');
#2 從sql tuning set中載入
exec DBMS_SPM.LOAD_PLANS_FROM_SQLSET('sql_tuning_set_name');
select * from DBA_SQLSET;
# 3 透過函式建立中間表,並透過Data Dump匯出匯入
exec DBMS_SPM.CREATE_STGTAB_BASELINE

SQL> desc DBMS_SPM
FUNCTION 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                IN
PROCEDURE CONFIGURE
Argument Name                  Type                    In/Out Default?
------------------------------ ----------------------- ------ --------
PARAMETER_NAME                 VARCHAR2                IN
PARAMETER_VALUE                NUMBER                  IN     DEFAULT
PROCEDURE CREATE_STGTAB_BASELINE
Argument Name                  Type                    In/Out Default?
------------------------------ ----------------------- ------ --------
TABLE_NAME                     VARCHAR2                IN
TABLE_OWNER                    VARCHAR2                IN     DEFAULT
TABLESPACE_NAME                VARCHAR2                IN     DEFAULT
FUNCTION DROP_MIGRATED_STORED_OUTLINE RETURNS BINARY_INTEGER
FUNCTION DROP_SQL_PLAN_BASELINE RETURNS BINARY_INTEGER
Argument Name                  Type                    In/Out Default?
------------------------------ ----------------------- ------ --------
SQL_HANDLE                     VARCHAR2                IN     DEFAULT
PLAN_NAME                      VARCHAR2                IN     DEFAULT
FUNCTION 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     DEFAULT
FUNCTION 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     DEFAULT
FUNCTION 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     DEFAULT
FUNCTION 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     DEFAULT
FUNCTION 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     DEFAULT
FUNCTION 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     DEFAULT
FUNCTION 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     DEFAULT
FUNCTION MIGRATE_STORED_OUTLINE RETURNS CLOB
Argument Name                  Type                    In/Out Default?
------------------------------ ----------------------- ------ --------
ATTRIBUTE_NAME                 VARCHAR2                IN
ATTRIBUTE_VALUE                CLOB                    IN     DEFAULT
FIXED                          VARCHAR2                IN     DEFAULT
FUNCTION MIGRATE_STORED_OUTLINE RETURNS CLOB
Argument Name                  Type                    In/Out Default?
------------------------------ ----------------------- ------ --------
OUTLN_LIST                     NAME_LIST               IN
FIXED                          VARCHAR2                IN     DEFAULT
FUNCTION 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     DEFAULT
FUNCTION 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

SQL>

##適用場景
# 10G升級11G
1 在現有10G環境捕獲sql語句的執行計劃儲存在STS中
begin
DBMS_SQLTUNE.CREATE_SQLSET('STS111TEST');
DBMS_SQLTUNE.CAPTURE_CURSOR_CACHE_SQLSET(SQLSET_NAME => 'STS111TEST',
                                                                                 TIME_LIMIT => 120,
                                                                                 REPEAT_INTERVAL => 5
                                                                                );
end;
/
上面PL/SQL塊表示:先建立STS111TEST的STS,採集時長120秒,每隔5秒採集一次sql執行計劃到STS中。
2 建立中間表(STGTAB111),並將 STS111TEST中的內容包裝到中間表(STGTAB111)中
begin
DBMS_SQLTUNE.CREATE_STGTAB_SQLSET('STGTAB111');
DBMS_SQLTUNE.PACK_STGTAB_SQLSET(SQLSET_NAME => 'STS111TEST',
                                                              STAGING_TABLE_NAME => 'STGTAB111'
                                                             );
end;
/

3 通exp/expdp,imp/impdp將STGTAB111表內容匯入11G目標資料中
exp system/oracle file=STGTAB111.dmp tables=STGTAB111
imp system/oracle file=STGTAB111.dmp full=y
4 下面的PL/SQL塊:現將中間表(STGTAB111)開啟並儲存在STS111TEST的STS內,然後再把這些語句的執行計劃匯入Plan Baseline中
declare
my_plans pls_integer;
begin
DBMS_SQLTUNE.UNPACK_STGTAB_SQLSET(SQLSET_NAME => 'STS111TEST',
                                                                  SQLSET_OWNER => '%',
                                                                  REPLACE => TRUE,
                                                                  STAGING_TABLE_NAME => 'STGTAB111'
                                                                 );
my_plans := DBMS_SQLTUNE.LOAD_PLANS_FROM_SQLSET(
                                        SQLSET_NAME => 'STS111TEST',
                                        SQLSET_OWNER => 'SYS',
                                        BASIC_FILTER => 'sql_text like ''%''',
                                        FIXED => 'YES',
                                        ENABLED => 'YES',
                                        COMMIT_ROWS => 1        
                                                                                             );
end;
/

#從開發資料庫到生產資料庫
1 建立SPM_SAVED的stage表,在將SQL Plan Baseline資料匯入SPM_SAVED表
declare
spm number;
begin
DBMS_SPM.CREATE_STGTAB_BASELINE(
                                            TABLE_NAME => 'SPM_SAVED',
                                            TABLE_OWNER => 'SYSTEM'
                                                             );
spm := DBMS_SPM.PACK_STGTAB_BASELINE(
                                            TABLE_NAME => 'SPM_SAVED',
                                            TABLE_OWNER => 'SYSTEM'
                                                                        );
end;
/

2 通exp/expdp,imp/impdp將SPM_SAVED表內容匯入生產資料中
exp system/oracle file=SPM_SAVED.dmp tables=SPM_SAVED
imp system/oracle file=SPM_SAVED.dmp full=y

3 將SPM_SAVED表的內容匯入生產庫的QL Plan Baseline
declare
spm number;
begin
spm := DBMS_SPM.UNPACK_STGTAB_BASELINE(
                                            TABLE_NAME => 'SPM_SAVED',
                                            TABLE_OWNER => 'SYSTEM'
                                                                            );
end;
/

select SIGNATURE,SQL_HANDLE,SQL_TEXT,PLAN_NAME,ORIGIN,ENABLED,ACCEPTED,FIXED,AUTOPURGE,LAST_EXECUTED,LAST_MODIFIED from dba_sql_plan_baselines;


注:大部分內容出自:羅敏 <<感悟oracle核心技術>>

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

相關文章