Thursday, 13 January 2022

Oracle Queries to check space or storage related details.

 Below are the few queries to check space related stuff in Oracle database.


How to Check Tablespace Details:

set pagesize 2000 lines 200
col TOTAL_MB  format 999,999,999,999
col FREE_MB   format 999,999,999,999
col USED_MB   format 999,999,999,999
col PCT_FREE format 999 heading 'PERCENT|FREE' just center
col TS_NAME format a30
SELECT a.tablespace_name TS_NAME,
       SUM(a.tots) TOTAL_MB,
       SUM(a.sumb) FREE_MB,
       SUM(a.tots)- SUM(a.sumb) USED_MB,
       SUM(a.sumb)*100/SUM(a.tots) PCT_FREE
FROM    ( SELECT tablespace_name,
                   0 tots,
                   SUM(bytes)/(1024*1024) sumb
              FROM DBA_FREE_SPACE a
              GROUP BY tablespace_name
            UNION
            SELECT     tablespace_name,SUM(bytes)/(1024*1024) tots,0
              FROM     DBA_DATA_FILES
              GROUP BY tablespace_name) a
GROUP BY a.tablespace_name
UNION
SELECT
        f.tablespace_name TS_NAME,
        sum(d.bytes/(1024*1024)) TOTAL_MB,
        round(SUM(ROUND(((f.bytes_free + f.bytes_used) - nvl(p.bytes_used, 0)) / 1024 / 1024, 2)),0) FREE_MB,
        round(SUM(ROUND(nvl(p.bytes_used, 0) / 1024 / 1024, 2)),0) USED_MB,
        (100-round((SUM(ROUND(nvl(p.bytes_used, 0)/1024/1024, 2)) * 100) / (SUM(ROUND((f.bytes_free + f.bytes_used)/1024/ 1024, 2))),2)) PCT_FREE
FROM
        sys.v_$temp_space_header f,
        dba_temp_files d,
        sys.v_$temp_extent_pool p
WHERE f.tablespace_name(+) = d.tablespace_name
AND   f.file_id(+) = d.file_id
AND   p.file_id(+) = d.file_id
GROUP BY f.tablespace_name;


How to Check  Tablespace details with additional Information.

set feed off

column "tablespace_name" heading "Tablespace | Name" format a20
column "FileCount" heading "File | Count" format 999999
column "Size(MB)" heading "Size | (MB)" format 999,999,999.99
column "Free(MB)" heading "Free | (MB)" format 999,999,999.99
column "Used(MB)" heading "Used | (MB)" format 999,999,999.99
column "Max Ext(MB)" heading "Max Ext | (MB)" format 999,999,999
column "%Free" heading "% | Free" format 999.99
column "%Free Ext" heading "% | Free Ext" format 999.99
column "Graph" heading "Graph" format a11
column tablespace_name heading "Tablespace | Name" format a20


SELECT
   ts.tablespace_name, "File Count",
   TRUNC("SIZE(MB)", 2) "Size(MB)",
   TRUNC(fr."FREE(MB)", 2) "Free(MB)",
   TRUNC("SIZE(MB)" - "FREE(MB)", 2) "Used(MB)",
   df."MAX_EXT" "Max Ext(MB)",
   (fr."FREE(MB)" / df."SIZE(MB)") * 100 "% Free",
   RPAD('*', TRUNC(CEIL((fr."FREE(MB)" / df."SIZE(MB)") * 100)/10), '*')    "Graph"
FROM
   (SELECT tablespace_name,
   SUM (bytes) / (1024 * 1024) "FREE(MB)"
   FROM dba_free_space
    GROUP BY tablespace_name) fr,
(SELECT tablespace_name, SUM(bytes) / (1024 * 1024) "SIZE(MB)", COUNT(*)
"File Count", SUM(maxbytes) / (1024 * 1024) "MAX_EXT"
FROM dba_data_files
GROUP BY tablespace_name) df,
(SELECT tablespace_name
FROM dba_tablespaces) ts
WHERE fr.tablespace_name = df.tablespace_name (+)
AND fr.tablespace_name = ts.tablespace_name (+)
ORDER BY "% Free" desc
/


How to Check Particular tablespace and its datafile size details:

set linesize 200
col tablespace_name format a25
col file_name format a70
select file_name,file_id,tablespace_name,bytes/1024/1024,AUTOEXTENSIBLE, MAXBYTES/1024 from dba_data_files where tablespace_name='$TBSNAME';

How to Check  Current DB Size:

