注:转自http://blog.csdn.net/wish503/article/details/52066944

------------------------------------------------------
--Step1: 创建名称为STS_NAME_TEST 的SQL_SET.
---------------------------------------------------
BEGIN
DBMS_SQLTUNE.CREATE_SQLSET(SQLSET_NAME => 'STS_NAME_TEST',
          DESCRIPTION => 'COMPLETE APPLICATION WORKLOAD',
          SQLSET_OWNER =>'TEST');
END;
/

--------------------------------------------------
--Step2: 初始加载当前数据库中的SQL.
---------------------------------------------------
DECLARE
   STSCUR   DBMS_SQLTUNE.SQLSET_CURSOR;
   v_cnt    NUMBER;
BEGIN
 OPEN STSCUR FOR
   SELECT VALUE (P)
    FROM TABLE (
    DBMS_SQLTUNE.SELECT_CURSOR_CACHE (
     '(lower(sql_text) LIKE ''%table_name%''
           ) and PARSING_SCHEMA_NAME <> ''SYS''',
          'ALL')) P;
   -- POPULATE THE SQLSET
  DBMS_SQLTUNE.LOAD_SQLSET (SQLSET_NAME       => 'STS_NAME_TEST',
                            POPULATE_CURSOR   => STSCUR,
                             commit_rows       => 100,
                             SQLSET_OWNER      => 'TEST');
  CLOSE STSCUR;
   COMMIT;
EXCEPTION
   WHEN OTHERS
   THEN
      RAISE;
END;
/

--------------------------------------------------
--Step3: 增量抓取数据库中的SQL, 会连续抓取三天,每小时抓取一次,在后台执行
---------------------------------------------------
--1编辑sql文件
vi getsql_TEST.sql

BEGIN
  DBMS_SQLTUNE.CAPTURE_CURSOR_CACHE_SQLSET(SQLSET_NAME=>''STS_NAME_TEST'',
    TIME_LIMIT=> 259200,                          
    REPEAT_INTERVAL=>3600,                          
    CAPTURE_OPTION=>'MERGE',                          
    CAPTURE_MODE =>DBMS_SQLTUNE.MODE_ACCUMULATE_STATS, 
    BASIC_FILTER=>
     '(lower(sql_text) LIKE ''%table_name%'') and PARSING_SCHEMA_NAME <> ''SYS''',
       SQLSET_OWNER => 'TEST');                          
    END;
/

--2编辑shell脚本
vi getsql_TEST.sh
#!/bin/ksh
sqlplus '/as sysdba' << EOF 
@getsql_TEST.sql
exit
EOF

--3后台执行脚本
nohup sh getsql_TEST.sh &

-------------------------
BSCOW,创建中间表
-------------------------
Begin
     dbms_sqltune.create_stgtab_sqlset(table_name => 'STGTAB_TEST' ,schema_name => 'TEST');
End;
/

---------------------------
BSCOW,将sqlset打包到中间表
---------------------------
Begin
 dbms_sqltune.pack_stgtab_sqlset(sqlset_name =>'STS_NAME_TEST',sqlset_owner =>'TEST',
 staging_table_name =>'STGTAB_TEST' ,staging_schema_owner => 'TEST' );
End;
/

-----------------
BSCOW,导出中间表
-----------------
exp STGTAB_TEST

--------------------------------------------
中间库,创建到部署cow的db_link,导入SQL,去重
--------------------------------------------
create public database link to_dbname connect to TEST identified by XXXX 
using 'DBNAME';

imp STGTAB_TEST

--按照plan_hash_value;
begin
for v in (
select plan_hash_value,count(*) cnt from TEST.STGTAB_TEST  group by plan_hash_value) loop
while(v.cnt>1) loop
delete from TEST.STGTAB_TEST a where a.plan_hash_value =v.plan_hash_value and rownum=1;
v.cnt:=v.cnt-1;
end loop;
commit;
end loop;
end;
/

