[ORACLE]管理方面的脚本收集

-----------------------------Cryking原创------------------------------

-----------------------转载请注明出处,谢谢!------------------------

1.查询AWR相关的视图名称

SELECT table_name
  FROM dba_tables t
 WHERE table_name LIKE 'WRH$%'
       AND NOT EXISTS (SELECT 'x'
          FROM dba_tab_columns c
         WHERE c.column_name = 'SNAP_ID'
               AND c.table_name = t.table_name);

2. CPU耗时查询

with AASSTAT as (
           select
                 decode(n.wait_class,'User I/O','User I/O',
                                     'Commit','Commit',
                                     'Wait')                               CLASS,
                 sum(round(m.time_waited/m.INTSIZE_CSEC,3))                AAS,
                 BEGIN_TIME ,
                 END_TIME
           from  v$waitclassmetric  m,
                 v$system_wait_class n
           where m.wait_class_id=n.wait_class_id
             and n.wait_class != 'Idle'
           group by  decode(n.wait_class,'User I/O','User I/O', 'Commit','Commit', 'Wait'), BEGIN_TIME, END_TIME
          union
             select 'CPU_ORA_CONSUMED'                                     CLASS,
                    round(value/100,3)                                     AAS,
                 BEGIN_TIME ,
                 END_TIME
             from v$sysmetric
             where metric_name='CPU Usage Per Sec'
               and group_id=2
          union
            select 'CPU_OS'                                                CLASS ,
                    round((prcnt.busy*parameter.cpu_count)/100,3)          AAS,
                 BEGIN_TIME ,
                 END_TIME
            from
              ( select value busy, BEGIN_TIME,END_TIME from v$sysmetric where metric_name='Host CPU Utilization (%)' and group_id=2 ) prcnt,
              ( select value cpu_count from v$parameter where name='cpu_count' )  parameter
          union
             select
               'CPU_ORA_DEMAND'                                            CLASS,
               nvl(round( sum(decode(session_state,'ON CPU',1,0))/60,2),0) AAS,
               cast(min(SAMPLE_TIME) as date) BEGIN_TIME ,
               cast(max(SAMPLE_TIME) as date) END_TIME
             from v$active_session_history ash
              where SAMPLE_TIME >= (select BEGIN_TIME from v$sysmetric where metric_name='CPU Usage Per Sec' and group_id=2 )
               and SAMPLE_TIME < (select END_TIME from v$sysmetric where metric_name='CPU Usage Per Sec' and group_id=2 )
)
select
       to_char(BEGIN_TIME,'HH:MI:SS') BEGIN_TIME,
       to_char(END_TIME,'HH:MI:SS') END_TIME,
       ( decode(sign(CPU_OS-CPU_ORA_CONSUMED), -1, 0, (CPU_OS - CPU_ORA_CONSUMED )) +
       CPU_ORA_CONSUMED +
        decode(sign(CPU_ORA_DEMAND-CPU_ORA_CONSUMED), -1, 0, (CPU_ORA_DEMAND - CPU_ORA_CONSUMED ))) CPU_TOTAL,
       decode(sign(CPU_OS-CPU_ORA_CONSUMED), -1, 0, (CPU_OS - CPU_ORA_CONSUMED )) CPU_OS,
       CPU_ORA_CONSUMED CPU_ORA,
       decode(sign(CPU_ORA_DEMAND-CPU_ORA_CONSUMED), -1, 0, (CPU_ORA_DEMAND - CPU_ORA_CONSUMED )) CPU_ORA_WAIT,
       COMMIT,
       READIO,
       WAIT
from (
select
       min(BEGIN_TIME) BEGIN_TIME,
       max(END_TIME) END_TIME,
       sum(decode(CLASS,'CPU_ORA_CONSUMED',AAS,0)) CPU_ORA_CONSUMED,
       sum(decode(CLASS,'CPU_ORA_DEMAND'  ,AAS,0)) CPU_ORA_DEMAND,
       sum(decode(CLASS,'CPU_OS'          ,AAS,0)) CPU_OS,
       sum(decode(CLASS,'Commit'          ,AAS,0)) COMMIT,
       sum(decode(CLASS,'User I/O'        ,AAS,0)) READIO,
       sum(decode(CLASS,'Wait'            ,AAS,0)) WAIT
from AASSTAT)

3.  等待事件信息