select
"Reserved_Space(GB)", "Reserved_Space(GB)" - "Free_Space(GB)" "Used_Space(GB)","Free_Space(GB)"
from(
select 
(select sum(bytes/1024/(1024*1024)) from dba_data_files) "Reserved_Space(GB)",
(select sum(bytes/1024/(1024*1024)) from dba_free_space) "Free_Space(GB)"
from dual
);

How to Check DB Growth details:

SET LINESIZE 200
SET PAGESIZE 200
COL "Database Size" FORMAT a13
COL "Used Space" FORMAT a11
COL "Used in %" FORMAT a11
COL "Free in %" FORMAT a11
COL "Database Name" FORMAT a13
COL "Free Space" FORMAT a12
COL "Growth DAY" FORMAT a11
COL "Growth WEEK" FORMAT a12
COL "Growth DAY in %" FORMAT a16
COL "Growth WEEK in %" FORMAT a16
SELECT
(select min(creation_time) from v$datafile) "Create Time",
(select name from v$database) "Database Name",
ROUND((SUM(USED.BYTES) / 1024 / 1024 /1024),2) || ' GB' "Database Size",
ROUND((SUM(USED.BYTES) / 1024 / 1024 /1024 ) - ROUND(FREE.P / 1024 / 1024 /1024 ),2) || ' GB' "Used Space",
ROUND(((SUM(USED.BYTES) / 1024 / 1024 /1024 ) - (FREE.P / 1024 / 1024 /1024 )) / ROUND(SUM(USED.BYTES) / 1024 / 1024 /1024,2)*100,2) || '% GB' "Used in %",
ROUND((FREE.P / 1024 / 1024 /1024 ),2) || ' GB' "Free Space",
ROUND(((SUM(USED.BYTES) / 1024 / 1024 /1024 ) - ((SUM(USED.BYTES) / 1024 / 1024 /1024 ) - ROUND(FREE.P / 1024 / 1024 /1024 )))/ROUND(SUM(USED.BYTES) / 1024 / 1024 /1024,2 )*100,2) || '% MB' "Free in %",
ROUND(((SUM(USED.BYTES) / 1024 / 1024 /1024) - (FREE.P / 1024 / 1024 /1024 ))/(select sysdate-min(creation_time) from v$datafile),2) || ' GB' "Growth DAY",
ROUND(((SUM(USED.BYTES) / 1024 / 1024 /1024) - (FREE.P / 1024 / 1024 /1024))/(select sysdate-min(creation_time) from v$datafile)/ROUND((SUM(USED.BYTES) / 1024 / 1024/1024 ),2)*100,3) || '% MB' "Growth DAY in %",
ROUND(((SUM(USED.BYTES) / 1024 / 1024 /1024) - (FREE.P / 1024 / 1024 /1024))/(select sysdate-min(creation_time) from v$datafile)*7,2) || ' GB' "Growth WEEK",
ROUND((((SUM(USED.BYTES) / 1024 / 1024 /1024) - (FREE.P / 1024 / 1024 /1024 ))/(select sysdate-min(creation_time) from v$datafile)/ROUND((SUM(USED.BYTES) / 1024 / 1024 /1024 ),2)*100)*7,3) || '% GB' "Growth WEEK in %"
FROM    (SELECT BYTES FROM V$DATAFILE
UNION ALL
SELECT BYTES FROM V$TEMPFILE
UNION ALL
SELECT BYTES FROM V$LOG) USED,
(SELECT SUM(BYTES) AS P FROM DBA_FREE_SPACE) FREE
GROUP BY FREE.P;


How to Check  Size of table/ object

  • select tablespace_name ,round(sum(bytes/1024/1024)) MB from dba_extents where segment_name in ('&1') group by tablespace_name;
  • select segment_name,segment_type,bytes/1024/1024 MB  from dba_segments where segment_type='TABLE' and segment_name='&OBJECT_NAME';

How to Check  Size of schema

  • select owner,sum(bytes/1024/1024) SIZE_IN_MB from dba_segments where owner='&1' group by owner;
  • select owner,sum(bytes/1024/1024/1024) SIZE_IN_GB from dba_segments group by owner order by size_in_gb;
  • select tablespace_name,sum(bytes/1024/1024) SIZE_IN_MB from dba_segments where owner='&USERNAME' group by tablespace_name;
  • select owner,sum(bytes/1024/1024) SIZE_IN_MB from dba_segments where owner='&1' group by owner;


These queries will help in your day-to-day activities for space related queries. Please let know if you would like to need more queries.

Questions are welcomed. :) 😀

No comments:

Post a Comment

Changing database name and/or database id using NID

  Changing database name and/or database id using NID   Oracle NID utility NID is utility to change the database name and/or database id. St...