ORACLE释放表空间中的空闲数据文件

匿名过程

​
-------------------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的场景

使用场景

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

碎碎念:有用,但是又没有那么有用的。本来是为特定客户开发的鸡肋工具

相关推荐
Rverdoser36 分钟前
【SQL】多表查询案例
数据库·sql
Galeoto39 分钟前
how to export a table in sqlite, and import into another
数据库·sqlite
人间打气筒(Ada)1 小时前
MySQL主从架构
服务器·数据库·mysql
leegong231111 小时前
学习PostgreSQL专家认证
数据库·学习·postgresql
喝醉酒的小白1 小时前
PostgreSQL:更新字段慢
数据库·postgresql
敲敲敲-敲代码1 小时前
【SQL实验】触发器
数据库·笔记·sql
和道一文字yyds1 小时前
MySQL 中的索引数量是否越多越好?为什么?如何使用 MySQL 的 EXPLAIN 语句进行查询分析?MySQL 中如何进行 SQL 调优?
数据库·sql·mysql
落笔画忧愁e2 小时前
FastGPT快速将消息发送至飞书
服务器·数据库·飞书
Σίσυφος19002 小时前
halcon 条形码、二维码识别、opencv识别
前端·数据库
小刘|3 小时前
深入理解 SQL 注入漏洞及解决方案
数据库·sql