开发者社区> jan1990> 正文

top99sql抓取

简介: ---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;gra...
+关注继续查看

---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;
/

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
如何设置阿里云服务器安全组?阿里云安全组规则详细解说
阿里云安全组设置详细图文教程(收藏起来) 阿里云服务器安全组设置规则分享,阿里云服务器安全组如何放行端口设置教程。阿里云会要求客户设置安全组,如果不设置,阿里云会指定默认的安全组。那么,这个安全组是什么呢?顾名思义,就是为了服务器安全设置的。安全组其实就是一个虚拟的防火墙,可以让用户从端口、IP的维度来筛选对应服务器的访问者,从而形成一个云上的安全域。
18580 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
27723 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,大概有三种登录方式:
12967 0
阿里云服务器安全组设置内网互通的方法
虽然0.0.0.0/0使用非常方便,但是发现很多同学使用它来做内网互通,这是有安全风险的,实例有可能会在经典网络被内网IP访问到。下面介绍一下四种安全的内网互联设置方法。 购买前请先:领取阿里云幸运券,有很多优惠,可到下文中领取。
21933 0
阿里云服务器ECS登录用户名是什么?系统不同默认账号也不同
阿里云服务器Windows系统默认用户名administrator,Linux镜像服务器用户名root
15287 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
19980 0
腾讯云服务器 设置ngxin + fastdfs +tomcat 开机自启动
在tomcat中新建一个可以启动的 .sh 脚本文件 /usr/local/tomcat7/bin/ export JAVA_HOME=/usr/local/java/jdk7 export PATH=$JAVA_HOME/bin/:$PATH export CLASSPATH=.
14852 0
+关注
43
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
JS零基础入门教程(上册)
立即下载
性能优化方法论
立即下载
手把手学习日志服务SLS,云启实验室实战指南
立即下载