select
    sid sw_sid,
    CASE WHEN state != 'WAITING' THEN 'WORKING'
         ELSE 'WAITING'
    END AS state,
    CASE WHEN state != 'WAITING' THEN 'On CPU / runqueue'
         ELSE event
    END AS sw_event,
    seq#,
    seconds_in_wait sec_in_wait,
    CASE state WHEN 'WAITING' THEN NVL2(p1text,p1text||'= ',null)||CASE WHEN P1 < 536870912 THEN to_char(P1) ELSE '0x'||rawtohex(P1RAW) END ELSE null END SW_P1,
    CASE state WHEN 'WAITING' THEN NVL2(p2text,p2text||'= ',null)||CASE WHEN P2 < 536870912 THEN to_char(P2) ELSE '0x'||rawtohex(P2RAW) END ELSE null END SW_P2,
    CASE state WHEN 'WAITING' THEN NVL2(p3text,p3text||'= ',null)||CASE WHEN P3 < 536870912 THEN to_char(P3) ELSE '0x'||rawtohex(P3RAW) END ELSE null END SW_P3,
    CASE state WHEN 'WAITING' THEN
        CASE
            WHEN event like 'cursor:%' THEN
                '0x'||trim(to_char(p1, 'XXXXXXXXXXXXXXXX'))
                    WHEN (event like 'enq%' OR event = 'DFS lock handle') AND state = 'WAITING' THEN
                '0x'||trim(to_char(p1, 'XXXXXXXXXXXXXXXX'))||': '||
                chr(bitand(p1, -16777216)/16777215)||
                chr(bitand(p1,16711680)/65535)||
                ' mode '||bitand(p1, power(2,14)-1)
            WHEN event like 'latch%' AND state = 'WAITING' THEN
                  '0x'||trim(to_char(p1, 'XXXXXXXXXXXXXXXX'))||': '||(
                        select name||'[par'
                            from v$latch_parent
                            where addr = hextoraw(trim(to_char(p1,rpad('0',length(rawtohex(addr)),'X'))))
                        union all
                        select name||'[c'||child#||']'
                            from v$latch_children
                            where addr = hextoraw(trim(to_char(p1,rpad('0',length(rawtohex(addr)),'X'))))
                  )
            WHEN event like 'library cache pin' THEN
                  '0x'||RAWTOHEX(p1raw)
        ELSE NULL END
    ELSE NULL END AS sw_p1transl
FROM
    v$session_wait
ORDER BY
    state,
    sw_event,
    p1,
    p2,
    p3;

4.  查询当前监听的连接信息

