Fragmentation & Re-organisation




Total  Savings:

set verify off
column file_name format a50 word_wrapped
column smallest format 999,990 heading "Smallest|Size|Poss."
column currsize format 999,990 heading "Current|Size"
column savings format 999,990 heading "Poss.|Savings"
break on report
compute sum of savings on report

column value new_val blksize
select value from v$parameter where name = 'db_block_size'
/

select file_name,
ceil( (nvl(hwm,1)*&&blksize)/1024/1024 ) smallest,
ceil( blocks*&&blksize/1024/1024) currsize,
ceil( blocks*&&blksize/1024/1024) -
ceil( (nvl(hwm,1)*&&blksize)/1024/1024 ) savings
from dba_data_files a,
( select file_id, max(block_id+blocks-1) hwm
from dba_extents
group by file_id ) b
where a.file_id = b.file_id(+) order by savings desc
/



HWM query for all datafiles:

col data_file_name for a50
col tablespace_name for a20
set lines 300

select tablespace_name,
file_id,
file_name DATA_FILE_NAME,
Allocated_MBYTES,
High_Water_Mark_MBYTES,
FREE_MBYTES,
trunc((FREE_MBYTES/Allocated_MBYTES)*100,2) "% Free",
trunc(Allocated_MBYTES-High_Water_Mark_MBYTES,2) Resizable
from
(
select ddf.tablespace_name tablespace_name,
ddf.file_id file_id,
ddf.file_name file_name,
ddf.bytes/1024/1024 Allocated_MBYTES,
trunc((ex.hwm*(dt.block_size))/1024/1024,2) High_Water_Mark_MBYTES,
FREE_MBYTES
from
dba_data_files ddf,
dba_tablespaces dt,
(
select file_id, sum(bytes/1024/1024) FREE_MBYTES
from dba_free_space
group by file_id
) free,
(
select file_id, max(block_id+blocks) hwm
from dba_extents
group by file_id
) ex
where ddf.file_id = ex.file_id
and ddf.tablespace_name = dt.tablespace_name
and ddf.file_id = free.file_id (+)
order by ddf.tablespace_name, ddf.file_id
);

Extents near to HWM:

select file_name data_file_name,
segment_type,
owner||'.'||segment_name segment_name,
partition_name,
block_id,
blockId_Mbytes
from
(
select
de.owner owner,
de.segment_name segment_name,
de.segment_type segment_type,
de.block_id block_id,
DE.PARTITION_NAME partition_name,
ddf.file_name file_name,
trunc((de.block_id*(dt.block_size))/1024/1024,2) blockId_Mbytes
from dba_extents de, dba_data_files ddf, dba_tablespaces dt
where ddf.file_id = &file_id 
and ddf.file_id = de.file_id
and ddf.tablespace_name = dt.tablespace_name
order by de.block_id desc
)
where rownum <= 100;

Database Size:
                   
col "Database Size" format a20
col "Free space" format a20
col "Used space" format a20
select round(sum(used.bytes) / 1024 / 1024 / 1024 ) || ' GB' "Database Size"
, round(sum(used.bytes) / 1024 / 1024 / 1024 ) -
round(free.p / 1024 / 1024 / 1024) || ' GB' "Used space"
, round(free.p / 1024 / 1024 / 1024) || ' GB' "Free space"
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
/               
                   
Top 20 Segments Used:
                   
set lines 300
set pages 1000
col owner for a30
col segment_name for a30
col tablespace_name for a30

select * from ( select segment_type, segment_name, owner, tablespace_name, bytes/1024/1024 as Size_MB from dba_segments order by 4 desc) where rownum <=20;

Top 20 LOB Segments Used:

set lines 300
set pages 1000
col owner for a30
col table_name for a30
col column_name for a30
col segment_name for a30
col tablespace_name for a30
col size_mb for 999999999.00

select * from ( select l.owner,l.table_name,l.column_name,1.segment_name,1.tablespace_name,round(s.bytes/1024/1024,2) as size_mb from dba_lobs l join dba_segments s on s.owner=l.owner and s.segment_name=l.segment_name order by 6 desc ) where rownum <=20;

 Tablespace Size:

