匿名过程

​
-------------------only can move table,table partition  and index,indexpartition
set serveroutput on
declare
--param def
v_tbs_rel varchar2(100);
v_tbs_tmp varchar2(100);
v_file_no number(10);

--execstr def
x_move_tp_str varchar2(1000);
x_move_ip_str varchar2(1000);
x_move_ct_str varchar2(1000);
x_move_ci_str varchar2(1000);
x_back_tp_str varchar2(1000);
x_back_ip_str varchar2(1000);
x_back_ct_str varchar2(1000);
x_back_ci_str varchar2(1000);
x_drop_str varchar2(1000);


--cursor def
cursor x_move_tp_cur is 
select distinct 'alter table '||owner||'.'||segment_name||' move partition '||partition_name||' tablespace '||v_tbs_tmp
from dba_extents
where tablespace_name=v_tbs_rel 
and segment_type ='TABLE PARTITION'
and file_id=v_file_no ;


cursor x_back_tp_cur is 
select distinct 'alter table '||owner||'.'||segment_name||' move partition '||partition_name||' tablespace '||v_tbs_rel
from dba_extents
where tablespace_name=v_tbs_rel 
and segment_type ='TABLE PARTITION'
and file_id=v_file_no ;


cursor x_move_ip_cur is 
select distinct 'alter index '||owner||'.'||segment_name||' rebuild partition '||partition_name||' online tablespace '||v_tbs_tmp
from dba_extents
where tablespace_name=v_tbs_rel 
and segment_type ='INDEX PARTITION'
and file_id=v_file_no ;


cursor x_back_ip_cur is 
select distinct 'alter index '||owner||'.'||segment_name||' rebuild partition '||partition_name||' online tablespace '||v_tbs_rel
from dba_extents
where tablespace_name=v_tbs_rel
and segment_type ='INDEX PARTITION'
and file_id=v_file_no ;


cursor x_move_ct_cur is 
select distinct 'alter table '||owner||'.'||segment_name||' move tablespace '||v_tbs_tmp
from dba_extents
where tablespace_name=v_tbs_rel
and segment_type ='TABLE'
and file_id=v_file_no ;

cursor x_back_ct_cur is 
select distinct 'alter table '||owner||'.'||segment_name||' move tablespace '||v_tbs_rel
from dba_extents
where tablespace_name=v_tbs_rel
and segment_type ='TABLE'
and file_id=v_file_no ;

cursor x_move_ci_cur is 
select distinct 'alter index '||owner||'.'||segment_name||' rebuild online tablespace '||v_tbs_tmp
from dba_extents
where tablespace_name=v_tbs_rel
and segment_type ='INDEX'
and file_id=v_file_no ;

cursor x_back_ci_cur is 
select distinct 'alter index '||owner||'.'||segment_name||' rebuild online tablespace '||v_tbs_rel
from dba_extents
where tablespace_name=v_tbs_rel
and segment_type ='INDEX'
and file_id=v_file_no ;


------------------------------------


begin
--param
v_tbs_rel:=upper('&release_tbs');
v_tbs_tmp:=upper('&temp_tbs');

select file_id into v_file_no
from (
select file_id,sum(bytes) from 
dba_extents 
where tablespace_name=v_tbs_rel
group by file_id
order by 2 
) where rownum<2;

x_drop_str:='alter tablespace '||v_tbs_rel||' drop datafile '||v_file_no;


--open cursor
open x_move_tp_cur;
open x_move_ip_cur;
open x_move_ct_cur;
open x_move_ci_cur;
open x_back_tp_cur;
open x_back_ip_cur;
open x_back_ct_cur;
open x_back_ci_cur;



----------move action
---move tab part
dbms_output.put_line('START MOVE TABLE PARTITION');

	loop
      fetch x_move_tp_cur into x_move_tp_str;
      exit when (x_move_tp_cur%notfound);
      dbms_output.put_line(x_move_tp_str);
	  execute immediate x_move_tp_str;
    end loop;