SELECT host_short || '.' || sid || '=
    (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = ' || hostname || ')(PORT = ' || port || '))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      ' || CASE
         WHEN instr(service_name, 'XDB') > 0 THEN
          NULL
         ELSE
          '(SERVICE_NAME = ' || service_name || ')'
       END || '(SID = ' || SID || ')
    ))
  '
  FROM (SELECT --target_name,
         UPPER(host_name) hostname
        ,UPPER(SUBSTR(t.host_name, 1, INSTR(t.host_name, '.') - 1)) HOST_SHORT
        ,(SUBSTR(t.host_name
                ,INSTR(t.host_name, '.') + 1
                ,LENGTH(t.host_name))) DOMAIN
        ,(SELECT p.property_value
            FROM mgmt$target_properties p
           WHERE p.property_name = 'Port'
                 AND p.target_guid = t.target_guid) port
        ,'sys/[email protected]' || host_name || ':' ||
         (SELECT p.property_value
            FROM mgmt$target_properties p
           WHERE p.property_name = 'Port'
                 AND p.target_guid = t.target_guid) || '/' ||
         (SELECT p.property_value
            FROM mgmt$target_properties p
           WHERE p.property_name = 'ServiceName'
                 AND p.target_guid = t.target_guid) || ' as sysdba' Connection_string
        ,(SELECT tp.property_value
            FROM mgmt$target_properties tp
           WHERE tp.target_type = 'host'
                 AND tp.property_name = 'IP_address'
                 AND tp.target_name = t.host_name) ip
        ,(SELECT p.property_value
            FROM mgmt$target_properties p
           WHERE p.property_name = 'DBVersion'
                 AND p.target_guid = t.target_guid) DB_Version
        ,(SELECT p.property_value
            FROM mgmt$target_properties p
           WHERE p.property_name = 'OracleHome'
                 AND p.target_guid = t.target_guid) oh
        ,(SELECT p.property_value
            FROM mgmt$target_properties p
           WHERE p.property_name = 'ServiceName'
                 AND p.target_guid = t.target_guid) Service_name
        ,(SELECT p.property_value
            FROM mgmt$target_properties p
           WHERE p.property_name = 'log_archive_mode'
                 AND p.target_guid = t.target_guid) logmode
        ,UPPER((SELECT p.property_value
                 FROM mgmt$target_properties p
                WHERE p.property_name = 'SID'
                      AND p.target_guid = t.target_guid)) sid
        ,(SELECT p.property_value
            FROM mgmt$target_properties p
           WHERE p.property_name = 'CPUCount'
                 AND p.target_guid = t.target_guid) CPU
        ,ROUND(SYSDATE - TO_DATE((SELECT p.property_value
                                   FROM mgmt$target_properties p
                                  WHERE p.property_name = 'StartTime'
                                        AND p.target_guid = t.target_guid)
                                ,'YYYY-MM-DD HH24:MI:SS')
              ,0) Days_Uptime
        ,(SELECT p.property_value
            FROM mgmt$target_properties p
           WHERE p.property_name = 'StartTime'
                 AND p.target_guid = t.target_guid) Uptime
        ,(SELECT p.property_value
            FROM mgmt$target_properties p
           WHERE p.property_name = 'VersionCategory'
                 AND p.target_guid = t.target_guid) VersionCategory
        ,(SELECT p.property_value
            FROM mgmt$target_properties p
           WHERE p.property_name = 'VersionBanner'
                 AND p.target_guid = t.target_guid) VersionBanner
        ,CASE
           WHEN (INSTR((SELECT UPPER(p.property_value)
                         FROM mgmt$target_properties p
                        WHERE p.property_name = 'VersionBanner'
                              AND p.target_guid = t.target_guid)
                      ,'ENTERPRISE')) > 0 THEN
            'Enterprise'
           ELSE
            'Standard/Standard One'
         END Edition
        ,(SELECT b.VALUE
            FROM MGMT$ECM_VISIBLE_SNAPSHOTS     A
                ,SYSMAN.MGMT_DB_INIT_PARAMS_ECM B
           WHERE A.ECM_SNAPSHOT_ID = B.ECM_SNAPSHOT_ID
                 AND a.TARGET_TYPE = 'oracle_database'
                 AND b.name = 'control_file_record_keep_time'
                 AND a.target_guid = t.target_guid) control_file_record_keep_time
        ,(SELECT b.VALUE
            FROM MGMT$ECM_VISIBLE_SNAPSHOTS     A
                ,SYSMAN.MGMT_DB_INIT_PARAMS_ECM B
           WHERE A.ECM_SNAPSHOT_ID = B.ECM_SNAPSHOT_ID
                 AND a.TARGET_TYPE = 'oracle_database'
                 AND b.name = 'optimizer_features_enable'
                 AND a.target_guid = t.target_guid) optimizer_features_enable
        ,(SELECT ROUND(b.VALUE / 1024 / 1024 / 1024, 2)
            FROM MGMT$ECM_VISIBLE_SNAPSHOTS     A
                ,SYSMAN.MGMT_DB_INIT_PARAMS_ECM B
           WHERE A.ECM_SNAPSHOT_ID = B.ECM_SNAPSHOT_ID
                 AND a.TARGET_TYPE = 'oracle_database'
                 AND b.name = 'memory_target'
                 AND a.target_guid = t.target_guid) memory_target
        ,(SELECT sessions_highwater
            FROM MGMT$ECM_VISIBLE_SNAPSHOTS A
                ,SYSMAN.MGMT_DB_license_ECM B
           WHERE A.ECM_SNAPSHOT_ID = B.ECM_SNAPSHOT_ID
                 AND TARGET_TYPE = 'oracle_database'
                 AND a.target_guid = t.target_guid) sessions_highwater
        ,(SELECT sessions_current
            FROM MGMT$ECM_VISIBLE_SNAPSHOTS A
                ,SYSMAN.MGMT_DB_license_ECM B
           WHERE A.ECM_SNAPSHOT_ID = B.ECM_SNAPSHOT_ID
                 AND TARGET_TYPE = 'oracle_database'
                 AND a.target_guid = t.target_guid) sessions_current
          FROM mgmt$target t
         WHERE t.target_type IN ('oracle_database')) Raw_data
 ORDER BY host_short
         ,sid;

更多的DBA脚本见一老外的BLOG: http://www.idevelopment.info/cgi/ORACLE_dba_scripts.cgi

[ORACLE]管理方面的脚本收集,布布扣,bubuko.com

时间: 2024-10-06 09:07:08

[ORACLE]管理方面的脚本收集的相关文章

Linux之进程管理及Shell脚本

