Skip to main content

Tablespace and datafiles space usage query to find free space,used space and its percentage

Hi,

Here i am going to write the queries related to tablespace usage. We can find the free space,used space and its percentage of tablespace and datafiles separately.

If your tablespace is not auto extendable, you can use the below query given in 1 a) and 1 b) to get space usage of tablespace

1 a) Combine query with sm$ts_avail,sm$ts_used,sm$ts_free:
=====================================================
set pages 1000
break on report
compute sum of Total_MB on report
compute sum of Used_MB on report
compute sum of Free_MB on report
select a.TABLESPACE_NAME, round(Curr_Size,1) Total_MB,round(Used,1) Used_MB,round(Free,1) Free_MB, round(100*(1-free/Curr_Size),1) Usage
from (select TABLESPACE_NAME,BYTES/(1024*1024) Curr_Size from sm$ts_avail) a
,(select TABLESPACE_NAME,BYTES/(1024*1024) Used from sm$ts_used) b,
(select TABLESPACE_NAME,BYTES/(1024*1024) Free from sm$ts_free) c
where a.TABLESPACE_NAME=b.TABLESPACE_NAME(+) and
a.TABLESPACE_NAME=c.TABLESPACE_NAME order by 1 ASC;

OUTPUT:















1 b) If you want to know the usage details of particular tablespace, you can add and tablespace_name='&tablespace_name' in the where condition of the above query. For example

set pages 1000
break on report
compute sum of Total_MB on report
compute sum of Used_MB on report
compute sum of Free_MB on report
select a.TABLESPACE_NAME, round(Curr_Size,1) Total_MB,round(Used,1) Used_MB,round(Free,1) Free_MB, round(100*(1-free/Curr_Size),1) Usage
from (select TABLESPACE_NAME,BYTES/(1024*1024) Curr_Size from sm$ts_avail) a
,(select TABLESPACE_NAME,BYTES/(1024*1024) Used from sm$ts_used) b,
(select TABLESPACE_NAME,BYTES/(1024*1024) Free from sm$ts_free) c
where a.TABLESPACE_NAME=b.TABLESPACE_NAME(+) and
a.TABLESPACE_NAME=c.TABLESPACE_NAME and a.TABLESPACE_NAME='TBS_DATA' order by 1 ASC;


If your tablespace is auto extendable, you can use the below query given in 2 a) and 2 b) to get space usage of tablespace

2 a) Below query will give you the free space details with extendable space of the tablespace if the tablespace is auto extendable.

set pages 1000
set lines 500
break on report
compute sum of CURR_SIZE_MB on report
compute sum of Used_MB on report
compute sum of MAXSIZE_MB on report
compute sum of Free+extendable_MB on report
col TABLESPACE_NAME for a30
col file_name for a45
select
   a.TABLESPACE_NAME,
   round(avail,1) curr_size_MB,
   round(used,1) used_MB,
   round(total,1) Maxsize_MB,
   round(free+extentable_MB,1) "Free+extendable_MB",
   round(100*(1-(free+extentable_MB)/total),1)"Usage %"
from (
      select
        TABLESPACE_NAME,
        sum(BYTES)/(1024*1024) avail,
        sum(MAXBYTES)/(1024*1024) total,
        (sum(MAXBYTES)/(1024*1024) - sum(BYTES)/(1024*1024)) extentable_MB
      from dba_data_files
      group by TABLESPACE_NAME) a,
     (
      select
         TABLESPACE_NAME,
         sum(BYTES)/(1024*1024) free
      from dba_free_space group by TABLESPACE_NAME) b,
     (
      select
         TABLESPACE_NAME,
         BYTES/(1024*1024) Used
      from sm$ts_used) c
where a.TABLESPACE_NAME=b.TABLESPACE_NAME and
      a.TABLESPACE_NAME=c.TABLESPACE_NAME
order by 4 DESC;

OUTPUT:














2 b) Here also you can get the same details for a particular tablespace by adding "and  a.TABLESPACE_NAME='&tablespace_name' in the where condition of the above query.

set pages 1000
set lines 500
break on report
compute sum of CURR_SIZE_MB on report
compute sum of Used_MB on report
compute sum of MAXSIZE_MB on report
compute sum of Free+extendable_MB on report
col TABLESPACE_NAME for a30
col file_name for a45
select
   a.TABLESPACE_NAME,
   round(avail,1) curr_size_MB,
   round(used,1) used_MB,
   round(total,1) Maxsize_MB,
   round(free+extentable_MB,1) "Free+extendable_MB",
   round(100*(1-(free+extentable_MB)/total),1)"Usage %"
from (
      select
        TABLESPACE_NAME,
        sum(BYTES)/(1024*1024) avail,
        sum(MAXBYTES)/(1024*1024) total,
        (sum(MAXBYTES)/(1024*1024) - sum(BYTES)/(1024*1024)) extentable_MB
      from dba_data_files
      group by TABLESPACE_NAME) a,
     (
      select
         TABLESPACE_NAME,
         sum(BYTES)/(1024*1024) free
      from dba_free_space group by TABLESPACE_NAME) b,
     (
      select
         TABLESPACE_NAME,
         BYTES/(1024*1024) Used
      from sm$ts_used) c
