Oracle tablespace 对象迁移

The following SQL generates SQL to move the existing objects; you will want to change the criteria as needed for your situation; this shows moving objects owned by SCOTT from SYSAUX tablespace to TARGET_TS:

Tables

复制代码

select 'alter table ' || do.owner || '.' || do.object_name || ' move tablespace TARGET_TS;' as cmd_to_invoke from dba_objects do, dba_segments ds, dba_tables dt where do.owner = 'SCOTT' and do.owner=ds.owner and do.owner=dt.owner and do.object_name = ds.segment_name and do.object_name=dt.table_name and dt.iot_name is null and do.object_type='TABLE' and ds.tablespace_name = 'SYSAUX';

Partitioned Tables

复制代码

alter table SCOTT.EMP modify default attributes tablespace TARGET_TS;

-- Then invoke the SQL generated by this query:

复制代码

select distinct 'alter table ' || dt.table_owner || '.' || dt.table_name || ' move partition ' || dt.partition_name || ' tablespace TARGET_TS' || ';' as cmd_to_invoke from dba_tab_partitions dt where dt.table_owner='SCOTT'and dt.tablespace_name='SYSAUX' order by 1;

IOT Tables

复制代码

select 'alter table ' || owner || '.' || table_name || ' move tablespace DEF_TABLESPACE;' from dba_indexes where owner = 'SCOTT' and index_type = 'IOT - TOP' and tablespace_name='SYSAUX';

Indexes

复制代码

select 'alter index ' || do.owner || '.' || do.object_name || ' rebuild tablespace apex;' as cmd from dba_objects do, dba_segments ds, dba_indexes di where do.owner = 'SCOTT' and do.owner=ds.owner and do.owner=di.owner and do.object_type = 'INDEX' and di.index_type in('NORMAL', 'FUNCTION-BASED NORMAL') and do.object_name = ds.segment_name and do.object_name = di.index_name and ds.tablespace_name='SYSAUX';

Partitioned Indexes

Similar to Partitioned Tables but using alter index and referencing either DBA_INDEXES or DBA_IND_PARTITIONS

LOBS

LOBS on Non-partitioned Tables

复制代码

select distinct 'alter table ' || dtc.owner || '.' || dtc.table_name || ' move lob(' || column_name || ')' || ' store as (tablespace DEF_TABLESPACE);' as cmd_to_invoke from dba_tab_columns dtc, dba_tables dt, dba_indexes di where dt.owner=dtc.owner and dt.owner=di.owner and dt.table_name=di.table_name and dt.table_name=dtc.table_name and dtc.owner = 'SCOTT' and dtc.data_type like '%LOB%' and di.index_name in (select segment_name from dba_segments inner where inner.owner=dt.owner and tablespace_name='SYSAUX') ;

LOBS on Partitioned Tables

复制代码

select distinct 'alter table ' || dtc.owner || '.' || dtc.table_name || ' move partition ' || dt.partition_name || ' lob(' || column_name || ')' || ' store as (tablespace TARGET_TS)' || ';' from dba_tab_columns dtc, dba_tab_partitions dt, dba_indexes di where dt.table_owner=dtc.owner and dt.table_owner=di.owner and dt.table_name=di.table_name and dt.table_name=dtc.table_name and dtc.owner ='SCOTT' and dtc.data_type like '%LOB%' and di.index_name in (select segment_name from dba_segments inner where inner.owner=dt.table_owner and tablespace_name='SYSAUX')

Moving the user table in another tablespace

复制代码
BEGIN
  FOR i IN (
    SELECT * FROM ALL_tables where owner = :owner 
      and (tablespace_name is null or tablespace_name != :tbs)
      and temporary != 'Y'
      and partitioned != 'YES'
    ) LOOP
    EXECUTE IMMEDIATE 'ALTER TABLE '  || i.table_name || ' MOVE TABLESPACE ' || :tbs;
  END LOOP; 
