Home  >  Article  >  Database  >  Oracle表空间收缩方案

Oracle表空间收缩方案

WBOY
WBOYOriginal
2016-06-07 17:20:211129browse

对于表空间收缩,Oracle只提供扩大的功能,而不提供收缩。所以,要实现这样的要求,就只能先创建一个中间表空间,然后将待收缩表

应用背景:
某些情况下,由于前期设计上没有考虑全面,导致表空间预建太大,远远超出实际使用大小。于是,就出现了收缩表空间这样的需求,即将这个表空间的占用空间进行收缩。
 
处理方案:
对于表空间收缩,Oracle只提供扩大的功能,,而不提供收缩。所以,要实现这样的要求,就只能先创建一个中间表空间,然后将待收缩表空间中的数据迁移到这个表空间下
 
处理方法:
1、找出该表空间下的所有数据对象;
select segment_type, partition_name, segment_name from dba_segments;
 
2、创建目标空间(不强制创建,但是建议)。
create tablespace dbs_temp datafile 'd:\dbs_temp01.dbf' size 100m;
 
3、根据对象类型重建或转移对应数据;
对于table:
alter table xx move tablespace dbs_temp;
对于partition table:
alter table xx move partition xx1 tablespace dbs_temp;
对于index:
alter index ixx rebuild tablespace dbs_temp;
对于lob字段:
alter table xx move lob(col_name) store as (tablespace dbs_temp);

一般情况下,下面语句基本可以涵盖所有的数据对象了:
select segment_type, segment_name, partition_name,
       case segment_type
       when 'TABLE' then 'alter table ' || owner || '.' || segment_name || ' move tablespace dbs_temp;',
       when 'INDEX' then 'alter index ' || owner || '.' || segment_name || ' rebuild tablespace dbs_temp;',
       when 'INDEX PARTITION' then 'alter index ' || owner || '.' || segment_name || ' rebuild tablespace dbs_temp;',
       when 'TABLE PARTITION' then 'alter table ' || owner || '.' || segment_name || ' move partition ' || partition_name || 'tablespace dbs_temp;' sqltext
  from dba_segments
 where tablespace_name = 'FUND_TABLE'
   and segment_type not like 'LOB%';
 
下面是迁移lob字段的
select table_name, column_name, 'alter table ' || owner || '.' || table_name || ' move lob(' || column_name || ') store as(tablespace dbs_temp);' sqltext
  from dba_lobs
 where tablespace_name = 'FUND_TABLE';
 
4、删除清空后的表空间;
drop tablespace dbs_old including contents and datafiles;
下面步骤简单了,就不一一列举。
 
5、如果对表空间名称有要求,则使用原来的表空间名再次创建一个合适大小的表空间。

6、将目标表空间中的数据再迁移回新建的最终的表空间。

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn