Query to check the Tablespace details and available free space
The below query give output as group by Tablespace name:
select
a.tablespace_name,
round(SUM(a.bytes)/(1024*1024*1024)) CURRENT_GB,
round(SUM(decode(b.maxextend, null, A.BYTES/(1024*1024*1024),
b.maxextend*8192/(1024*1024*1024)))) MAX_GB,
(SUM(a.bytes)/(1024*1024*1024) - round(c.Free/1024/1024/1024)) USED_GB,
round((SUM(decode(b.maxextend, null, A.BYTES/(1024*1024*1024),
b.maxextend*8192/(1024*1024*1024))) - (SUM(a.bytes)/(1024*1024*1024) -
round(c.Free/1024/1024/1024))),2) FREE_GB,
round(100*(SUM(a.bytes)/(1024*1024*1024) -
round(c.Free/1024/1024/1024))/(SUM(decode(b.maxextend, null, A.BYTES/(1024*1024*1024),
b.maxextend*8192/(1024*1024*1024))))) USED_PCT
from
dba_data_files a,
sys.filext$ b,
(SELECT
d.tablespace_name ,sum(nvl(c.bytes,0)) Free
FROM
dba_tablespaces d,
DBA_FREE_SPACE c
WHERE
d.tablespace_name = c.tablespace_name(+)
group by d.tablespace_name) c
WHERE
a.file_id = b.file#(+)
and a.tablespace_name = c.tablespace_name
GROUP BY a.tablespace_name, c.Free/1024
ORDER BY used_pct desc;
This is a complex SQL query that retrieves information about the tablespaces in an Oracle database. A tablespace is a logical unit of storage that consists of one or more data files. The query calculates the current, maximum, used, and free space in gigabytes (GB) and the percentage of used space for each tablespace. It also sorts the results by the used percentage in descending order. Here is a brief explanation of each part of the query:
- The
select
clause specifies the columns to be displayed in the result table. Each column is derived from an expression that involves theround
,sum
, anddecode
functions. Theround
function rounds a number to a specified number of decimal places. Thesum
function calculates the total of a set of values. Thedecode
function compares an expression to a list of values and returns a corresponding result. For example,decode(b.maxextend, null, A.BYTES/(1024*1024*1024), b.maxextend*8192/(1024*1024*1024))
means ifb.maxextend
is null, then returnA.BYTES/(1024*1024*1024)
, otherwise returnb.maxextend*8192/(1024*1024*1024)
. - The
from
clause specifies the tables to be queried. The query joins three tables:dba_data_files
,sys.filext$
, and a subquery. Thedba_data_files
table contains information about the data files that belong to each tablespace. Thesys.filext$
table contains information about the extent allocation of each data file. An extent is a contiguous set of data blocks allocated to a segment within a tablespace. The subquery calculates the free space in each tablespace by summing the bytes in thedba_free_space
table, which contains information about the free extents in each tablespace. Thedba_tablespaces
table contains information about the tablespaces in the database. - The
where
clause specifies the conditions for filtering the rows from the tables. The query uses the=
operator to join the tables on the matching columns. For example,a.file_id = b.file#(+)
means join thedba_data_files
table (aliased asa
) with thesys.filext$
table (aliased asb
) on thefile_id
andfile#
columns. The(+)
indicates an outer join, which means include the rows froma
even if there is no matching row inb
. - The
group by
clause specifies the columns to be used for grouping the rows. The query groups the rows by thetablespace_name
andc.Free/1024
columns. Thec.Free/1024
column is derived from the subquery and represents the free space in megabytes (MB) for each tablespace. - The
order by
clause specifies the column to be used for sorting the result table. The query sorts the table by theused_pct
column in descending order. Theused_pct
column is derived from an expression that calculates the percentage of used space for each tablespace.
The below query give output as separate file wise detail:
To check the tablespace through SQL Developer
based on the requirement add tablespace to it, through sql developer or ALTER query in sql prompt
Example:
We can view the existing file configuration as well through sql developer, to view what parameter were added in existing file, in a particular tablespace and accordingly create the alter tablespace query.
To view the existing tablespace file
- Click on the file name on the datafile after going through
- SQL Developer -> View -> DBA -> Connection->Select database -> Storage-> expand and double click datafile header
- There would be two tab General and SQL
- General will give overall info of the file and its size and we can create it accordingly or
- we can make use of the SQL tab where the query pre exist and copy and make the changes as per the need like change the filename etc.
- Run the Query in sql prompt to create new tablespace file in that particular tablespace
alter database datafile '/u01/efsp01data/APPS_TS_TOOLS01.dbf' autoextend on maxsize unlimited;
or
ALTER TABLESPACE DW_SYS_DATA ADD DATAFILE '/oracledata/DMPG/dw_sys_data_003.dbf' SIZE 33523200K AUTOEXTEND ON NEXT 8K MAXSIZE 33554416K;