ORACLE SPA

注:转自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 */%‘;

时间: 2024-11-09 17:22:13

ORACLE SPA的相关文章

『ORACLE』SPA性能分析器

SPA的主要作用:主要测试由于系统环境变更包括操作系统变更,数据库升级等对sql性能的影响.尽量减少由于系统环境的变更对业务的影响程度. 分析流程:1.获取sql存取到Tunning set=>2.准备测试环境并传输tunning set=>3.实施系统变更=>4.创建spa task=>5.spa分析产生系统改变前和改变后的对比报告=>6.根据对比结果对sql语句进行优化=>7.为了验证优化结果再次分析报告与系统改变前进行对比 EM创建tunning set set

Oracle通过sqlplus spool导入导出数据

第一部分(实例,主要分两步),第二部分(参数小总结),第三部分(完全参数总结) 第一部分 第一步 :这是我的导出数据的脚本call.sqlconn scott/tigerset echo offset term offset line 1000 pages 0set feedback offset heading offset trimspool on spool /temp/test/ldr_test.csvselect a.empno||',"'||a.ename||'",'||t

实战:ORACLE SQL Performance Analyzer

通过 SPA,您能够依据各种更改类型(如初始化參数更改.优化器统计刷新和数据库升级)播放特定的SQL 或整个 SQL 负载,然后生成比較报告,帮助您评估它们的影响. 在 Oracle Database 11g 之前的版本号中,我必须捕获全部 SQL 语句,通过跟踪执行这些语句,然后得到运行计划 - 这是一项极其耗时又极易出错的任务.新版本号中,我们不须要再那样做了,我改用很easy而有效的 SQL Performance Analyzer. ---使用场景 1.数据库升级2.实施优化建议3.更改

oracle 11g 新特性

1.Exadata是结合了数据库威力的硬件. 闪存:不同于数据库闪存,这是一个基于文件扩展的常驻SGA缓冲区的高速缓存(类似于交换区),为数据库提供二级缓存.当数据库被换出SGA时该文件被使用. 存储索引:利用最小值/最大值使查询运行得更快.主要维护数据的摘要信息.内存结构驻留在存储单元层面上.对于一个存储单元而言,存储索引最多可以存储8个查询表列的最小值/最大值.存储索引指示数据如何存储在存储单元上(就像分区的最小值/最大值). 2.高级压缩 允许数据在更新和插入表时保持压缩 create t

Oracle sqlplus 常用设置

Oracle sqlplus 常用设置 §2.5 SQLPLUS常用设置 使用过程中大都需要进行必要的环境设置才能完成我们所需要的输出. 所有环境的设置由SET命令加相应的环境变量来完成.下面是常用的环境设置: §2.5.1   ARRAYSIZE(取回的行数)SET ARRAY[SIZE]{integer} 一次可以提取(Fetch)的行的数目,1->5000,当有较长字段时应设小些. §2.5.2   AUTOCOMMIT(自动提交)SET AUTO [COMMIT] { [ OFF | O

Oracle 11g OCM考试小结

经过三个半月的浴血奋战,终于在5月5号.6号完成了最后的备考征程,终于可以释放啦(5月31号才收到oracle的考试通过通知)! 整个过程,洋溢着个人的汗水和辛苦,还有家人的默默付出.每天晚上都要学习到12点,以保证按计划完成每天的学习任务.儿子的学习.家里的大小情况,一概包在老婆的身上啦.所以要由衷地感谢她们,感谢她们对我的照顾,也感谢她们对我的忍让! 虽然学习过程充满了辛苦,但整个学习路线却是非常清晰的.一个就是,跟着西班牙Oracle大师Raulibaper的学习分享,逐个目录进行学习.后

SPA 最佳实践

SPA 最佳实践 SPA官方描绘的蛮完美的,但在实践中,还是有很多的坑.下面会一一展示本人在SPA实践项目中遇到的各种坑,以及解决方案. OPTIMIZER_MODE问题 SQL在目标库实际运行时需要跟源库保持一致,以SIEBEL库为例,它都会在SESSION级别设置以下影响CBO的参数. alter session set optimizer_mode=FIRST_ROWS_10; alter session set "_hash_join_enabled"=FALSE; alter

Oracle SQL tuning 步骤

Oracle SQL tuning 步骤 SQL是的全称是Structured Query Language(结构化查询语言).SQL是一个在80年代中期被使用的工业标准数据库查询语言.不要把SQL语言与商业化产品如Microsoft SQL server或开源产品MySQL相混淆.所有的使用SQL缩略词的这些都是SQL标准的一部分. 一.SQL tuning之前的调整    下面这个粗略的方法能够节省数千小时乏味的SQL tuning,因为一旦调整它将影响数以百计的SQL查询.记住,你必须优先

Oracle 18C新特性介绍

Oracle 18c 是在 2018-02-16 发布出来的,还是秉承着 Oracle 的 Cloud first 理念,18c 现在 Cloud 和 Engineered Systems 上推出.Oracle 18c号称是一款自治性的数据库,可以减少很多DBA的工作,很多从事DBA工作的人员是不是要担心自己的工作受到影响?其实Oracle 18c上并未体现的特别明显,不过这是未来的一个趋势和信号! 查看了官方的一些文档资料,就新增的一些新的特点和大家分享一下,18C继续着12C的云数据库的特点