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;
相关推荐
编程饭碗1 小时前
【Mysql日期字段】
数据库·mysql
Thomas.Sir1 小时前
精通 MySQL 面试题
数据结构·数据库·mysql
上海云盾-小余1 小时前
应用层漏洞实战防护:SQL 注入、XSS、文件上传漏洞一站式加固方案
数据库·sql·xss
鸽芷咕1 小时前
从语法兼容到语义一致:深度解析金仓如何“无感”承接MySQL复杂业务
数据库·mysql
新缸中之脑1 小时前
AI智能体评估指南
数据库·人工智能·oracle
add45a1 小时前
Python类型提示(Type Hints)详解
jvm·数据库·python
曾阿伦2 小时前
SQL 用法详解:从基础操作到进阶实战的全场景指南
数据库·sql
NCU_wander2 小时前
操作系统/数据库和业务应用/中间件/硬件之间的关系
数据库·中间件
Navicat中国2 小时前
如何从0到1完成函数设计 | Navicat 教程
数据库·函数·navicat