select t.tablespace,  t.totalspace as " Totalspace(MB)",
     round((t.totalspace-nvl(fs.freespace,0)),2) as "Used Space(MB)",
     nvl(fs.freespace,0) as "Freespace(MB)",
     round(((t.totalspace-nvl(fs.freespace,0))/t.totalspace)*100,2) as "%Used",
     round((nvl(fs.freespace,0)/t.totalspace)*100,2) as "% Free"
     from
     (select round(sum(d.bytes)/(1024*1024)) as totalspace,d.tablespace_name tablespace
     from dba_data_files d
     group by d.tablespace_name) t,
     (select round(sum(f.bytes)/(1024*1024)) as freespace,f.tablespace_name tablespace
     from dba_free_space f
     group by f.tablespace_name) fs
     where t.tablespace=fs.tablespace (+)
     order by "% Free";         

Get DDL for tablespace

set long 999999999
select dbms_metadata.get_ddl('TABLESPACE','<old_tablespace_name>') from dual;

col file_name for a60
col tablespace_name for a30
select file_id,tablespace_name,file_name,bytes/1024/1024/1024 as Size_GB, autoextensible, status from dba_data_files where tablespace_name='<TABLESPACE_NAME>';

select distinct segment_name,owner,segment_type,bytes/1024/1024 as Size_MB,tablespace_name from dba_segments where tablespace_name='<TABLESPACE_NAME>' order by segment_name;

select owner,segment_name,segment_type,partition_name,tablespace_name from dba_segments where tablespace_name='<TABLESPACE_NAME>' ;

select owner,table_name,column_name,segment_name,index_name,tablespace_name from dba_lobs where segment_name in ('<SEGMENT_NAME>');

col object_name for a40
select owner,object_name,object_type,created,last_ddl_time,status from dba_objects where status <>'VALID';

col owner for a20
col referenced_name for a30
col referenced_link_name for a10
col dependency_type for a10
select * from dba_dependencies where owner='<OWNER_NAME>';
select owner,name,type,referenced_owner,referenced_name,referenced_type,referenced_link_name,dependency_type from dba_dependencies where owner='<OWNER_NAME>';

select * from dba_tab_privs where owner='<OWNER_NAME>';
select * from dba_sys_privs where grantee='<OWNER_NAME>';
select * from dba_role_privs where grantee='<OWNER_NAME>';


Table:

select 'alter table '||owner||'.'||table_name||' move tablespace '||'<new_tablespace_name> ;' from dba_tables where table_name in ( select distinct segment_name from dba_segments where tablespace_name='<old_tablespace_name>' and partitioned='NO';
  
  Index:
  
  select 'alter index '||owner||'.'||index_name||' rebuild online parallel '||degree||' ;' from dba_indexes where table_name in ( select distinct segment_name from dba_segments where tablespace_name='<old_tablespace_name>') and partitioned='NO';
  
  Table Partition:
  
  set pages 3000
  set lines 300
  select 'alter table '||table_owner||'.'||table_name||' move partition '||partition_name||' tablespace <new_tablespace_name> ;' from dba_tab_partitions where table_name in ( select distinct segment_name from dba_segments where tablespace_name='<old_tablespace_name>' and segment_type='TABLE PARTITION');
  
  Index partition:
  
  select 'alter index '||index_owner||'.'||index_name||' rebuild partition '||partition_name||' ;' from dba_ind_partitions where index_name in ( select index_name from dba_indexes where table_name in ( select distinct segment_name from dba_segments where tablespace_name='<old_tablespace_name>'));
  
  Run Below last for partitioned parent table:
  
  select 'alter table '||owner||'.'||table_name||' move tablespace '||'<new_tablespace_name> parallel '||degree||';' from dba_tables where table_name in ( select distinct segment_name from dba_segments where tablespace_name='<old_tablespace_name>') and partitioned='YES';

Comments

Popular Posts

AWR Report Analysis

Goldengate 19c Installation with issue resolution

Oracle 12c 2-node RAC setup in VMware Player 15 -- Part01

Oracle to Postgres

Execution_Explain Plan

Active Session History Analysis

General Queries

Postgresql installation offline using NGINX

OEM patches & vulnerabilities