-------- Tablespace

column "Tablespace" format a30
column "Used MB"    format 99,999,999
column "Free MB"    format 99,999,999
column "Total MB"   format 99,999,999

select
   fs.tablespace_name                          "Tablespace",
   (df.totalspace - fs.freespace)              "Used MB",
   fs.freespace                                "Free MB",
   df.totalspace                               "Total MB",
   round(100 * (fs.freespace / df.totalspace)) "Pct. Free"
from
   (select
      tablespace_name,
      round(sum(bytes) / 1048576) TotalSpace
   from
      dba_data_files
   group by
      tablespace_name
   ) df,
   (select
      tablespace_name,
      round(sum(bytes) / 1048576) FreeSpace
   from
      dba_free_space
   group by
      tablespace_name
   ) fs
where
   df.tablespace_name = fs.tablespace_name
   and df.tablespace_name = 'IND';

SELECT  a.tablespace_name,
    ROUND (((c.BYTES - NVL (b.BYTES, 0)) / c.BYTES) * 100,2) percentage_used,
    c.BYTES / 1024 / 1024 space_allocated,
    ROUND (c.BYTES / 1024 / 1024 - NVL (b.BYTES, 0) / 1024 / 1024,2) space_used,
    ROUND (NVL (b.BYTES, 0) / 1024 / 1024, 2) space_free, 
    c.DATAFILES
  FROM 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
    ) c
  WHERE b.tablespace_name(+) = a.tablespace_name 
    AND c.tablespace_name(+) = a.tablespace_name
ORDER BY NVL (((c.BYTES - NVL (b.BYTES, 0)) / c.BYTES), 0) DESC;


SELECT
  ts.tablespace_name,
  TO_CHAR(SUM(NVL(fs.bytes,0))/1024/1024, '99,999,990.99') AS MB_FREE
FROM
  user_free_space fs,
  user_tablespaces ts,
  user_users us
WHERE
  fs.tablespace_name(+)   = ts.tablespace_name
AND ts.tablespace_name(+) = us.default_tablespace
GROUP BY
  ts.tablespace_name;


column TABLESPACE_NAME format a30
column FILE_NAME format a40
column MAXBYTES format 999999999999


select TABLESPACE_NAME, FILE_NAME,AUTOEXTENSIBLE,MAXBYTES from dba_Data_files where TABLESPACE_NAME like 'PERFSTAT';


select TABLESPACE_NAME, MAX_SIZE
from dba_tablespaces
where tablespace_name = 'PERFSTAT'
order by tablespace_name;

select TABLESPACE_NAME, MAX_SIZE, NEXT_EXTENT from dba_tablespaces

TABLESPACE_NAME                  MAX_SIZE NEXT_EXTENT
------------------------------ ---------- -----------
TS_VDS_ALL_TRANS_202103_NU     2147483645

SET LONG 20000 LONGCHUNKSIZE 20000 PAGESIZE 9999 LINESIZE 1000 FEEDBACK OFF VERIFY OFF TRIMSPOOL ON
column TABLESPACE_NAME format a30
column FILE_NAME format a70
column MAXBYTES format 999999999999


select TABLESPACE_NAME, FILE_NAME, MAXBYTES
from   DBA_DATA_FILES
where tablespace_name = 'PERFSTAT'
group  by TABLESPACE_NAME, FILE_NAME, MAXBYTES;


ALTER DATABASE DATAFILE '+UATDATA/repo/datafile/omsfg_tbs.533' AUTOEXTEND ON MAXSIZE 30GB;


select TABLESPACE_NAME, sum(decode(AUTOEXTENSIBLE, 'YES', MAXBYTES, BYTES)) MAX_SIZE
from   DBA_DATA_FILES
where tablespace_name = 'TS_VDS_ALL_TRANS_202103_NU'
group  by TABLESPACE_NAME;

select tablespace_name, round(sum(bytes)/1024/1024, 2) 
as free_space 
from dba_free_space 
where tablespace_name = 'TS_VDS_ALL_TRANS_202103_NU'
group by tablespace_name;

