Oracle备份状态和慢SQL查询脚本

Posted Demonson

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle备份状态和慢SQL查询脚本相关的知识,希望对你有一定的参考价值。

一、备份状态脚本

1.创建备份查询视图

create or replace view backup_status_view as
select operation,
        status,
        object_type,
        mbytes_processed,
        start_time,
        end_time,
        round(input_bytes / 1024 / 1024 / 1024, 2) input_g,
        round(output_bytes / 1024 / 1024 / 1024, 2) output_g
   from v$rman_status
  where trunc(START_TIME)=trunc(sysdate)
  and object_type in ('DB FULL','DB INCR')
  and operation='BACKUP'
  order by start_time desc;

2.创建定时任务

select case status when 'COMPLETED' then 1 else 2 end as count 
from 
backup_status_view 
where   rownum =1

二、慢SQL查询脚本

1.创建慢sql视图

create or replace view slow_sql_view as
select  to_char(sysdate-1/24,'yyyy-mm-dd hh24') as snapshot_time,--快照时间
        v_1.sql_id,
        v_1.elapsed_time,--一小时内累计耗时
        v_1.cpu_time,--一小时内累计CPU时间
        v_1.iowait_time,--一小时内累计io等待时间
        v_1.gets,--一小时内累逻辑读
        v_1.reads,--一小时内累计物理读
        v_1.rws,--一小时内累计返回行数
        v_1.clwait_time,--一小时内累计集群等待时间
        v_1.execs,--一小时内累计执行次数
        v_1.elpe,--平均每条SQL消耗时间
        nvl(v_2.machine,'null') as machine,--客户服务器名称
        nvl(v_2.username,'null') as username,--客户连接用户名
        to_char(substr(v_1.sqt,1,3000)) as sql

from
  (select s.sql_id,
          round(elapsed_time / 1000000,2) elapsed_time,
          round(cpu_time / 1000000,2) cpu_time,
          round(iowait_time / 1000000,2) iowait_time,
          gets,
          reads,
          rws,
          round(clwait_time / 1000000,2) clwait_time,
          execs,
          st.sql_text sqt,
          round(elapsed_time / 1000000 / decode(execs, 0, null, execs),2) elpe
     from (select *
             from (select sql_id,
                          sum(executions_delta) execs,
                          sum(buffer_gets_delta) gets,
                          sum(disk_reads_delta) reads,
                          sum(rows_processed_delta) rws,
                          sum(cpu_time_delta) cpu_time,
                          sum(elapsed_time_delta) elapsed_time,
                          sum(clwait_delta) clwait_time,
                          sum(iowait_delta) iowait_time
                     from dba_hist_sqlstat
                    where snap_id =(select max(snap_id) from dba_hist_snapshot)
                    group by sql_id
                    order by sum(elapsed_time_delta) desc)
            where rownum <= 20) s,
          dba_hist_sqltext  st
    where st.sql_id = s.sql_id) v_1
    left join
  (select distinct a.sql_id, a.machine, b.username
     from dba_hist_active_sess_history a
     left join dba_users b
       on a.user_id = b.user_id
    where a.snap_id = (select max(snap_id) from dba_hist_snapshot)) v_2
      on v_1.sql_id = v_2.sql_id
      where v_1.elpe >=1  --平均执行时间大于1s的sql过滤出来
      order by elpe desc
;

2.创建定时任务

10 * * * * /usr/bin/python /u01/app/slow_log/oracle_slow.py > /u01/app/slow_log/exec_qurey_slow.log 2>&1
[oracle@CHN1-52-12 ~]$ cat /u01/app/slow_log/oracle_slow.py
#!/usr/bin/python
#coding=utf-8
import os
import cx_Oracle

os.environ['ORACLE_HOME'] = '/u01/app/oracle/product/11.2.0/db_1'
os.environ['ORACLE_SID'] = 'orcl'
os.environ['PATH']

#连接数据库,查视图
def slow_sql_qurey():
    conn = cx_Oracle.connect('oracle_user','pwd','')
    cursor=conn.cursor()
    lists = []
    try:
        cursor.execute ("select * from slow_sql_view")  
        #print("连接成功!")
        lists = cursor.fetchall()  
    except Exception:
        print("connenct oracle error,dblink error!",Exception)
    finally:
        cursor.close()  
        conn.close()  
    #print(lists)
	
    msg = ''
    if len(lists):
        for i in lists:
            msg = msg +  ",".join(map(str, i)) + '\\n'
        #print(msg)
        #将文件输出到文件
        f=open('/u01/app/slow_log/slow.log','w+')
        f.write(msg)
        f.close()

	
def main():
    slow_sql_qurey()
if __name__ == "__main__":
    main()

以上是关于Oracle备份状态和慢SQL查询脚本的主要内容,如果未能解决你的问题,请参考以下文章

Oracle 自动化备份脚本

mysql开启binlog日志和慢查询日志

mysql开启binlog日志和慢查询日志

oracle数据库如何每天自动执行脚本完成正式数据库到备份数据库的备份

ftp脚本

如何从 Java 中的 Oracle SQL 选择中获取原始脚本输出而不是查询结果