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

目录
相关文章
|
SQL Java Shell
通过shell脚本抓取awr报告中的问题sql
awr报告中的sql明细部分基本必看的部分,尤其是SQL Order by Elapsed time这个部分,能够很清晰的看到哪些sql语句占用了较多的DB time,所占的比例。
1206 0
|
SQL XML 存储
SQL Server定时自动抓取耗时SQL并归档数据脚本分享
原文:SQL Server定时自动抓取耗时SQL并归档数据脚本分享 SQL Server定时自动抓取耗时SQL并归档数据脚本分享 第一步建库 USE [master] GO CREATE DATABASE [MonitorElapsedHighSQL] GO 第二步创建sp_who3存储过程 -- http://sqlserverplanet.
1318 0
|
SQL Go 数据库
如何有效抓取SQL Server的BLOCKING信息
原文: 如何有效抓取SQL Server的BLOCKING信息 转自:微软亚太区数据库技术支持组 官方博客 http://blogs.msdn.com/b/apgcdsd/archive/2011/12/12/sql-server-blocking.aspx SQL Server允许并发操作,BLOCKING是指在某一操作没有完成之前,其他操作必须等待,以便于保证数据的完整性。
904 0
|
SQL 索引
通过top命令抓取cpu高消耗的sql
top命令在linux环境维护中很实用,虽然功能缺失不够sar那么全面。今天和大家分享一个通过top命令来抓取性能sql的案例。 通过top命令抓取了如下的信息。
873 0
|
SQL 关系型数据库 MySQL
利用tcpdump抓取mysql sql语句
这个脚本是我之前在网上无意间找个一个利用tcpdump 抓包工具获取mysql流量,并通过过滤把sql 语句输入。 脚本不是很长,但是效果很好。 #!/bin/bash #this script used montor mysql network traffic.
1597 0
|
SQL 关系型数据库 Linux
[20140212]linux下使用tcpdump抓取sql语句
[20140212]linu下使用tcpdump抓取sql语句.txt 我们生产系统问题多多,经常要跟踪用户执行的sql语句,当出现问题时要跟踪比较麻烦,我需要一个快捷的方式"看到"用户执行的sql语 句,想到了tcpdump抓包软件。
1563 0
|
SQL 关系型数据库 MySQL
用Tcpdump抓取MySQL执行的SQL
编写脚本文件dumpsql.sh,内容如下: !/bin/bashtcpdump -i eth0 -s 0 -l -w out.
768 0
|
2月前
|
关系型数据库 MySQL 网络安全
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
|
4月前
|
SQL 存储 监控
SQL Server的并行实施如何优化?
【7月更文挑战第23天】SQL Server的并行实施如何优化?
115 13