---
title: "dba常用检查脚本"
author: "vtyi"
date: "2009-05-26T17:32:37Z"
tags: []
slug: "dba"
draft: false
lastmod: "2009-05-27T17:38:37Z"
id: 396595
url: "https://vtyi.iteye.com/blog/396595"
---

检查警告文件，跟踪文件，备分情况

1.表空间的使用情况：

   select  a.tablespace\_name as table\_space, a.total\_size as total\_size,round(b.total\_free\_size,1) as  total\_free\_size,round((a.total\_size - b.total\_free\_size),2) as userd\_size,  to\_char(100\*b.total\_free\_size/a.total\_size,'99.99')||'%' as percent\_free  from (select tablespace\_name,sum(bytes)/1024/1024 as total\_size from dba\_data\_files group by tablespace\_name) a, (select tablespace\_name, sum(bytes/1024/1024) as total\_free\_size from dba\_free\_space  group by tablespace\_name) b  where a.tablespace\_name= b.tablespace\_name;

 

2.检查有无失效对象：

   select owner object\_owner,object\_name,object\_type,status from dba\_objects where owner not in('sys','system') and status='INVALID' order by owner,object\_type,object\_name;

 

3.检查有无失效索引：

   select  index\_name,index\_type,table\_name,status from dba\_indexes where status='UNSABLE';

 

4. 检查是否有表空间碎片:FSFI(free space fragmentation index) 100表示没有碎片，低于30%要处理

   select tablespace\_name, sqrt(max(blocks)/sum(blocks))\*(100/sqrt(sqrt(count(blocks)))) as FSFI  from dba\_free\_space group by tablespace\_name order by 1;

 

5.每月数据增长情况:

   select to\_char(creation\_time,'RRRR Month') as month, sum(bytes)/1024/1024/1024 growth\_in\_gb from sys.v\_$datafile  where creation\_time&gt;sysdate -365 group by to\_char(creation\_time,'RRRR Month');

 

6.每月表空间增长情况:

    select a.ts# as tablespace\_no, b.name as tablespace\_name, to\_char(creation\_time,'RRRR Month') month,sum(a.bytes)/1024/1024/1024  growht\_in\_gb from sys.v\_$datafile a, sys.v\_$tablespace b where a.creation\_time&gt;sysdate-365 and a.ts#=b.ts# group by a.ts#,b.name,to\_char(a.creation\_time,'RRRR Month');

 

7.在线重做日志文件的创建:

    添加一个组

   alter database add logfile group 4('/u01/oracle/data/redo\_4a.log','/u02/oracle/data/redo\_4b.log') size 5M;

    添加到某个组:

    alter database add logfile member  '/u03/oracle/data/redo\_4c.log' to group 3;

  系统提供: v$log       v$logfile

 

 

确认数据文件的使用量:

       select d.tablespace\_name tablespace,d.file\_name filename,d.bytes total\_file\_size,nvl((d.bytes-s.bytes),d.bytes) userd\_size,trunc(((nvl((d.bytes-s.bytes),d.bytes))/d.bytes)\*100) pct\_used  from sys.dba\_data\_files d,v$datafile v,(select file\_id,sum(bytes) bytes from sys.dba\_free\_space group by file\_id) s   where (s.file\_id(+)=d.file\_id) and (d.file\_name=v.name) union select  d.tablespace\_name tablespace,d.file\_name filename,d.bytes total\_file\_size,nvl(t.bytes\_cached,0)  used\_size  ,trunc((t.bytes\_cached/d.bytes)\*100) pct\_used  from sys.dba\_temp\_files  d,v$temp\_extent\_pool t,v$tempfile v where (t.file\_id(+)=d.file\_id) and (d.file\_id=v.file#);

 

确认数据库缓冲快取区的hit ratio：

    select 1-(phy.value/(cur.value+con.value)) cache\_hit\_ratio, round((1-(phy.value/(cur.value+con.value)))\*100,2)  ratio   from v$sysstat cur,v$sysstat con,v$sysstat phy  where cur.name='DB BLOCK GETS' and con.name='CONSISTENT GETS'  and phy.name='PHYSICAL READS';

 

 

确认使用者联机使用PGA的大小:

   select username, name, value  from v$statname n,v$session s,v$sesstat t  where s.sid=t.sid and n.statistic#=t.statistic#   and s.type='USER'  and  s.username is not null and n.name ='SESSION PGA MEMORY'  and username not like 'SYSTEM' and t.value &gt;30000;