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

使用场景

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

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

相关推荐
李元豪2 小时前
grpo nl2sql qwen3 模型强化学习训练有效果的成立条件有哪些
数据库·oracle
Hello.Reader5 小时前
RedisJSON 路径语法深度解析与实战
数据库·redis·缓存
TDengine (老段)5 小时前
TDengine 使用最佳实践(2)
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
设计师小聂!8 小时前
Linux系统中部署Redis详解
linux·运维·数据库·redis
kfepiza8 小时前
Debian-10编译安装Mysql-5.7.44 笔记250706
linux·数据库·笔记·mysql·debian·bash
Touper.8 小时前
Redis 基础详细介绍(Redis简单介绍,命令行客户端,Redis 命令,Java客户端)
java·数据库·redis
不剪发的Tony老师8 小时前
phpMyAdmin:一款经典的MySQL在线管理工具又回来了
数据库·mysql·phpmyadmin
极限实验室8 小时前
TDBC 2025 可信数据库发展大会,极限科技邀您来赴约!
数据库
lixia0417mul210 小时前
使用Starrocks替换Clickhouse的理由
数据库
张璐月11 小时前
mysql的性能优化:组提交、数据页复用、全表扫描优化、刷脏页
数据库·mysql·性能优化