Oracle入门教程:在表空间之间移动数据对象


在表空间之间移动数据对象  

一、基础脚本

在Oracle数据库的表空间之间移动对象会涉及多种类型的数据对象
移动表:

  1. alter table t_a move tablespace tbs_a;  

移动索引:

  1. alter index i_a rebuild tablespace tbs_a;  

移动LOBSEGMENT:

  1. alter table t_a move lob(colume_a) store as (tablespace tbs_a);  

根据实际检验LOBINDEX会随着相应的LOBSEGMENT自动移动
移动表的同时移动LOBSEGMENT:

  1. alter table t_a move tablespace tbs_a lob(colume_a) store as (tablespace tbs_a);  

如果表中有字段为LONG类型的,那么该表不能按照上面的方法移动,只能使用EXP/IMP来移动了

二、批量生成移动脚本

如果有大量的对象需要移动,一行一行写脚本就很不现实,可以使用下面的脚本来生成移动的脚本

--生成LOBSEGMENT的移动语句,目的是把用户USR_A的LOBSEGMENT对象从表空间TBS_OLD移动到TBS_NEW,所生成的语句执行后,相应的LOBINDEX对象也会跟着移动到TBS_NEW

  1. select 'alter table ' || owner || '.' || table_name || ' move LOB(' || column_name || ') store as (tablespace TBS_NEW);' from dba_lobs where owner = 'USR_A' and tablespace_name='TBS_OLD';  

--如要把LOB段所在的表也随同LOB段移动到新的表空间,可使用以下语句来生成脚本

  1. select 'alter table ' || owner || '.' || table_name || ' move tablespace TBS_NEW LOB(' || column_name || ') store as (tablespace TBS_NEW);' from dba_lobs where owner = 'USR_A' and tablespace_name='TBS_OLD';  

--生成移动USR_A的表和索引的脚本,排序是为了移动表的脚本在前,移动索引的脚本在后,按序执行即可;否则先移索引后移表会导致索引失效

  1. select 'alter ' || segment_type || ' USR_A.' || segment_name || ' ' || decode(segment_type,'TABLE','move','INDEX','rebuild') || ' ' || 'tablespace TBS_NEW;' from dba_segments where owner = 'USR_A' and tablespace_name='TBS_OLD' and segment_type in ('TABLE','INDEX'order by segment_type desc;  

--可以用以下语句检查dba_indexes数据字典表中的失效的索引信息

  1. select * from dba_indexes where status = 'UNUSABLE';  

三、一些可能会用到的查询语句

另外还有一些可能会用到的查询表空间中数据对象信息的语句

--统计用户USR_A的数据对象数

  1. select count(*) from dba_segments where owner='USR_A';  

--查看用户USR_A的数据对象所分布的表空间名

  1. select distinct tablespace_name from dba_segments where owner='USR_A';  

--查看用户USR_A的LOB段和LOB索引对象

  1. select * from dba_segments where owner='USR_A' and segment_type in ('LOBSEGMENT','LOBINDEX');  

--分表空间、段类型统计用户USR_A的数据对象数

  1. select tablespace_name,segment_type,count(*) from dba_segments where owner='USR_A';  
  2. group by tablespace_name,segment_type;  
  3. order by tablespace_name,segment_type;  

相关内容