--按照hash_value:
begin
for v in (
select hash_value,count(*) cnt from TEST.STGTAB_TEST  group by hash_value) loop
while(v.cnt>1) loop
delete from TEST.STGTAB_TEST a where a.hash_value =v.hash_value and rownum=1;
v.cnt:=v.cnt-1;
end loop;
commit;
end loop;
end;
/

-------------------------------------------------
中间库,对中间表进行解包,将sql导入到目标端sqlset
-------------------------------------------------

update TEST.STGTAB_TEST set owner='TEST';

Begin
   dbms_sqltune.unpack_stgtab_sqlset(sqlset_name => 'STS_NAME_TEST',sqlset_owner => 'TEST'  ,replace => TRUE,staging_table_name =>'STGTAB_TEST' ,staging_schema_owner => 'TEST');
End;
/

------------------
中间库,过滤sqlset
-----------------
begin
  DBMS_SQLTUNE.DELETE_SQLSET(sqlset_name => 'STS_NAME_TEST',basic_filter => 'SQL_TEXT LIKE ''%Analyze(%''',sqlset_owner => 'TEST');
END;
/
(把sqlset中的analyze语句去掉,这种语句非业务语句)
select sql_text from dba_sqlset_statements where sqlset_name='STS_NAME_TEST';(查询sqlset中的sql语句)

--------------
BSCOW,增加授权
--------------
grant execute on SYS.DBMS_SQLPA to TEST;

------------------------
中间库,创建spa task
---------------------
使用devmgr用户创建SPA任务,后续执行SPA任务也使用test用户:
需要在中间库对test用户授权:
grant execute on SYS.dbms_sqlpa to TEST;
conn TEST/xxxxxx
declare
  mytask varchar2(100);
begin
  mytask := dbms_sqlpa.create_analysis_task(sqlset_name => 'STS_NAME_TEST',
                                            task_name   => 'task_test01');
END;
/

-----------
中间库,查询task是否创建成功
----------
Select * From Dba_Advisor_Tasks Where task_name='task_test01';
select TASK_ID,owner,TASK_NAME,status from dba_advisor_tasks where task_name='task_test01';

-----------------------------------------------
中间库,执行spa task,生成性能测试基线,后台执行,参数可能还需要考虑下任务里sql的执行次数以及对执行耗时长的sql的处理
-----------------------------------------------
sqlplus TEST/ptmjygb8 <<EOF

begin
  dbms_sqlpa.execute_analysis_task(task_name        => 'task_test01',
                                   execution_type   => 'TEST EXECUTE',
                                   execution_name   => 'exec01',
                                   execution_params => dbms_advisor.argList('DATABASE_LINK','TO_DBNAME','EXECUTE_COUNT',3,'LOCAL_TIME_LIMIT','300'),
                                   execution_desc   => 'executeion on 11g');
end;
/
EOF

-----------------------
中间库,查询基线进度
-----------------------
SELECT owner,task_name,execution_name,a.execution_type,execution_start,execution_last_modified,execution_end,status,b.SOFAR,b.START_TIME,b.LAST_UPDATE_TIME
from  dba_advisor_executions a,  v$advisor_progress b where TASK_NAME='task_test01' and a.task_id=b.TASK_ID;

------------------
BSCOW,执行变更
------------------
比如对表收集统计信息,新建索引等。

--------------------------------------
中间库,第二次执行spa task,后台执行
-------------------------------------
sqlplus TEST/ptmjygb8 <<EOF
begin
  dbms_sqlpa.execute_analysis_task(task_name        => 'task_test01',
                                   execution_type   => 'TEST EXECUTE',
                                   execution_name   => 'exec02',
                                   execution_params => dbms_advisor.argList(
'DATABASE_LINK','TO_RSAPPCOW','EXECUTE_COUNT',3,'LOCAL_TIME_LIMIT','300'),
                                   execution_desc   => 'executeion on 10g');
