工作已经三个多月了,也积累了很多经验,脚本是一个可以简化工作的好工具,如果能用的恰到好处,那便可以事半功倍。
对于oracle DBA的工作,除了备份恢复之外,还要懂得日常维护,如果用几个简单的脚本,可以减轻每天重复的工作量,何乐为不为呢?
1.查看表空间使用情况的脚本 usedtablespace.sql
- select a.tablespace_name, round(a.total_size,1) "total(M)",
- round(a.total_size)-round(nvl(b.free_size,0),1) "used(M)",
- round(nvl(b.free_size,0),1) "free(M)",
- round(nvl(b.free_size,0)/total_size*100,1) "free rate(%)"
- from (select tablespace_name,sum(bytes)/1024/1024 total_size
- from dba_data_files
- group by tablespace_name) a,
- (select tablespace_name,sum(bytes)/1024/1024 free_size
- from dba_free_space
- group by tablespace_name) b
- where a.tablespace_name = b.tablespace_name(+)
- order by "free rate(%)";
执行结果
SQL> start usedtablespace.sql
TABLESPACE_NAME total(M) used(M) free(M) free rate(%)
-------------------- ---------- ---------- ---------- ------------
SYSTEM 720 712.4 7.6 1.1
SYSAUX 804.2 763.5 40.5 5
EXAMPLE 100 77.7 22.3 22.3
USERS 5 3.1 1.9 38.8
UNDOTBS1 145 21.3 123.7 85.3
CMS_DATA 2048 238 1810 88.4
WBSC_DATA 2048 151 1897 92.6
DBS_OSS_KPI_INDEX 512 34.9 477.1 93.2
DBS_OSS_KPI_DAT 1024 29.4 994.6 97.1
BIZ_DATA 2048 33 2015 98.4
DBS_OSS_SAT_DAT 1024 7.6 1016.4 99.3
- SELECT A.OWNER,
- A.OBJECT_NAME,
- B.XIDUSN,
- B.XIDSLOT,
- B.XIDSQN,
- B.SESSION_ID,
- B.ORACLE_USERNAME,
- B.OS_USER_NAME,
- B.PROCESS,
- B.LOCKED_MODE,
- C.MACHINE,
- C.STATUS,
- C.SERVER,
- C.SID,
- C.SERIAL#,
- C.PROGRAM
- FROM ALL_OBJECTS A,
- V$LOCKED_OBJECT B,
- SYS.GV_$SESSION C
- WHERE ( A.OBJECT_ID = B.OBJECT_ID )
- AND (B.PROCESS = C.PROCESS )
- ORDER BY 1,2;
- set heading off; --关闭表头
- set feedback off; --关闭回显
- spool /tmp/dropobj.sql;
- prompt --Drop constraint
- select 'alter table '||table_name||' drop constraint '||constraint_name||' ;' from user_constraints where constraint_type='R';
- prompt --Drop tables
- select 'drop table '||table_name ||';' from user_tables;
- prompt --Drop view
- select 'drop view ' ||view_name||';' from user_views;
- prompt --Drop sequence
- select 'drop sequence ' ||sequence_name||';' from user_sequences;
- prompt --Drop function
- select 'drop function ' ||object_name||';' from user_objects where object_type='FUNCTION';
- prompt --Drop procedure
- select 'drop procedure '||object_name||';' from user_objects where object_type='PROCEDURE';
- prompt --Drop package
- prompt --Drop package body
- select 'drop package '|| object_name||';' from user_objects where object_type='PACKAGE';
- prompt --Drop database link
- select 'drop database link '|| object_name||';' from user_objects where object_type='DATABASE LINK';
- spool off;
- set heading on;
- set feedback on;
- col sid for 999999
- col username for a10
- col schemaname for a10
- col osuser for a16
- col machine for a16
- col terminal for a20
- col owner for a10
- col object_name for a30
- col object_type for a10
- 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;