在较长时间的与Oracle的交往中,每个DBA都有各种各样的完成各种用途的脚本工具,这样很方便很快捷的完成了日常的工作,下面把我常用的一部分展现给大家,此篇主要侧重于数据库管理,这些脚本都经过严格测试。
1、 表空间统计
脚本说明:这是DBA最常用的一个脚本,用它可以显示出数据库中所有表空间的状态,如表空间的大小、已使用空间、使用的百分比、空闲空间数及现在表空间的最大块是多大。
SELECT upper(f.tablespace_name) "表空间名", d.Tot_grootte_Mb "表空间大小(M)",
d.Tot_grootte_Mb - f.total_bytes "已使用空间(M)",
to_char(round((d.Tot_grootte_Mb - f.total_bytes) / d.Tot_grootte_Mb * 100,2),'990.99') "使用比",
f.total_bytes "空闲空间(M)",f.max_bytes "最大块(M)"
FROM
(SELECT tablespace_name, round(SUM(bytes)/(1024*1024),2) total_bytes,
round(MAX(bytes)/(1024*1024),2) max_bytes
FROM sys.dba_free_space
GROUP BY tablespace_name) f,
(SELECT dd.tablespace_name, round(SUM(dd.bytes)/(1024*1024),2) Tot_grootte_Mb
FROM sys.dba_data_files dd
GROUP BY dd.tablespace_name) d
WHERE d.tablespace_name = f.tablespace_name ORDER BY 4 DESC;
2、查看无法扩展的段
脚本说明:ORACLE对一个段比如表段或索引段无法扩展时,取决的并不是表空间中剩余的空间是多少,而是取决于这些剩余空间中最大的块是否够表比索引的“NEXT”值大,所以有时一个表空间剩余几个G的空闲空间,在你使用时ORACLE还是提示某个表或索引无法扩展,就是由于这一点,这时说明空间的碎片太多了。这个脚本是找出无法扩展的段的一些信息。
SELECT segment_name, segment_type, owner, a.tablespace_name "tablespacename",
initial_extent/1024 "inital_extent(K)", next_extent/1024 "next_extent(K)",
pct_increase, b.bytes/1024 "tablespace max free space(K)",
b.sum_bytes/1024 "tablespace total free space(K)"
FROM dba_segments a,
(SELECT tablespace_name, MAX(bytes) bytes, SUM(bytes) sum_bytes FROM dba_free_space
GROUP BY tablespace_name) b
WHERE a.tablespace_name=b.tablespace_name
AND next_extent>b.bytes
ORDER BY 4,3,1;
3、查看段(表段、索引段)所使用空间的大小
脚本说明:有时你可能想知道一个表或一个索引占用多少M的空间,这个脚本就是满足你的要求的,把<>中的内容替换一下就可以了。
SELECT owner, segment_name, SUM(bytes)/1024/1024
FROM dba_segments
WHERE owner=<segment owner>
And segment_name=<your table or index name>
GROUP BY owner,segment_name
ORDER BY 3 DESC;