select df.tablespace_name "Tablespace",
       totalusedspace "Used MB",
       (df.totalspace - tu.totalusedspace) "Free MB",
       df.totalspace "Total MB",
       round(100 * ( (df.totalspace - tu.totalusedspace)/ df.totalspace)) "Pct. Free"
  from (select tablespace_name,
               round(sum(bytes) / 1048576) TotalSpace
          from dba_data_files 
         group by tablespace_name) df,
       (select round(sum(bytes)/(1024*1024)) totalusedspace,
               tablespace_name
          from dba_segments 
         group by tablespace_name) tu
 where df.tablespace_name = tu.tablespace_name 
and tu.tablespace_name = 'TS_VLD_BASEII_202106_NU'


select tablespace_name, max_size 
from dba_tablespaces
/

select tablespace_name
       , count(*) as no_of_data_files
       , sum(maxblocks) as max_size
from dba_data_files
group by tablespace_name
/


select df.tablespace_name "Tablespace",
       totalusedspace "Used MB",
       (df.totalspace - tu.totalusedspace) "Free MB",
       df.totalspace "Total MB",
       round(100 * ( (df.totalspace - tu.totalusedspace)/ df.totalspace)) "Pct. Free"
  from (select tablespace_name,
               round(sum(bytes) / 1048576) TotalSpace
          from dba_data_files 
         group by tablespace_name) df,
       (select round(sum(bytes)/(1024*1024)) totalusedspace,
               tablespace_name
          from dba_segments 
         group by tablespace_name) tu
 where df.tablespace_name = tu.tablespace_name 
   and df.totalspace <> 0;

-- dla partycji

select owner,
       segment_name,
       partition_name,
       segment_type,
       bytes / 1024/1024 "MB" 
  from dba_segments 
 where owner = <owner_name>;

-- sam status

SELECT TABLESPACE_NAME, STATUS, CONTENTS
FROM USER_TABLESPACES;

-- dodawanie plików do przestrzeni


   ALTER TABLESPACE PERFSTAT
    ADD DATAFILE '/db/AIGGBTER/datafiles/tbs_perfstat_02.dbf' SIZE 1G
      AUTOEXTEND ON
      NEXT 1G
      MAXSIZE 30G;


-- kroki do dodania plików do przestrzeni


select instance_name,name,open_mode,status,startup_time,logins from v$database,v$instance;
select instance_name,name,open_mode,status,startup_time,logins from gv$instance, gv$database;

select file_name from dba_data_files;
select file_name from dba_data_files where tablespace_name IN ('EVENTS88');

col TABLESPACE_NAME for a15
col FILE_NAME for a45
select tablespace_name,file_name,FILE_ID, bytes/1048576 meg,  status,autoextensible, maxbytes/1048576 maxmeg, increment_by FROM dba_data_files where tablespace_name= 'DEVRO_DATA';

select distinct status from v$backup;

-- sprawdzenie zajętości przestrzeni


set pages 300 lines 200
col tablespace_name for a20
col FILE_NAME for a60
select df.tablespace_name "Tablespace",
(df.totalspace - fs.freespace) "Used MB",
fs.freespace "Free MB",df.totalspace "Total MB",
round(100 * (fs.freespace / df.totalspace)) "Pct. Free"
from (select tablespace_name,
round(sum(bytes) / 1048576) TotalSpace
from dba_data_files group by tablespace_name) df,
(select tablespace_name, round(sum(bytes) / 1048576) FreeSpace
from dba_free_space group by tablespace_name) fs
where df.tablespace_name = fs.tablespace_name(+)
order by 5;

set pages 300 lines 200
col tablespace_name for a20
col FILE_NAME for a60
select df.tablespace_name "Tablespace",
(df.totalspace - fs.freespace) "Used MB",
fs.freespace "Free MB",df.totalspace "Total MB",
round(100 * (fs.freespace / df.totalspace)) "Pct. Free"
from (select tablespace_name,
round(sum(bytes) / 1048576) TotalSpace
from dba_data_files group by tablespace_name) df,
(select tablespace_name, round(sum(bytes) / 1048576) FreeSpace
from dba_free_space group by tablespace_name) fs
where df.tablespace_name = fs.tablespace_name(+)
and df.tablespace_name in ('NE_08_15')
order by 5;
