This small Oracle script can be executed as a script file from the SQL*Plus prompt and reports the total size, used space, free space and percentage of used and free space in all the tablespaces in a database. Very useful for DBAs! The user executing this script should have the select privilege on the dba_free_space and dba_data_files views. The script has been tested on Oracle 8i enterprise and standard editions.
/* TOTAL, FREE AND USED SPACE IN TABLESPACES */ SET LINESIZE 100 COLUMN TABLESPACE FORMAT A15 select t.tablespace, t.totalspace as " Totalspace(MB)", round((t.totalspace-fs.freespace),2) as "Used Space(MB)", fs.freespace as "Freespace(MB)", round(((t.totalspace-fs.freespace)/t.totalspace)*100,2) as "% Used", round((fs.freespace/t.totalspace)*100,2) as "% Free" from (select round(sum(d.bytes)/(1024*1024)) as totalspace, d.tablespace_name tablespace from dba_data_files d group by d.tablespace_name) t, (select round(sum(f.bytes)/(1024*1024)) as freespace, f.tablespace_name tablespace from dba_free_space f group by f.tablespace_name) fs where t.tablespace=fs.tablespace order by t.tablespace;
Paul S. writes: I find it frustrating to find a freespace tip that does not take into account the fact that if tablespace has no freespace then it in not listed in the table. The simple solution is to place an outer join in the where clause, as use nvl like so:
1> /* TOTAL, FREE AND USED SPACE IN TABLESPACES */ 2> SET LINESIZE 100 3> COLUMN TABLESPACE FORMAT A15 4> select t.tablespace, t.totalspace as " Totalspace(MB)", 5> round((t.totalspace-nvl(fs.freespace,0)),2) as "Used Space(MB)", 6> nvl(fs.freespace,0) as "Freespace(MB)", 7> round(((t.totalspace-nvl(fs.freespace,0))/t.totalspace)*100,2) as "%Used", 8> round((nvl(fs.freespace,0)/t.totalspace)*100,2) as "% Free" 9> from 10> (select round(sum(d.bytes)/(1024*1024)) as totalspace,d.tablespace_name tablespace 11> from dba_data_files d 12> group by d.tablespace_name) t, 13> (select round(sum(f.bytes)/(1024*1024)) as freespace,f.tablespace_name tablespace 14> from dba_free_space f 15> group by f.tablespace_name) fs 16> where t.tablespace=fs.tablespace (+) 17> order by t.tablespace;
Note changes on lines 5, 6, 7, 8, & 16.
For More Information
- Feedback: E-mail the editor with your thoughts about this tip.
- More tips: Hundreds of free Oracle tips and scripts.
- Tip contest: Have an Oracle tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
- Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
- Forums: Ask your technical Oracle questions--or help out your peers by answering them--in our active forums.
- Best Web Links: Oracle tips, tutorials, and scripts from around the Web.