批量获取oracle的AWR报告方法

awr快照默认每小时抓取1次,生成awr报告则通常时间跨度为1小时,如果想看1天的每小时的awr则需要手工一个个生成,选择开始快照号和截止快照号,可考虑用以下方法批量生成。

先查看当前有哪些时间的快照

sql 复制代码
set linesize 300
col snap_id for 99999999
col dbid for 99999999999
col startup_time for a20
col begin_interval_time for a20
col end_interval_time for a25
alter session set NLS_TIMESTAMP_FORMAT='yyyy-mm-dd hh24:mi:ss';
select dbid,instance_number inst_id,snap_id,begin_interval_time,end_interval_time,startup_time
from dba_hist_snapshot order by 1,2,3;

脚本内容如下:

sql 复制代码
--------------------------------------------------------------------------------
/*
set linesize 300
col snap_id for 99999999
col dbid for 99999999999
col startup_time for a20
col begin_interval_time for a20
col end_interval_time for a25
alter session set NLS_TIMESTAMP_FORMAT='yyyy-mm-dd hh24:mi:ss';
select dbid,instance_number inst_id,snap_id,begin_interval_time,end_interval_time,startup_time
from dba_hist_snapshot order by 1,2,3;
*/
-- 用法:       
--     @awrbatch <dbid> <instance_num> <start_snap> <end_snap>
--
-- 示例:
--     @awrbatch 2580850603 1 32302 32310
--
--------------------------------------------------------------------------------
set serveroutput on
set feedback off

declare
  v_dbid            number;
  v_instance        number;
  v_b_id            number;
  v_e_id            number;
  v_code            number;
  v_errm            varchar2(300);
  v_sql             varchar2(300);
  v_html            varchar2(20000);
  cur_awrrpt_html   SYS_REFCURSOR;
  cur_snapshot      SYS_REFCURSOR;
  fileID            utl_file.file_type;
  v_filename        varchar2(30);
  v_snap_id         number;
  v_startup_time    timestamp(3);
  v_begin_snap_time timestamp(3);
  v_end_snap_time   timestamp(3);
  v_dpath           varchar2(60);
begin
  v_dbid     := &1;
  v_instance := &2;
  v_b_id     := &3;
  v_e_id     := &4;
  dbms_output.put_line(chr(13) || chr(10) || 'awrrpt report files:');
  for k in v_b_id .. v_e_id - 1 loop
    v_filename := 'awr_' || v_instance || '_' || k || '_' || (k + 1) ||'.html';
    fileID     := utl_file.fopen('DATA_PUMP_DIR', v_filename, 'a', 32767);
    v_sql      := 'select output from table(dbms_workload_repository.awr_report_html(' ||
                  v_dbid || ',' || v_instance || ',' || k || ',' || (k + 1) ||',8))';
    open cur_awrrpt_html for v_sql;
    loop
      exit when cur_awrrpt_html%notfound;
      fetch cur_awrrpt_html  into v_html;
      utl_file.put_line(fileID, v_html);
    end loop;
    utl_file.fclose(fileID);
    execute immediate 'select directory_path from dba_directories where directory_name=:dname' into v_dpath using 'DATA_PUMP_DIR';
    dbms_output.put_line(v_dpath || v_filename);
  end loop;
exception
  when others then
    v_code := SQLCODE;
    v_errm := SQLERRM;
    dbms_output.put_line('ERROR CODE ' || v_code || ':' || v_errm);
end;
/

生成的报告会输出到目录下

sql 复制代码
select directory_path from dba_directories where directory_name='DATA_PUMP_DIR';
相关推荐
X566125 分钟前
如何在 Laravel 中正确保存嵌套动态表单数据(主服务与子服务)
jvm·数据库·python
虹科网络安全2 小时前
艾体宝干货|数据复制详解:类型、原理与适用场景
java·开发语言·数据库
2301_771717212 小时前
解决mysql报错:1406, Data too long for column
android·数据库·mysql
小江的记录本2 小时前
【Kafka核心】架构模型:Producer、Broker、Consumer、Consumer Group、Topic、Partition、Replica
java·数据库·分布式·后端·搜索引擎·架构·kafka
dvjr cloi2 小时前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
dFObBIMmai3 小时前
MySQL主从同步中大事务导致的延迟_如何拆分大事务优化同步
jvm·数据库·python
szccyw03 小时前
mysql如何限制特定存储过程执行权限_MySQL存储过程安全访问
jvm·数据库·python
czlczl200209253 小时前
利用“延迟关联”优化 MySQL 巨量数据的深分页查询
数据库·mysql
ACP广源盛139246256734 小时前
IX8024与科学大模型的碰撞@ACP#筑牢科研 AI 算力高速枢纽分享
运维·服务器·网络·数据库·人工智能·嵌入式硬件·电脑
Elastic 中国社区官方博客4 小时前
ES|QL METRICS_INFO 和 TS_INFO:为你的时间序列数据建立目录
大数据·数据库·elasticsearch·搜索引擎·信息可视化·全文检索