将分区移动到另一个表空间
分区表是一种将数据分成若干段的表。段不同于包含具体数据的模式对象。
不同的段可以存储在不同的表空间中,这就是为什么不能像普通表一样移动分区表的原因。如果您确实将其视为普通表,则会收到错误 ORA-14511。
将分区表完全移动到另一个表空间有两个主要步骤。
- 移动所有分区段
- 修改表存储属性
A. 移动所有分区段
在这一步中,我们必须将每个单独的分区按顺序移动到另一个表空间,但首先,我们必须编写用于移动单个分区的语句。
1. 编写可执行的 SQL 语句
在这种情况下,我们希望将分区表SH.SALES移动到另一个表空间。
SQL> column stmts format a90;
SQL> set pagesize 100;
SQL> select 'alter table ' || table_owner || '.' || table_name || ' move partition ' || partition_name || ' tablespace USERS;' stmts from all_tab_partitions where table_owner = 'SH' and table_name = 'SALES' order by 1;
STMTS
--------------------------------------------------------------------------------
alter table SH.SALES move partition SALES_1995 tablespace USERS;
alter table SH.SALES move partition SALES_1996 tablespace USERS;
alter table SH.SALES move partition SALES_H1_1997 tablespace USERS;
alter table SH.SALES move partition SALES_H2_1997 tablespace USERS;
alter table SH.SALES move partition SALES_Q1_1998 tablespace USERS;
alter table SH.SALES move partition SALES_Q1_1999 tablespace USERS;
alter table SH.SALES move partition SALES_Q1_2000 tablespace USERS;
alter table SH.SALES move partition SALES_Q1_2001 tablespace USERS;
alter table SH.SALES move partition SALES_Q1_2002 tablespace USERS;
alter table SH.SALES move partition SALES_Q1_2003 tablespace USERS;
alter table SH.SALES move partition SALES_Q2_1998 tablespace USERS;
alter table SH.SALES move partition SALES_Q2_1999 tablespace USERS;
alter table SH.SALES move partition SALES_Q2_2000 tablespace USERS;
alter table SH.SALES move partition SALES_Q2_2001 tablespace USERS;
alter table SH.SALES move partition SALES_Q2_2002 tablespace USERS;
alter table SH.SALES move partition SALES_Q2_2003 tablespace USERS;
alter table SH.SALES move partition SALES_Q3_1998 tablespace USERS;
alter table SH.SALES move partition SALES_Q3_1999 tablespace USERS;
alter table SH.SALES move partition SALES_Q3_2000 tablespace USERS;
alter table SH.SALES move partition SALES_Q3_2001 tablespace USERS;
alter table SH.SALES move partition SALES_Q3_2002 tablespace USERS;
alter table SH.SALES move partition SALES_Q3_2003 tablespace USERS;
alter table SH.SALES move partition SALES_Q4_1998 tablespace USERS;
alter table SH.SALES move partition SALES_Q4_1999 tablespace USERS;
alter table SH.SALES move partition SALES_Q4_2000 tablespace USERS;
alter table SH.SALES move partition SALES_Q4_2001 tablespace USERS;
alter table SH.SALES move partition SALES_Q4_2002 tablespace USERS;
alter table SH.SALES move partition SALES_Q4_2003 tablespace USERS;
28 rows selected.2.执行SQL语句
然后我们一一执行上面的语句。
SQL> alter table SH.SALES move partition SALES_1995 tablespace USERS;
Table altered.
SQL> alter table SH.SALES move partition SALES_1996 tablespace USERS;
Table altered.
SQL> alter table SH.SALES move partition SALES_H1_1997 tablespace USERS;
Table altered.
...3. 在线条款
从 12.2 版本开始,我们可以在ALTER TABLE MOVE PARTITION中添加ONLINE修饰符来移动表,而不会像Data Manipulation Language (DML)这样阻塞事务。这个特性就像我们在网上重建索引时所做的一样。
SQL> select 'alter table ' || table_owner || '.' || table_name || ' move partition ' || partition_name || ' tablespace USERS online;' stmts from all_tab_partitions where table_owner = 'SH' and table_name = 'SALES' order by 1;您可以利用在线移动分区的优势。
B.修改表存储属性
对于以后任何新的分区,我们应该改变表的存储属性。
SQL> alter table SH.SALES modify default attributes tablespace USERS;
Table altered.上述语句使用modify_table_default_attrs子句与segment_attributes_clause结合形成有用的 SQL 命令。
通过执行上述两个步骤,我们将整个分区表移动到另一个表空间。
原文标题:Alter Table Move Partition to Another Tablespace
原文作者:Ed Chen
原文链接:https://logic.edchen.org/how-oracle-move-partitioned-table/
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