END;
 BEGIN
  FOR i IN (
    SELECT * FROM ALL_tab_partitions 
    WHERE table_owner = :owner and tablespace_name != :tbs
  ) LOOP
    EXECUTE IMMEDIATE 'ALTER TABLE ' 
      || i.table_name || ' MOVE PARTITION '
      || i.partition_name ||' TABLESPACE '|| :tbs;
  END LOOP;
END;
BEGIN
  FOR i IN (
    SELECT * FROM ALL_tables where owner = :owner and partitioned = 'YES'
  ) LOOP
    EXECUTE IMMEDIATE 'ALTER TABLE '
      || i.table_name || ' MODIFY DEFAULT ATTRIBUTES TABLESPACE ' || :tbs;
  END LOOP;
END;
BEGIN
  FOR i IN (
    SELECT * FROM ALL_Lobs WHERE owner = :owner 
      and (tablespace_name is null or tablespace_name != :tbs)
  ) LOOP
    EXECUTE IMMEDIATE 'ALTER TABLE '  || i.table_name || ' MOVE TABLESPACE ' || :tbs;
  END LOOP; 
END;

Moving indexes to another user tablespace

复制代码
BEGIN
  FOR i IN (
    SELECT * FROM ALL_indexes 
    where owner = :owner 
      and tablespace_name != :index_tbs
      and index_type != 'IOT - TOP' 
  ) LOOP
    EXECUTE IMMEDIATE 'ALTER INDEX '  || i.index_name || ' REBUILD TABLESPACE ' || :index_tbs;
  END LOOP;
END;
BEGIN
  FOR i IN (
    SELECT * FROM ALL_indexes 
    where owner = :owner 
      and partitioned = 'YES' 
  ) LOOP
    EXECUTE IMMEDIATE 'ALTER INDEX '  || i.index_name 
      || ' MODIFY DEFAULT ATTRIBUTES TABLESPACE ' || :index_tbs;
  END LOOP;
END; 
BEGIN
  FOR i IN (
    SELECT * FROM ALL_ind_partitions 
    where index_owner = :owner 
      and tablespace_name != :index_tbs 
  ) LOOP
    EXECUTE IMMEDIATE 'ALTER INDEX '  
      || i.index_name || '  REBUILD PARTITION ' 
      || i.partition_name || ' TABLESPACE ' || :index_tbs;
  END LOOP;
END;

Moving Lob segment

复制代码
BEGIN
  FOR i IN (
    select * from user_lobs where (tablespace_name is null or tablespace_name != :tbs)
    ) LOOP
    EXECUTE IMMEDIATE 'ALTER TABLE '  || i.table_name || ' MOVE TABLESPACE ' || :tbs
      || ' LOB (' || i.column_name || ')' 
      || ' STORE AS ' || i.segment_name || ' (TABLESPACE ' ||  :tbs ||' )';
  END LOOP;
相关推荐
m0_747854521 天前
mysql如何设置数据库连接字符编码_修改default-character
jvm·数据库·python
Wyz201210241 天前
如何在 React 中正确将父组件函数传递给子组件并触发调用
jvm·数据库·python
2401_865439631 天前
Go语言如何用logrus_Go语言logrus日志框架教程【技巧】
jvm·数据库·python
NotFound4861 天前
CSS如何利用Flex实现悬浮的侧边按钮组_利用fixed定位与flex布局组合
jvm·数据库·python
神の愛1 天前
@Pattern,@NotBlank
数据库·mysql
qq_189807031 天前
Golang怎么实现RBAC权限控制_Golang如何用casbin实现基于角色的访问控制系统【教程】
jvm·数据库·python
vegetablec1 天前
CSS如何处理相对定位留下的原本占位空白_认识到相对定位不会脱离文档流,需借助负margin消除视觉空隙
jvm·数据库·python
2401_832635581 天前
HTML怎么创建响应式图片备选方案_HTML srcset与sizes结构【详解】
jvm·数据库·python
浅念-1 天前
从LeetCode入门位运算:常见技巧与实战题目全解析
数据结构·数据库·c++·笔记·算法·leetcode·牛客
Chasing__Dreams1 天前
Mysql--基础知识点--99--两个线程同时给同一个间隙加锁 造成死锁的原因
数据库·mysql