end;
/
EOF

——保留相关报错信息
create table Dba_Advisor_Objects_old as select * from Dba_Advisor_Objects;
create table Dba_Advisor_Findings_old as select * from Dba_Advisor_Findings;
--保留参数信息
create table dba_advisor_parameters_old as select * from dba_advisor_parameters;
create table Dba_Advisor_Executions_old as select * from Dba_Advisor_Executions;
--保留性能数据
create table dba_advisor_sqlstats_old as select * from dba_advisor_sqlstats;
create table dba_advisor_sqlplans_old as select * from dba_advisor_sqlplans;

---------------
中间库,分析结果
----------------
--执行计划改变的SQL
Select st.sql_id,
       sst.executions,
       dbms_lob.substr(st.sql_text, 3000) sql_text
  From sys.wrh$_sqltext st,
       dba_sqlset_statements sst,
       (Select Distinct sql_id
          From (Select sql_id,
                       operation,
                       options,
                       object_name,
                       object_alias,
                       object_type,
                       Id,
                       parent_id,
                       depth
                  From dba_advisor_sqlplans
                 Where execution_name = 'exec02'
                   And task_name = 'task_test01'
                Minus
                Select sql_id,
                       operation,
                       options,
                       object_name,
                       object_alias,
                       object_type,
                       Id,
                       parent_id,
                       depth
                  From dba_advisor_sqlplans
                 Where execution_name = 'exec01'
                   And task_name = 'task_test01')) cp
 Where st.sql_id = cp.sql_id
   And sst.sql_id = cp.sql_id
   And st.sql_text Not Like '%Analyze(%'
   And st.sql_text Not Like '%SELECT /* DS_SVC */%'
   And st.sql_text Not Like '%/* OPT_DYN_SAMP */%'
   And sst.sqlset_name = 'STS_NAME0314';
   
--以Buffer_Gets作为比较条件,获取性能数据进行比对
Select *
  From (Select b.*, dbms_lob.substr(st.sql_text, 3000) sql_text
          From SYS.Wrh$_Sqltext st,
               (Select TASK_NAME,
                       sql_id,
                       executions,
                       detal_buffer_gets,
                       bf_buffer_gets,
                       af_buffer_gets,
                       bf_plan_hash_value,
                       bf_rows_processed,
                       af_plan_hash_value,
                       af_ROWS_PROCESSED
                  From (Select TASK_NAME,
                               sql_id,
                               bf_executions executions,
                               round(af_buffer_gets / af_EXECUTIONS) -
                               round(bf_buffer_gets / bf_executions) detal_buffer_gets,
                               round(bf_buffer_gets / bf_executions) bf_buffer_gets,
                               round(af_buffer_gets / af_EXECUTIONS) af_buffer_gets,
                               bf_plan_hash_value,
                               bf_rows_processed / bf_executions bf_rows_processed,
                               af_plan_hash_value,
                               af_ROWS_PROCESSED / af_EXECUTIONS af_ROWS_PROCESSED
                          From (Select bf.TASK_NAME,
                                       bf.sql_id,
                                       bf.executions bf_executions,
                                       bf.plan_hash_value bf_plan_hash_value,
                                       bf.buffer_gets bf_buffer_gets,
                                       bf.rows_processed bf_rows_processed,
                                       af.plan_hash_value af_plan_hash_value,
                                       af.BUFFER_GETS af_BUFFER_GETS,
                                       af.EXECUTIONS af_EXECUTIONS,
                                       af.ROWS_PROCESSED af_ROWS_PROCESSED
                                  From dba_advisor_sqlstats  af,
                                       dba_advisor_sqlstats bf
                                 Where af.execution_name = 'exec02'
                                   And af.task_name = 'task_test01'
                                   And bf.execution_name = 'exec01'
                                   and bf.task_name = 'task_test01'
                                   And bf.sql_id = af.sql_id))
                 Where detal_buffer_gets > 0) b
         Where st.sql_id = b.sql_id
         Order By detal_buffer_gets Desc)
 Where sql_text Not Like '%Analyze(%'
   And sql_text Not Like '%SELECT /* DS_SVC */%'
   And sql_text Not Like '%/* OPT_DYN_SAMP */%';
   
