Wednesday, 15 March 2017

How to generate AWR & ASH from SQLPLUS or through procedure

set serveroutput on;
declare
dbid number;
instance_id number;
start_id number;
end_id number;
begin
dbms_output.enable(1000000);
select max(snap_id)-1,max(snap_id) into start_id,end_id from dba_hist_snapshot;
/* If you know the snap_id then comment the above line  and use the below code
start_id :=1233;
end_id := 1234;
*/
select dbid into dbid from v$database;
select instance_number into instance_id from v$instance;

--dbms_output.put_line('<PRE>');
for rc in ( select output from
   table(dbms_workload_repository.awr_report_text(dbid,instance_id,start_id,end_id))
           ) loop
  -- dbms_output.put_line('<BR>');
   dbms_output.put_line(rc.output);
end loop;
--dbms_output.put_line('</PRE>');
end;

FOR AWR html output

declare
dbid number;
instance_id number;
start_id number;
end_id number;
begin
dbms_output.enable(1000000);
select max(snap_id)-1,max(snap_id) into start_id,end_id from dba_hist_snapshot;
/* If you know the snap_id then comment the above line  and use the below code
start_id :=1233;
end_id := 1234;
*/
select dbid into dbid from v$database;
select instance_number into instance_id from v$instance;

--dbms_output.put_line('<PRE>');
for rc in ( select output from
   table(dbms_workload_repository.awr_report_html(dbid,instance_id,start_id,end_id))
           ) loop
  -- dbms_output.put_line('<BR>');
   dbms_output.put_line(rc.output);
end loop;
--dbms_output.put_line('</PRE>');
end;

/*privileges needed


grant select any dictionary to username;
grant SELECT ON SYS.V_$DATABASE  to username;
grant SELECT ON SYS.V_$INSTANCE to username;
grant EXECUTE ON SYS.DBMS_WORKLOAD_REPOSITORY  to username;
grant SELECT ON SYS.DBA_HIST_DATABASE_INSTANCE to username;
grant SELECT ON SYS.DBA_HIST_SNAPSHOT to username;
grant ADVISOR to username;

*/

ASH Report for the past 30 minutes

declare
dbid number;
instance_id number;
begin
select dbid into dbid from v$database;
select instance_number into instance_id from v$instance;
dbms_output.enable(500000);
dbms_output.put_line('<PRE>');
for rc in ( select output from
   table(dbms_workload_repository.ash_report_text( dbid,instance_id,SYSDATE-31/1440, SYSDATE-1/1440))) loop
   dbms_output.put_line(rc.output);
end loop;
dbms_output.put_line('</PRE>') ;
end;

for ASH HTML output

declare
dbid number;
instance_id number;
begin
select dbid into dbid from v$database;
select instance_number into instance_id from v$instance;
dbms_output.enable(500000);
dbms_output.put_line('<PRE>');
for rc in ( select output from
   table(dbms_workload_repository.ash_report_html( dbid,instance_id,SYSDATE-31/1440, SYSDATE-1/1440))) loop
   dbms_output.put_line(rc.output);
end loop;
dbms_output.put_line('</PRE>') ;
end;

No comments:

Post a Comment