当前位置: 代码网 > it编程>数据库>MsSqlserver > Oralce数据库健康和性能巡检监控的25个SQL脚本

Oralce数据库健康和性能巡检监控的25个SQL脚本

2025年05月18日 MsSqlserver 我要评论
oracle日常检查项的sql脚本:涵盖表空间状态、日志、锁、缓存命中率、sql性能、死锁处理等,确保数据库稳定运行,关键指标如缓冲区、数据字典命中率需达标,及时处理失效索引、僵死进程及扩展异常对象1

oracle日常检查项的sql脚本:涵盖表空间状态、日志、锁、缓存命中率、sql性能、死锁处理等,确保数据库稳定运行,关键指标如缓冲区、数据字典命中率需达标,及时处理失效索引、僵死进程及扩展异常对象

1 检查表空间使用情况

select b.tablespace_name          tablespace,
       a.extent_management        ext_mgt,
       a.segment_space_management seg_mgt,
       a.status,
       a.logging,
       b.total,
       b.free,
       b.used_pct
  from dba_tablespaces a,
       (select d.tablespace_name tablespace_name,
               round((d.sumbytes / 1024 / 1024 / 1024), 2) || 'gb' total,
               round(decode(f.sumbytes, null, 0, f.sumbytes) / 1024 / 1024 / 1024,
                     2) || 'gb' free,
               round((d.sumbytes - decode(f.sumbytes, null, 0, f.sumbytes)) * 100 /
                     d.sumbytes,
                     2) || '%' used_pct
          from (select tablespace_name, sum(bytes) sumbytes
                  from dba_free_space
                 group by tablespace_name) f,
               (select tablespace_name,     
                       sum(bytes) sumbytes
                  from dba_data_files
                 group by tablespace_name) d
         where f.tablespace_name(+) = d.tablespace_name
         order by d.tablespace_name) b
 where a.tablespace_name = b.tablespace_name;

2 检查是否有 offline 状态的表空间

select file_id as id,
       relative_fno "fno",
       round(bytes / 1024 / 1024) as mbytes,
       round(maxbytes / 1024 / 1024) maxmbytes,
       blocks,
       maxblocks,
       autoextensible "auto",
       increment_by "inc",
       round(user_bytes / 1024 / 1024) "now_mb",
       user_blocks,
       status,
       online_status "online_s"
  from dba_data_files;

3 在线日志是否存在小于 50m 的及状态不正常

select a.group#, a.status, a.bytes, b.member, a.archived
  from v$log a, v$logfile b
 where a.group# = b.group#;

4 检查锁阻塞

select decode(request, 0, '阻塞者:', '等待者:') || sid sid,
       id1,
       id2,
       lmode,
       request,
       type
  from v$lock
 where (id1, id2, type) in
       (select id1, id2, type from v$lock where request > 0)
 order by id1, request;

5 查看是否有僵死进程

select spid from v$process where addr not in (select paddr from v$session);

6 检查是否有失效索引

select owner, a.index_name, a.index_type, a.status
  from dba_indexes a
 where status = 'unusable';

select a.index_name, a.partition_name, a.tablespace_name, a.status
  from dba_ind_partitions a
 where status = 'unusable';

7 检查不起作用的约束

select owner, constraint_name, table_name, constraint_type, status
  from dba_constraints
 where status = 'disable'
   and constraint_type = 'p';

8 缓冲区命中率

缓冲命中率应大于90%。

select (1 - (sum(decode(name, 'physical reads', value, 0)) /
       (sum(decode(name, 'db block gets', value, 0)) +
       sum(decode(name, 'consistent gets', value, 0))))) * 100 "hit ratio"
  from v$sysstat;

9 数据字典命中率

数据字典命中率应大于 95%。

select (1 - (sum(getmisses) / sum(gets))) * 100 "hit ratio" from v$rowcache;

10 库缓存命中率

库缓存命中率应大于 95%。

select sum(pins) / (sum(pins) + sum(reloads)) * 100 "hit ratio" from v$librarycache;

11 内存中的排序

如果存在大量的磁盘排序,则表明检查目前系统中消耗大量磁盘的 sql 是否已经经过调整。

select name, value from v$sysstat where name like '%sorts%';

12 磁盘中的排序

检查使用磁盘排序的会话信息,可以定位执行了大量磁盘排序的会话。

select , a.sid, a.value
  from v$sesstat a, v$statname b
 where a.statistic# = b.statistic#
   and  = 'sorts (disk)'
   and a.value > 0
   and rownum < 10
 order by a.value desc;

13 临时空间使用率

select * from v$temp_space_header;

14 检查oracle实例状态

其中“status”表示oracle当前的实例状态,必须为“open”;“database_status”表示oracle当前数据库的状态,必须为“active”。

select instance_name, host_name, startup_time, status, database_status from v$instance;

15 检查oracle表空间的状态

输出结果中status应该都为online。

select tablespace_name, status from dba_tablespaces;

16 检查oracle所有数据文件状态

输出结果中“status”应该都为“online”。

select name, status from v$datafile;

输出结果中“status”应该都为“available”。

select file_name, status from dba_data_files;

17 检查所有回滚段状态

输出结果中所有回滚段的“status”应该为“online”。

select segment_name, status from dba_rollback_segs;

18 检查一些扩展异常的对象

如果有记录返回,则这些对象的扩展已经快达到它定义时的最大扩展值,对于这些对象要修改它的存储结构参数。

select segment_name,
       segment_type,
       tablespace_name,
       (extents / max_extents) * 100 percent
  from sys.dba_segments
 where max_extents != 0
   and (extents / max_extents) * 100 >= 95
 order by percent;

19 disk read最高的sql语句的获取

select sql_text
  from (select * from v$sqlarea order by disk_reads)
 where rownum <= 5;

20 性能最差的前10条sql

select *
  from (select parsing_user_id executions,
               sorts,
               command_type,
               disk_reads,
               sql_text
          from v$sqlarea
         order by disk_reads desc)
 where rownum < 10;

21 检查运行很久的sql

select username,
       sid,
       opname,
       round(sofar * 100 / totalwork, 0) || '%' as progress,
       time_remaining,
       sql_text
  from v$session_longops, v$sql
 where time_remaining <> 0
   and sql_address = address
   and sql_hash_value = hash_value;

22 检查碎片程度高的表

select segment_name table_name, count(*) extents
  from dba_segments
 where owner not in ('sys', 'system')
 group by segment_name
having count(*) = (select max(count(*))
                     from dba_segments
                    group by segment_name);

23 检查死锁及处理

select sid,
       serial#,
       username,
       schemaname,
       osuser,
       machine,
       terminal,
       program,
       owner,
       object_name,
       object_type,
       o.object_id
  from dba_objects o, v$locked_object l, v$session s
 where o.object_id = l.object_id
   and s.sid = l.session_id;

24 失效的触发器

select owner, trigger_name, table_name, status
  from dba_triggers
 where status = 'disabled';

25 失败的job

select job, what, last_date, next_date, failures, broken
  from dba_jobs
 where schema_user = 'user';

到此这篇关于oralce数据库健康和性能巡检监控的sql脚本的文章就介绍到这了,更多相关oralce数据库监控的sql脚本内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com