--以elapsed_time作为比较条件获取性能数据进行比对
Select *
  From (Select b.*, dbms_lob.substr(st.sql_text, 3000) sql_text
          From SYS.Wrh$_Sqltext st,
               (Select TASK_NAME,
                       sql_id,
                       executions,
                       detal_elapsed_time,
                       bf_elapsed_time,
                       af_elapsed_time,
                       bf_plan_hash_value,
                       bf_rows_processed,
                       af_plan_hash_value,
                       af_ROWS_PROCESSED
                  From (Select TASK_NAME,
                               sql_id,
                               bf_executions executions,
                               round(af_elapsed_time / af_EXECUTIONS) -
                               round(bf_elapsed_time / bf_executions) detal_elapsed_time,
                               round(bf_elapsed_time / bf_executions) bf_elapsed_time,
                               round(af_elapsed_time / af_EXECUTIONS) af_elapsed_time,
                               bf_plan_hash_value,
                               bf_rows_processed / bf_executions bf_rows_processed,
                               af_plan_hash_value,
                               af_ROWS_PROCESSED / af_EXECUTIONS af_ROWS_PROCESSED
                          From (Select bf.TASK_NAME,
                                       bf.sql_id,
                                       bf.executions bf_executions,
                                       bf.plan_hash_value bf_plan_hash_value,
                                       bf.elapsed_time bf_elapsed_time,
                                       bf.rows_processed bf_rows_processed,
                                       af.plan_hash_value af_plan_hash_value,
                                       af.elapsed_time af_elapsed_time,
                                       af.EXECUTIONS af_EXECUTIONS,
                                       af.ROWS_PROCESSED af_ROWS_PROCESSED
                                  From dba_advisor_sqlstats  af,
                                       dba_advisor_sqlstats bf
                                 Where af.execution_name = 'exec02'
                                   And af.task_name = 'task_test01'
                                   And bf.execution_name = 'exec01'
                                   and bf.task_name = 'task_test01'
                                   And bf.sql_id = af.sql_id))
                 Where detal_elapsed_time > 0) b
         Where st.sql_id = b.sql_id
         Order By detal_elapsed_time Desc)
 Where sql_text Not Like '%Analyze(%'
   And sql_text Not Like '%SELECT /* DS_SVC */%'
   And sql_text Not Like '%/* OPT_DYN_SAMP */%';

ORACLE SPA的更多相关文章

  1. Oracle SPA取报告阶段xml解析失败解决方案

    SPA的整体测试过程可以参考: 记录一则完整的SPA(10g->11g)测试过程 故障描述:数据库字符集:US7ASCII,在SPA分析阶段正常,但在取报告阶段xml解析失败,具体现象如下: S ...

  2. 转://Oracle数据库升级后保障SQL性能退化浅谈

    一.数据库升级后保障手段 为了保障从10.2.0.4版本升级到11.2.0.4版本更加平稳,我们事先采用了oracle性能分析器(SQL Performance Analyzer)来预测数据库的关键S ...

  3. 11g SPA (sql Performance Analyze) 进行升级测试

    注;转自http://ju.outofmemory.cn/entry/77139 11G的新特性SPA(SQL Performance Analyze)现在被广泛的应用到升级和迁移的场景.当然还有一些 ...

  4. 关于Oracle开启自动收集统计信息的SPA测试

    主题:关于Oracle开启自动收集统计信息的SPA测试 环境:Oracle RAC 11.2.0.4(Primary + Standby) 需求:生产Primary库由于历史原因关闭了自动统计信息的收 ...

  5. Oracle通过sqlplus spool导入导出数据

    第一部分(实例,主要分两步),第二部分(参数小总结),第三部分(完全参数总结) 第一部分 第一步 :这是我的导出数据的脚本call.sqlconn scott/tigerset echo offset ...

  6. 实战:ORACLE SQL Performance Analyzer

    通过 SPA,您能够依据各种更改类型(如初始化參数更改.优化器统计刷新和数据库升级)播放特定的 SQL 或整个 SQL 负载,然后生成比較报告,帮助您评估它们的影响. 在 Oracle Databas ...

  7. Oracle SQL tuning 步骤

    Oracle SQL tuning 步骤 SQL是的全称是Structured Query Language(结构化查询语言).SQL是一个在80年代中期被使用的工业标准数据库查询语言.不要把SQL语 ...

  8. [整理]:oracle spool 用法

    本文来自iDB Stock:http://www.idb-stock.net/idb/2011/06/01/153.html 1.spool的作用是什么? spool的作用可以用一句话来描述:在sql ...

  9. oracle 之数据字典屣履造门。

     oracle 之数据字典屣履造门.(更新中)       今天是2013-06-20,哎,写这篇笔记的时候,我发现我是一个非常懒惰的人,这篇文章本该昨天就完成的,想起了钱鹤滩的<明日歌> ...

