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;
相关推荐
凌虚11 小时前
面向 MySQL 用户的 PostgreSQL 快速上手指南
数据库·后端·架构
五阿哥永琪11 小时前
MySQL中操作json的函数!
数据库·mysql·json
来者皆善11 小时前
了解Mysql优化吗?如何优化索引?
数据库·mysql
赤壁小虾13 小时前
【渲染流水线】[逐片元阶段]-[透明度测试]以UnityURP为例
java·前端·数据库
MC皮蛋侠客13 小时前
SQLAlchemy 系列(七):高级建模与高效写入——批量 DML、方言与扩展
数据库·python
奈斯先生Vector14 小时前
把 Midjourney 二次编辑做成生产系统:customId 能力令牌、Action Graph 与 WebUI 精修工作台
数据库·人工智能·架构·aigc·音视频·midjourney
Lethehong15 小时前
双擎并驱·全链路并行:KFS让TB级异构增量同步秒级到达
数据库
MC皮蛋侠客15 小时前
SQLAlchemy 系列(八):AsyncIO、并发与 Web 生命周期——让每个并发任务持有自己的 Session
数据库·python
一水15 小时前
Redis实战:一个AI工作流系统里的五个应用场景,从传参到限流的完整链路
数据库·redis·wpf
枫叶v.16 小时前
Prompt Injection 防不住怎么办?从 Source-Sink 模型设计 Agent 安全边界
数据库·安全·prompt