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

使用场景

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

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

相关推荐
恰薯条的屑海鸥15 分钟前
零基础在实践中学习网络安全-皮卡丘靶场(第十六期-SSRF模块)
数据库·学习·安全·web安全·渗透测试·网络安全学习
咖啡啡不加糖18 分钟前
Redis大key产生、排查与优化实践
java·数据库·redis·后端·缓存
曼汐 .25 分钟前
数据库管理与高可用-MySQL高可用
数据库·mysql
2301_7931024940 分钟前
Linux——MySql数据库
linux·数据库
喵叔哟43 分钟前
第4章:Cypher查询语言基础
数据库
刘 大 望1 小时前
数据库-联合查询(内连接外连接),子查询,合并查询
java·数据库·sql·mysql
从零开始学习人工智能1 小时前
Doris 数据库深度解析:架构、原理与实战应用
数据库·架构
LiRuiJie2 小时前
深入剖析MySQL锁机制,多事务并发场景锁竞争
数据库·mysql
2501_915374352 小时前
Faiss向量数据库全面解析:从原理到实战
数据库·faiss
睡觉待开机2 小时前
0. MySQL在Centos 7环境安装
数据库·mysql·centos