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;
相关推荐
AI-好学者2 小时前
阶段一-图数据库基础与PropertyGraph模型
数据库·rag·knowledge graph·graphrag
其实防守也摸鱼3 小时前
运维--学习阶段问题解答(1)(自测)
linux·运维·服务器·数据库·学习·自动化·命令模式
懒鸟一枚4 小时前
深入理解 Linux 内存、Swap 交换分区与分页机制的关系
java·linux·数据库
龙仔7254 小时前
SQL Server 创建只读账号完整操作(分两种场景:SSMS图形界面 + T-SQL脚本)
数据库·sql·oracle
霁月的小屋4 小时前
生产环境中的事务实践——银行系统上线记(四)
数据库
Database_Cool_4 小时前
AI 应用数据底座首选:阿里云 PolarDB 为大模型 RAG 提供一体化支撑
数据库·阿里云
玖玥拾5 小时前
C# 语言进阶(十五)C# 游戏服务端 MySQL 数据库
服务器·开发语言·网络·数据库·mysql·c#
IvorySQL6 小时前
PG 日报|社区讨论重构 pg_hba 配置文件格式
数据库·人工智能·postgresql·重构·ivorysql
逝水无殇6 小时前
C# 文件的输入与输出详解
开发语言·数据库·后端·c#
罗政7 小时前
PDF 批量合并工具:本地 AI 自动排序、识别正文日期与合同编号
数据库·pdf·php