where a.TABLESPACE_NAME=b.TABLESPACE_NAME and
      a.TABLESPACE_NAME=c.TABLESPACE_NAME and
      a.TABLESPACE_NAME='SE3_DATA'
order by 4 DESC;


3) Below query will give you each datafiles' free space,used space of the tablespace.

set lines 500
col FILE_NAME for a45
compute sum of ALLOCATED_MB on report
compute sum of Used_MB on report
compute sum of FREE_SPACE_MB on report
set lines 500
COLUMN free_space_mb format 999999.90
COLUMN allocated_mb format 999999.90
COLUMN used_mb format 999999.90
col file_name for a60
SELECT   SUBSTR (df.NAME, 1, 60) file_name, df.bytes / 1024 / 1024 allocated_mb,
         ((df.bytes / 1024 / 1024) - NVL (SUM (dfs.bytes) / 1024 / 1024, 0))
               used_mb,
         NVL (SUM (dfs.bytes) / 1024 / 1024, 0) free_space_mb
    FROM v$datafile df, dba_free_space dfs
   WHERE df.file# = dfs.file_id(+) and dfs.TABLESPACE_NAME='TEK_DATA'
GROUP BY dfs.file_id, df.NAME, df.file#, df.bytes
ORDER BY file_name;

OUTPUT:










4 a) You can use the below query to find the current tablespace structure details like current size,max size,auto extendable or not, next increment by

set lines 500
set pages 100
col TABLESPACE_NAME for a30
col FILE_NAME for a60
break on report
compute sum of Size_MB on report
compute sum of Maxsize_MB on report
compute sum of extentable_MB on report
select TABLESPACE_NAME,FILE_NAME,BYTES/(1024*1024) Size_MB,MAXBYTES/(1024*1024)
Maxsize_MB,AUTOEXTENSIBLE,(MAXBYTES - BYTES)/(1024*1024) extentable_MB,INCREMENT_BY*(8192/1024) "Next (in KB)"
from dba_data_files order by 2;


4 b) You can get the details for particular tablespace also using below query

set lines 500
set pages 100
col TABLESPACE_NAME for a30
col FILE_NAME for a60
break on report
compute sum of Size_MB on report
compute sum of Maxsize_MB on report
compute sum of extentable_MB on report
select TABLESPACE_NAME,FILE_NAME,BYTES/(1024*1024) Size_MB,MAXBYTES/(1024*1024)
Maxsize_MB,AUTOEXTENSIBLE,(MAXBYTES - BYTES)/(1024*1024) extentable_MB,INCREMENT_BY*(8192/1024) "Next (in KB)"
from dba_data_files where TABLESPACE_NAME='SE3_DATA' order by 2;


5) query to find growth history for a tablespace:

SELECT TO_CHAR (sp.begin_interval_time,'YYYY-MM-DD') days
, ts.tsname
, max(round((tsu.tablespace_size* dt.block_size )/(1024*1024),2) ) cur_size_MB
, max(round((tsu.tablespace_usedsize* dt.block_size )/(1024*1024),2)) usedsize_MB
FROM DBA_HIST_TBSPC_SPACE_USAGE tsu
, DBA_HIST_TABLESPACE_STAT ts
, DBA_HIST_SNAPSHOT sp
, DBA_TABLESPACES dt
WHERE tsu.tablespace_id= ts.ts#
AND tsu.snap_id = sp.snap_id
AND ts.tsname = dt.tablespace_name
AND ts.tsname IN ('TEK_DATA')
GROUP BY TO_CHAR (sp.begin_interval_time,'YYYY-MM-DD'), ts.tsname
ORDER BY ts.tsname, days desc;

OUTPUT:














6) Query to find average Growth MB/Day of the tablespace in last 30 days:

SELECT b.tsname tablespace_name
, MAX(b.used_size_mb) cur_used_size_mb
, round(AVG(inc_used_size_mb),2)avg_growth_mb_per_day
FROM (
  SELECT a.days, a.tsname, used_size_mb
  , used_size_mb - LAG (used_size_mb,1)  OVER ( PARTITION BY a.tsname ORDER BY a.tsname,a.days) inc_used_size_mb
  FROM (
      SELECT TO_CHAR(sp.begin_interval_time,'MM-DD-YYYY') days
       ,ts.tsname
       ,MAX(round((tsu.tablespace_usedsize* dt.block_size )/(1024*1024),2)) used_size_mb
      FROM DBA_HIST_TBSPC_SPACE_USAGE tsu, DBA_HIST_TABLESPACE_STAT ts
       ,DBA_HIST_SNAPSHOT sp, DBA_TABLESPACES dt
      WHERE tsu.tablespace_id= ts.ts# AND tsu.snap_id = sp.snap_id
       AND ts.tsname = dt.tablespace_name  AND sp.begin_interval_time > sysdate-30
      GROUP BY TO_CHAR(sp.begin_interval_time,'MM-DD-YYYY'), ts.tsname
      ORDER BY ts.tsname, days
  ) A
) b where b.tsname='TEK_DATA' GROUP BY b.tsname ORDER BY b.tsname
/

