Oracle database tablespace report – SQL script
In this article I’ll share a SQL script that I probably use most often. The SQL will show Oracle database tablespace information. Many Oracle DBAs and developers use GUI tools nowadays and me also, but sometimes you need to see some extras. The SQL script will list Oracle database tablespaces including tablespace status and type, counts of files and objects and the most important – proper tablespace sizes.Before looking into the SQL script see my notes below:
– Script should work for Oracle 10g and 11g
– “Max Size” means the maximum size of tablespaces that can be achieved including data files auto-extention.
– “TS type” column meaning: DM – Dictionary Managed; LM-UNI/SYS – Locally managed with UNIFORM/SYSTEM extent management
– “Used size” for TEMP tablespace is HWM (largest ever used size)
* Script updated on Feb-2016:
3
| col tablespace_name format A22 heading "Tablespace"col segments format 999999 heading "Segments"col files format 9999col allocated_mb format 9,999,990.000 heading "Allocated Size|(MB)"col used_mb format 9,999,990.000 heading "Used Size|(MB)"col ts_type format A6 heading "TS|type"col max_size_mb format 9,999,990.000 heading "Max Size|(MB)"col max_free_mb format 9,999,990.000 heading "Max Free|(MB)"col max_free_pct format 999 heading "Max Free|%"BREAK ON REPORTCOMPUTE SUM LABEL "Total SUM:" OF objects files allocated_mb used_mb max_size_mb MAX_FREE_MB ON REPORTCOMPUTE AVG LABEL "Average %:" OF FREE_PCT MAX_FREE_PCT ON REPORTSELECT ts.tablespace_name, status,DECODE(dt.contents,'PERMANENT',DECODE(dt.extent_management,'LOCAL',DECODE(dt.allocation_type,'UNIFORM','LM-UNI','LM-SYS'),'DM'),'TEMPORARY','TEMP',dt.contents) ts_type,NVL(s.count,0) segments,ts.files,ts.allocated/1024/1024 allocated_mb,ROUND((ts.allocated-nvl(ts.free_size,0))/1024/1024,3) used_mb,ROUND(maxbytes/1024/1024,3) max_size_mb,ROUND((maxbytes-(ts.allocated-nvl(ts.free_size,0)))/1024/1024,3) max_free_mb,ROUND((maxbytes-(ts.allocated-nvl(ts.free_size,0)))*100/maxbytes,2) max_free_pctFROM(-- TS sizing info for permanent TSSELECT ddf.tablespace_name,files,allocated,NVL(free_size,0) free_size,maxbytes FROM (SELECT fs.tablespace_name, sum(fs.bytes) free_size FROM dba_free_space fs GROUP BY fs.tablespace_name) dfs, (SELECT df.tablespace_name, count(*) files, sum(df.bytes) allocated, sum(DECODE(df.maxbytes,0,df.bytes,df.maxbytes)) maxbytes, max(autoextensible) autoextensible FROM dba_data_files df WHERE df.status = 'AVAILABLE' GROUP BY df.tablespace_name) ddf WHERE dfs.tablespace_name (+)= ddf.tablespace_nameUNION-- Temp filesSELECT dtf.tablespace_name,files,allocated,free_size,maxbytesFROM(SELECT tf.tablespace_name, count(*) files, sum(tf.bytes) allocated,sum(DECODE(tf.maxbytes,0,tf.bytes,tf.maxbytes)) maxbytes, max(autoextensible) autoextensibleFROM dba_temp_files tfGROUP BY tf.tablespace_name)dtf,(SELECT th.tablespace_name, SUM (th.bytes_free) free_sizeFROM v$temp_space_header thGROUP BY tablespace_name)tshWHERE dtf.tablespace_name = tsh.tablespace_name) ts,( SELECT s.tablespace_name, count(*) countFROM dba_segments sGROUP BY s.tablespace_name) s,dba_tablespaces dt,v$parameter pWHERE p.name = 'db_block_size'AND ts.tablespace_name = dt.tablespace_nameAND ts.tablespace_name = s.tablespace_name (+)ORDER BY 1/ |
See below the expected SQL script output:
TS Allocated Size Used Size Max Size Max Free Max FreeTablespace STATUS type Objects FILES (Mb) (Mb) (Mb) (Mb) %--------------- --------- ------ ------- ----- -------------- ----------- -------------- -------------- --------EXAMPLE ONLINE LM-SYS 313 1 345.625 309.813 32,767.984 32,458.172 99SYSAUX ONLINE LM-SYS 4152 1 610.000 575.375 32,767.984 32,192.609 98SYSTEM ONLINE LM-SYS 1693 1 700.000 686.688 32,767.984 32,081.297 98TEMP ONLINE TEMP 0 1 59.000 59.000 32,767.984 32,708.984 100UNDOTBS1 ONLINE UNDO 10 1 830.000 13.250 32,767.984 32,754.734 100USERS ONLINE LM-SYS 41 1 5.000 4.000 32,767.984 32,763.984 100 ------- ----- -------------- ----------- -------------- -------------- --------Average %: 99Total SUM: 6209 6 2,549.625 1,648.126 196,607.904 194,959.7806 rows selected.