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

使用场景

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

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

相关推荐
Java探秘者3 小时前
Maven下载、安装与环境配置详解:从零开始搭建高效Java开发环境
java·开发语言·数据库·spring boot·spring cloud·maven·idea
2301_786964363 小时前
3、练习常用的HBase Shell命令+HBase 常用的Java API 及应用实例
java·大数据·数据库·分布式·hbase
阿维的博客日记4 小时前
图文并茂解释水平分表,垂直分表,水平分库,垂直分库
数据库·分库分表
wrx繁星点点5 小时前
事务的四大特性(ACID)
java·开发语言·数据库
小小娥子6 小时前
Redis的基础认识与在ubuntu上的安装教程
java·数据库·redis·缓存
DieSnowK6 小时前
[Redis][集群][下]详细讲解
数据库·redis·分布式·缓存·集群·高可用·新手向
-XWB-6 小时前
【MySQL】数据目录迁移
数据库·mysql
老华带你飞6 小时前
公寓管理系统|SprinBoot+vue夕阳红公寓管理系统(源码+数据库+文档)
java·前端·javascript·数据库·vue.js·spring boot·课程设计
我明天再来学Web渗透7 小时前
【hot100-java】【二叉树的层序遍历】
java·开发语言·数据库·sql·算法·排序算法
Data 3177 小时前
Hive数仓操作(十一)
大数据·数据库·数据仓库·hive·hadoop