随机推荐

  1. python基础学习(十一)公共方法

    Python内置函数 Python 包含了以下内置函数: 切片 切片 使用 索引值 来限定范围,从一个大的 字符串 中 切出 小的 字符串 列表 和 元组 都是 有序 的集合,都能够 通过索引值 获取 ...

  2. 三问助你Fundebug

    译者按: Debug也要三省吾身! 原文: Three Questions About Each Bug You Find 译者: Fundebug 为了保证可读性,本文采用意译而非直译.另外,本文版 ...

  3. 微信小程序之封装http请求

    下面将封装http请求服务部分的服务以及引用部分 // 本服务用于封装请求 // 返回的是一个promisepromise var sendRrquest = function (url, metho ...

  4. jquery网页日历显示控件calendar3.1使用详解

    关于日历插件,我做了好多次尝试,一直致力于开发一款简单易用的日历控件.我的想法是争取在引用这个控件后,用一行js代码就能做出一个日历,若在加点参数,就能自定义外观和功能丰富多彩的日历.Calendar ...

  5. react学习笔记2

    1.build文件介绍 (1)react.js  是react的核心库 (2)react-dom.js  提供与DOM相关功能 (3)browser.js  是将JSX语法转为javascript语法 ...

  6. eNSP 常用操作

    1.eNSP关闭保存文件的提示信息 总是提示如下信息: Oct 12 2017 23:49:24-08:00 Huawei DS/4/DATASYNC_CFGCHANGE:OID 1.3.6.1.4. ...

  7. Velodyne VLP-16 gmapping 建图

    1. 测试环境 Ubuntu 16.04 x64.ROS Kinetic.Velodyne VLP-16.RoboWare Studio 2. 安装 ROS 功能包 sudo apt-get inst ...

  8. VR技术了解(作业)

    增强现实技术 概念:将现实世界和虚拟世界无缝集成的新技术. 突出特点: 真实世界与虚拟的信息集成 实时交互 三维空间中增添定位虚拟物 应用 医疗领域:医生可以利用增强现实技术,轻易地进行手术部位的精确 ...

  9. pyHook和pythoncom的安装

    pyHook包为Windows中的全局鼠标和键盘事件提供回调.Python应用程序为用户输入事件注册事件处理程序,例如鼠标左键,鼠标左键,按键等,并设置键盘和/或鼠标挂钩.底层C库报告的信息包括事件的 ...

  10. cve-2017-11882 poc分析

    目录 CVE-2017-11882 poc分析 0x00 工具&实验环境 0x01 分析行为 第一步:观察poc行为 第二步:找出计算器被弹出的地方 0x02 调试定位漏洞触发点 0x03 分 ...