---TOP99抓取
grant select,insert,update,delete on outln.ol$ to DBA_OWNER;
grant create any outline to DBA_OWNER;
grant drop any outline to DBA_OWNER;
grant alter any outline to DBA_OWNER;
grant select any table to DBA_OWNER;
grant update any table to DBA_OWNER;
grant delete any table to DBA_OWNER;
grant insert any table to DBA_OWNER;
grant execute on dbms_outln to DBA_OWNER;
grant select on v_$sqltext to DBA_OWNER;
grant select on v_$sql to DBA_OWNER;
grant select on dba_users to DBA_OWNER;
grant select on dba_outlines to DBA_OWNER;
-- Create table
create table DBA_OWNER.MY_TOP99_0215
(
sql_id varchar2(200),
HASH_VALUE NUMBER,
CHILD_NUMBER NUMBER,
PLAN_HASH_VALUE NUMBER,
PARSING_SCHEMA_ID NUMBER,
EXECUTIONS NUMBER,
CPU_PER_EXEC NUMBER,
GETS_PER_EXEC NUMBER,
SQLLEN NUMBER,
SQL_fullTEXT clob,
sql_plan_baselines varchar2(200)
)
tablespace DBADATA;
create or replace package DBA_OWNER.admin_outline is
Function get_sqltext(i_hash_value Number) Return clob;
Procedure get_topsql;
end admin_outline;
/
create or replace package body DBA_OWNER.admin_outline
Is
Function get_sqltext(i_hash_value Number)
Return clob
Is
v_clob Clob;
v_hash_value Number := i_hash_value;
cursor c_sql is select sql_text from v$sqltext where hash_value = v_hash_value order by piece;
v_sqltext varchar2(100);
begin
open c_sql;
loop
fetch c_sql into v_sqltext;
exit when c_sql%NOTFOUND;
v_clob:=v_clob||v_sqltext;
end loop;
close c_sql;
Return v_clob;
End get_sqltext;
Procedure get_topsql
Is
l_clob clob;
l_exist Number :=0;
l_sqllen Number;
Cursor l_Cur Is
with
a as (
select PARSING_SCHEMA_ID,
plan_hash_value,
hash_value,
length(sql_text) sqllen,
sql_text,
sql_id,
sql_plan_baseline,
child_number,
cpu_time,
buffer_gets,
executions,
sum(executions) over(partition by PARSING_SCHEMA_ID, plan_hash_value) as executions_by_plan,
sum(cpu_time) over(partition by PARSING_SCHEMA_ID, plan_hash_value) as cputime_by_plan,
sum(buffer_gets) over(partition by PARSING_SCHEMA_ID, plan_hash_value) as buffergets_by_plan,
sum(executions) over() as total_exec,
sum(cpu_time) over() as total_cpu,
sum(buffer_gets) over() as total_gets
from v$sql
Where plan_hash_value <> 0
And upper(sql_text) Not Like 'INSERT%INTO%VALUES%'
And upper(sql_fulltext) Not Like 'SELECT%NEXTVAL FROM DUAL%'
And PARSING_SCHEMA_ID not in
(select user_id from dba_users where username in ('GGMGR', 'FOGLIGHT', 'SYS',
'SYSTEM', 'OUTLN', 'DBSNMP',
'WMSYS', 'CTXSYS', 'XDB', 'MDSYS',
'ORDPLUGINS', 'ORDSYS', 'OLAPSYS', 'HR', 'OE',
'SCOTT', 'DIP', 'TSMSYS',
'EXFSYS', 'LBACSYS', 'TRACESVR',
'AURORA$JIS$UTILITY$',
'OSE$HTTP$ADMIN',
'VERITAS_I3_ORCL', 'QUEST',
'SPOTLIGHT', 'I3_ORCL',
'PERFSTAT', 'DBA_OWNER', 'OVSEE',
'DBMON', 'DBCONN', 'DBQUA',
'DEPLOYOP', 'DMLBAK', 'DBSTATS',
'APPMGR', 'TOADEXP', 'GGDEPLOY',
'DBACOMM', 'FGLPA', 'SQLTXPLAIN'))),
exec_by_plan_result as (
select e.*,round(e.pr/e.total_exec,5) as cumulate_exec_percent ,round(e.re,5) as exec_percent from
(select PARSING_SCHEMA_ID, plan_hash_value, hash_value,sqllen,sql_text,sql_id, sql_plan_baseline,child_number,cpu_time,buffer_gets,executions,executions_by_plan,
sum(executions) over(order by executions_by_plan desc,executions desc) pr,
RATIO_TO_REPORT(executions) OVER() as re,
total_exec
from a
order by executions_by_plan desc,executions desc) e) ,
exec_all_result as (
select e.*,round(e.pr/e.total_exec,5) as cumulate_exec_percent ,round(e.re,5) as exec_percent from (
select PARSING_SCHEMA_ID, plan_hash_value, hash_value,sqllen,sql_text,sql_id, sql_plan_baseline,child_number,cpu_time,buffer_gets,executions,
sum(executions) over(order by executions desc) pr,
RATIO_TO_REPORT(executions) OVER() as re,
total_exec
from a
order by executions desc) e),
cputime_by_plan_result as (
select c.*,round(c.pr/c.total_cpu,5) as cumulate_exec_percent,round(c.re,5) as exec_percent from
(select PARSING_SCHEMA_ID, plan_hash_value, hash_value,sqllen,sql_text,sql_id, sql_plan_baseline,child_number,cpu_time,buffer_gets,executions,cputime_by_plan,
sum(cpu_time) over(order by cputime_by_plan desc,cpu_time desc) pr,
RATIO_TO_REPORT(cpu_time) OVER() as re,
total_cpu
from a
order by cputime_by_plan desc,cpu_time desc) c),
cputime_all_result as (
select c.*,round(c.pr/c.total_cpu,5) as cumulate_exec_percent ,round(c.re,5) as exec_percent from (
select PARSING_SCHEMA_ID, plan_hash_value, hash_value,sqllen,sql_text,sql_id, sql_plan_baseline,child_number,cpu_time,buffer_gets,executions,
sum(cpu_time) over(order by cpu_time desc) pr,
RATIO_TO_REPORT(cpu_time) OVER() as re,
total_cpu
from a
order by cpu_time desc) c),
buffergets_by_plan_result as (
select b.*,round(b.pr/b.total_gets,5) as cumulate_exec_percent,round(b.re,5) as exec_percent from
(select PARSING_SCHEMA_ID, plan_hash_value, hash_value,sqllen,sql_text,sql_id, sql_plan_baseline,child_number,cpu_time,buffer_gets,executions,buffergets_by_plan,
sum(BUFFER_GETS) over(order by buffergets_by_plan desc,buffer_gets desc) pr,
RATIO_TO_REPORT(BUFFER_GETS) OVER() as re,
total_gets
from a
order by BUFFERGETS_by_plan desc,BUFFER_GETS desc) b),
buffergets_all_result as (
select b.*,round(b.pr/b.total_gets,5) as cumulate_exec_percent ,round(b.re,5) as exec_percent from (
select PARSING_SCHEMA_ID, plan_hash_value, hash_value,sqllen,sql_text,sql_id, sql_plan_baseline,child_number,cpu_time,buffer_gets,executions,
sum(BUFFER_GETS) over(order by BUFFER_GETS desc) pr,
RATIO_TO_REPORT(BUFFER_GETS) OVER() as re,
total_gets
from a
order by BUFFER_GETS desc) b)
Select Distinct sql_id, PARSING_SCHEMA_ID,hash_value,child_number,plan_hash_value,executions,cpu_per_exec,gets_per_exec,sqllen,sql_text,sql_plan_baseline from (
select PARSING_SCHEMA_ID, plan_hash_value, hash_value,sqllen,sql_text,sql_id, sql_plan_baseline,child_number,cpu_time,buffer_gets,executions
,round(decode(executions,'0','-1',cpu_time/executions),2) cpu_per_exec,
round( decode(executions,'0','-1',buffer_gets/executions),2) gets_per_exec
from exec_by_plan_result
where cumulate_exec_percent<=0.99
union all
select PARSING_SCHEMA_ID, plan_hash_value, hash_value,sqllen,sql_text,sql_id, sql_plan_baseline,child_number,cpu_time,buffer_gets,executions
,round(decode(executions,'0','-1',cpu_time/executions),2) cpu_per_exec,
round( decode(executions,'0','-1',buffer_gets/executions),2) gets_per_exec
from cputime_by_plan_result
where cumulate_exec_percent<=0.99
union all
select PARSING_SCHEMA_ID, plan_hash_value, hash_value,sqllen,sql_text,sql_id, sql_plan_baseline,child_number,cpu_time,buffer_gets,executions
,round(decode(executions,'0','-1',cpu_time/executions),2) cpu_per_exec,
round( decode(executions,'0','-1',buffer_gets/executions),2) gets_per_exec
from buffergets_by_plan_result
where cumulate_exec_percent<=0.99
union all
select PARSING_SCHEMA_ID, plan_hash_value, hash_value,sqllen,sql_text,sql_id, sql_plan_baseline,child_number,cpu_time,buffer_gets,executions
,round(decode(executions,'0','-1',cpu_time/executions),2) cpu_per_exec,
round( decode(executions,'0','-1',buffer_gets/executions),2) gets_per_exec
from exec_all_result
where cumulate_exec_percent<=0.99
union all
select PARSING_SCHEMA_ID, plan_hash_value, hash_value,sqllen,sql_text,sql_id, sql_plan_baseline,child_number,cpu_time,buffer_gets,executions
,round(decode(executions,'0','-1',cpu_time/executions),2) cpu_per_exec,
round( decode(executions,'0','-1',buffer_gets/executions),2) gets_per_exec
from cputime_all_result
where cumulate_exec_percent<=0.99
union all
select PARSING_SCHEMA_ID, plan_hash_value, hash_value,sqllen,sql_text,sql_id, sql_plan_baseline,child_number,cpu_time,buffer_gets,executions
,round(decode(executions,'0','-1',cpu_time/executions),2) cpu_per_exec,
round( decode(executions,'0','-1',buffer_gets/executions),2) gets_per_exec
from buffergets_all_result
where cumulate_exec_percent<=0.99
) ;
l_Rec l_Cur%Rowtype;
Begin
Open l_Cur;
Loop
Fetch l_Cur Into l_Rec;
Exit When l_Cur%Notfound;
Select Count(1) Into l_exist From MY_TOP99_0215 Where parsing_schema_id = l_rec.parsing_schema_id And hash_value = l_rec.hash_value and plan_hash_value=l_rec.plan_hash_value;
--如果已经存在,则跳过
If (l_exist > 0 ) Then
Null;
Else
If (l_Rec.Sqllen >= 900) Then
l_clob := Get_Sqltext(l_Rec.Hash_Value);
else
l_clob :=l_Rec.Sql_Text;
end if;
Insert Into MY_TOP99_0215(sql_id,Hash_Value,Child_Number,plan_hash_value,parsing_schema_id, Executions,Cpu_Per_Exec,Gets_Per_Exec, Sqllen,Sql_fullText, sql_plan_baselines)
Values
(l_rec.sql_id,l_Rec.Hash_Value,l_Rec.Child_Number,l_rec.plan_hash_value,l_rec.parsing_schema_id,l_Rec.Executions, l_Rec.Cpu_Per_Exec,l_Rec.Gets_Per_Exec, l_rec.sqllen,l_clob, l_rec.sql_plan_baseline);
End If;
l_exist :=0;
End Loop;
Close l_cur;
Commit;
End get_topsql;
DBA_OWNE
end admin_outline;
/
Declare
l_jno Number;
l_instid Number;
l_nextdate Date := to_date('2016-02-15 23:10:00','yyyy-mm-dd hh24:mi:ss');
l_what Varchar2(300) :='DBA_OWNER.admin_outline.get_topsql;';
l_interval Varchar2(300) := 'trunc(Sysdate,''MI'')+1/48';
Begin
For stid In (Select inst_id From gv$instance)
Loop
dbms_job.submit(job => l_jno,what =>l_what ,next_date =>l_nextdate,interval =>l_interval,instance => stid.inst_id);
Commit;
End Loop;
End;
/
Declare
l_jno Number;
l_instid Number;
l_what Varchar2(300) :='DBA_OWNER.admin_outline.get_topsql;';
Begin
For stid In (Select job,Instance From Dba_Jobs Where what = l_what)
Loop
dbms_job.remove(job => stid.job);
End Loop;
Commit;
End;
/