使用!来调用过往命令 !! 重复执行上一条指令 !a 重复执行上一条以a为首的指令 !nubmer 重复执行上一条在history表中记录号码为number的指令 !-number重复执行前第number条指令 ailas abc='cd xxxxxxxxxxx/xxxxxxxx/xxxxxxxxxxxxx' unalias abc " "将一串字符当成字符串来看,可以转译特殊字符 ' '将一串字符当成字符串来看,无法转译特殊字符 ·能够返回命令执行的结果 echo `uname -a

Oracle 性能相关常用脚本(SQL)

在缺乏的可视化工具来监控数据库性能的情形下,常用的脚本就派上用场了,下面提供几个关于Oracle性能相关的脚本供大家参考.以下脚本均在Oracle 10g测试通过,Oracle 11g可能要做相应调整. 1.寻找最多BUFFER_GETS开销的SQL 语句 [sql] view plain copy print? --filename: top_sql_by_buffer_gets.sql --Identify heavy SQL (Get the SQL with heavy BUFFER_G

InstallShield在MySQL和Oracle中执行SQL脚本的方法InstallShield在MySQL和Oracle中执行SQL脚本的方法

简述 InstallShield已经内建了对MySQL和Oracle的支持.但是这个功能是通过ODBC实现的,它对SQL脚本的格式要求非常严格,因此已经通过官方客户端测试的脚本在IS中执行时往往就会报错. 一般来说,数据库脚本只保证通过官方客户端测试即可,同时维护一份供IS执行的脚本费时费力.因此,考虑安装程序对两数据库的支持通过官方客户端实现. MySQL   function InstallMySQLComponent(szComponent) NUMBER nResult; STRING

Oracle 之 安装配置脚本

#!/bin/bash #安装oracle所需的软件包 yum -y install binutils compat-libcap1 compat-libstdc++ gcc gcc-c++ glibc glibc-devel ksh libgcc libstdc++ libstdc++-devel libaio libaio-devel make sysstat unixODBC unixODBC-devel >/dev/null 2>&1 #若没安装图形界面,则用下面指令安装 rp

Oracle每天自动备份脚本

因为需要最近写了一个适用于Oracle上的备份脚本,Oracle的数据备份不像MySQL一样直接使用二进制工具就可以备份,在Oracle上进行数据备份需要 做好相应的配置,在配置好后才能进行正常的数据备份脚本,所以在Oracle备份数据前需要做一些配置. 首先,因为Oracle的11g开始的特性,空表不会分配表空间所以在备份脚本中我使用了expdp工具来进行数据备份,在备份开始之前需要先把Oracle的数据泵导出目录用DBA账户先做好指定并且授权读写,再修改目录的属组,操作很简单,就是一些exp

mysql oracle静默 一键安装脚本

pre-read; 为了达到一键搞定的目的!现Ruiy简单做如下几小条规定   如果你想这么一键来搞定请君莫要违背约束!1. 下载 `二进制` mysql软件介质版本不限,二进制包务必,源码及rpm包格式,此处玩不转;eg:    wget http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.17-linux-glibc2.5-x86_64.tar.gz 2. 将下载下来的二进制mysql安装介质放到/opt/yoodo/installs/目录,

Linux Oracle服务启动&amp;停止脚本与开机自启动

在CentOS 6.3下安装完Oracle 10g R2,重开机之后,你会发现Oracle没有自行启动,这是正常的,因为在Linux下安装Oracle的确不会自行启动,必须要自行设定相关参数,首先先介绍一般而言如何启动oracle. 一.在Linux下启动Oracle 登录到CentOS,切换到oracle用户权限 # su – oracle 接着输入: $ sqlplus "/as sysdba" 原本的画面会变为SQL> 接着请输入SQL> startup 就可以正常的

centOS 6 服务管理与服务脚本

服务管理与服务脚本 linux服务 服务管理与服务脚本 linux服务 服务启动过程详解 chkconfig命令 非独立服务与xinetd进程 一个特殊的服务脚本 服务启动过程详解 在开机启动过程中,我们计算机的各种服务也会按照配置信息启动服务,那个我们的服务使如何启动的呢?我们以 init 3模式为例,进行说明: 上图中:rc 3 --> 意味着读取/etc/rc.d/rc3.d/下的内容,现在我们看看/etc/rc.d/rc3.d/下有什么东西. 在/etc/rc.d/rc3.d/目录下,我

Oracle 12c Lnux 启动脚本

#!/bin/bash # #oracle: Start/Stop Oracle Database 11g R2/12c # # chkconfig: 345 90 10 # description: The Oracle Database Server is an RDBMS created by Oracle Corporation # # processname: oracle . /etc/rc.d/init.d/functions LOCKFILE=/var/lock/subsys/o