dbms_output.put_line('END MOVE TABLE PARTITION');
dbms_output.put_line('----------------------------------');
---move ind part
dbms_output.put_line('START MOVE INDEX PARTITION');
	loop
      fetch x_move_ip_cur into x_move_ip_str;
      exit when (x_move_ip_cur%notfound);
      dbms_output.put_line(x_move_ip_str);
	  execute immediate x_move_ip_str;
    end loop;
dbms_output.put_line('END MOVE INDEX PARTITION');
dbms_output.put_line('----------------------------------');
---move tab
dbms_output.put_line('START MOVE COMMON TABLE');
	loop
      fetch x_move_ct_cur into x_move_ct_str;
      exit when (x_move_ct_cur%notfound);
      dbms_output.put_line(x_move_ct_str);
	  execute immediate x_move_ct_str;
    end loop;
dbms_output.put_line('END MOVE COMMON TABLE');
dbms_output.put_line('----------------------------------');	
---move ind
dbms_output.put_line('START MOVE COMMON INDEX');
	loop
      fetch x_move_ci_cur into x_move_ci_str;
      exit when (x_move_ci_cur%notfound);
      dbms_output.put_line(x_move_ci_str);
	  execute immediate x_move_ci_str;
    end loop;
dbms_output.put_line('END MOVE COMMON INDEX');
dbms_output.put_line('----------------------------------');	


----------drop action
dbms_output.put_line('START DROP DATAFILE');
dbms_output.put_line(x_drop_str);
execute immediate x_drop_str;
dbms_output.put_line('END DROP DATAFILE');
dbms_output.put_line('----------------------------------');	

----------back action
dbms_output.put_line('START BACK TABLE PARTITION');
	loop
      fetch x_back_tp_cur into x_back_tp_str;
      exit when (x_back_tp_cur%notfound);
      dbms_output.put_line(x_back_tp_str);
	  execute immediate x_back_tp_str;
    end loop;
dbms_output.put_line('END BACK TABLE PARTITION');
dbms_output.put_line('----------------------------------');
dbms_output.put_line('START BACK INDEX PARTITION');

	loop
      fetch x_back_ip_cur into x_move_tp_str;
      exit when (x_back_ip_cur%notfound);
      dbms_output.put_line(x_move_tp_str);
	  execute immediate x_move_tp_str;
    end loop;
dbms_output.put_line('END BACK INDEX PARTITION');
dbms_output.put_line('----------------------------------');
dbms_output.put_line('START BACK COMMON TABLE');
	loop
      fetch x_back_ct_cur into x_back_ct_str;
      exit when (x_back_ct_cur%notfound);
      dbms_output.put_line(x_back_ct_str);
	  execute immediate x_back_ct_str;
    end loop;
dbms_output.put_line('END BACK COMMON TABLE');
dbms_output.put_line('----------------------------------');
dbms_output.put_line('START BACK COMMON INDEX');	
	loop
      fetch x_back_ci_cur into x_back_ci_str;
      exit when (x_back_ci_cur%notfound);
      dbms_output.put_line(x_back_ci_str);
	  execute immediate x_back_ci_str;
    end loop;
dbms_output.put_line('END BACK COMMON INDEX');
dbms_output.put_line('----------------------------------');

--close cursor
close x_move_tp_cur;
close x_move_ip_cur;
close x_move_ct_cur;
close x_move_ci_cur;
close x_back_tp_cur;
close x_back_ip_cur;
close x_back_ct_cur;
close x_back_ci_cur;

end;
/

​

过程逻辑

        使用dba_extent根据数据绝对量排序,选择一个使用最少的数据文件,配合使用一个临时的表空间,将数据库的表,索引,表分区,索引分区,挪过去,再删除dbf,再将对象挪回表空间。

        每次执行删除一个dbf

        没有检查空间是否足够的逻辑

        没有判断是否为第一个dbf的逻辑

        没有考虑lob的场景

使用场景

       特大型表空间,分配了过量的数据文件,且数据文件都给了一个不小的初始值,需要回收数据文件时。

12-16 08:17