OUTPUT:








Comments

  1. Very good. . But I have a problem. .I need to shrink avery larg datafile that contains only about 20 % data.. ihope tofind a solution .

    ReplyDelete
    Replies
    1. Dear saleh.. We have a solution to reclaim unused space from datafiles using export/import or Manual Tablespace Reorganization method if your environment allows to do. We had done the manual tablespace reorganization method & export/import method many times in our environment for the tablespace which has more than 300 GB of size.

      For steps you can see below URL for your reference.

      http://oracle-base.com/articles/misc/reclaiming-unused-space.php

      Or else, You find the high water mark of the datafile & re-size accordingly to that value. That's what we can do.If you are not preferring the above method.

      How to Resolve ORA-03297 When Resizing a Datafile by Finding the Table Highwatermark (Doc ID 130866.1)

      Delete
  2. Very nice sharing !!
    Regards, :o)

    ReplyDelete
  3. Hello,
    Please specify if we need or not Oracle tuning license for queries you provided. If yes, for which of them.
    Regards, Cristian

    ReplyDelete
    Replies
    1. The last two queries actually require the Diagnostic Pack to stay compliant with licenses, because of DBA_HIST_TBSPC_SPACE_USAGE and DBA_HIST_TABLESPACE_STAT. See here for more info: http://docs.oracle.com/cd/E11882_01/license.112/e47877/options.htm#CIHIHDDJ

      Delete
    2. Sorry Cristian.. I was not able to answer your query since i was out of internet world for last 5 days.I hope you got the answer from Tom..

      Delete
    3. Thank you dear Tom Canty for answering with your valuable knowledge.. :)

      Delete
  4. Thank you for sharing valuable information. Please keep sharing more information. Thank you again .....

    ReplyDelete
  5. thank you for sharing, but you need to check 2a again. Once you declared it Free+extendable_MB and called it, Free+extentable_MB. How does it work?

    ReplyDelete
    Replies
    1. Sorry Thanh-Dương.. I didn't get you clearly.
      But thanks for your comment.
      Free_MB means (CURR_SIZE_MB minus USED_MB).
      Free+extendable_MB means (MAXSIZE_MB minus USED_MB). That's what i mentioned in my query but in a different calculation.

      Delete

Post a Comment

Popular posts from this blog

Export and import multiple schema using expdp/impdp (Data Pump utility)

Use the below sql query to export and import multiple schema: expdp schemas=schema1,schema2,schema3 directory=DATA_PUMP_DIR dumpfile=schemas120514bkp.dmp exclude=statistics logfile=expdpschemas120514.log impdp schemas=schema1,schema2,schema3 directory=DATA_PUMP_DIR dumpfile=schemas120514bkp.dmp logfile=impdpschemas120514.log sql query to export and import a schema: expdp schemas=schema directory=DATA_PUMP_DIR dumpfile=schema120514bkp.dmp exclude=statistics logfile=expdpschema120514.log impdp schemas=schema directory=DATA_PUMP_DIR dumpfile=schema120514bkp.dmp logfile=expdpschema120514.log Parameter STATISTICS=NONE can either be used in export or import. No need to use the parameter in both. To export meta data only to get ddl of the schemas: expdp schemas=schema1,schema2,schema3 directory=TEST_DIR dumpfile=content.dat content=METADATA_ONLY exclude=statistics To get the DDL in a text file: impdp directory=TEST_DIR sqlfile=sql.dat logfile=sql.log dumpfile=content12051

Pinning execution plan for a sql_id using 'SQL Profile' to consistently reproduce a good plan

Deal all, Below post content is not my own. It is taken from MOSC document but the example shown below is taken from our environment. I got to know the below topic when we had experienced the bad SQL query performance in one of our production environment during daily batch running time. Hence we had implemented the same mentioned in the title "Pinning execution plan for a sql_id using 'SQL Profile' to consistently reproduce a good plan". Introduction The "cost-based optimizer (CBO or just optimizer) is the part of database software that determines the most efficient means of accessing the specified data (or at least what it "calculates" as the most efficient). The optimizer determines which execution plan is most efficient by considering several sources of information, including query conditions, available access paths, statistics gathered for the system, and hints. Some changes in those conditions may affect the calculations of the plan

SQL query to find the cause or reason for more archive log generation

Finding reason or cause for heavy or more archive log generation in a particular time period As i said in the previous post we are going to see SQL queries to find the cause or reason for more archive log generation in a problematic window... Sometimes customer would ask the sql query which generated more archive logs sometimes before or days before (not for current archive logs generation which is described in the previous post). In such scenarios, follow the steps below. Step 1: ====== First you must know the timing when more number of archive logs generated in the databases. for that you can use the below query. Below sql query gives how many number of archive logs generated for each and every hour... col day for a12 set lines 1000 set pages 999 col "00" for a3 col "01" for a3 col "02" for a3 col "03" for a3 col "04" for a3 col "05" for a3 col "06" for a3 col "07" for