Monday 29 February 2016

Useful graphical SQL query to monitor the space on your database

Hi guys,

I have thinked that is a good idea to share with you this graphical SQL query useful to control and monitor the space on your database.

I have executed this query on SQL Developer in my database and this is the situation:

SELECT "TABLESPACE_NAME", "PERCENT_USED", "PCT_USED", "ALLOCATED", "USED", "FREE", "DATAFILES" FROM(SELECT a.tablespace_name,'SQLDEV:GAUGE:0:100:0:0:'||nvl(ROUND(((c.bytes-nvl(b.bytes,0))/c.bytes)*100,2),0) percent_used,ROUND(((c.bytes-nvl(b.bytes,0))/c.bytes)*100,2) PCT_USED,c.bytes/1024/1024 allocated,round(c.bytes/1024/1024-nvl(b.bytes,0)/1024/1024,2) used,round(nvl(b.bytes,0)/1024/1024,2) free,c.datafilesFROM dba_tablespaces a,( SELECT tablespace_name, SUM(bytes) bytes FROM dba_free_space GROUP BY tablespace_name ) b,( select count(1) datafiles, SUM(bytes) bytes, tablespace_name from dba_data_files GROUP BY tablespace_name ) cWHERE b.tablespace_name (+) = a.tablespace_nameAND c.tablespace_name (+) = a.tablespace_nameORDER BY nvl(((c.bytes-nvl(b.bytes,0))/c.bytes),0) DESC);

This is the result




As you can see, there are the red line indicating the name with full tablespace (or partially full) and the green line in which there is still space for use. All the numbers in the columns of "Assigned", "Free" and the column "Used" are specified in MB.

Enjoy