如何在oracle中从表中分离分区并将其附加到另一个表?

发布于 2024-12-28 18:04:18 字数 126 浏览 1 评论 0原文

我有一个包含 5 年的海量数据(比如数百万条记录,但这只是一个案例研究!)的表,每年都有一个分区。现在我想保留最近 2 年的数据,并将其余 3 年的数据传输到一个名为存档的新表中?

停机时间最短且性能较高的理想方法是什么?

I have a table with huge data( say millions of records, its just a case study though!) of 5 years, with a partition for each year. Now i would want to retain the last 2 years data, and transfer the rest of the 3 year data to a new table called archive?

What would be the Ideal method, with minimal down time and high performance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

庆幸我还是我 2025-01-04 18:04:18
alter table exchange partition 

就是答案。该命令将分区的段与表的段交换。它的速度是光速,因为它只进行一些参考交换。
所以,你需要一些临时表,因为据我所知你不能直接交换它们。

例如:

create table tmp_table(same columns);
Add partition p_2011 in table ARCH_TABLE;

ALTER TABLE CURR_TABLE EXCHANGE PARTITION P_2011 WITH TABLE tmp_table;
ALTER TABLE ARCH_TABLE EXCHANGE PARTITION P_2011 WITH TABLE tmp_table;

请在运行之前测试您的代码。

alter table exchange partition 

is the answer. This command exange the segment of a partition with the segment of a table. It is at light speed because it does only some reference interchages.
So, you need some temp tables, because AFAIK you can't exchange them directly.

Something like:

create table tmp_table(same columns);
Add partition p_2011 in table ARCH_TABLE;

ALTER TABLE CURR_TABLE EXCHANGE PARTITION P_2011 WITH TABLE tmp_table;
ALTER TABLE ARCH_TABLE EXCHANGE PARTITION P_2011 WITH TABLE tmp_table;

Please test test your code before run.

清旖 2025-01-04 18:04:18

如果您的表按 RANGE、INTERVAL 或 HASH 分区进行分区,那么使用分区交换应该不会出现重大问题:

ALTER TABLE <partitioned table>
EXCHANGE PARTITION <partition name>
WITH TABLE <non-partitioned temporary table>

最后使用可选指令 WITHOUT VALIDATIONUPDATE GLOBAL INDEXES

但请记住,如果要移动使用 PARTITION BY REFERENCE 的表分区或在按引用分区的表中包含按引用使用的数据的父表,则必须使用 Oracle Server 版本 12.1 或更高版本。在 Oracle Server 11gR2 中,不完全支持按引用分区的表的分区交换,您将偶然发现日期/引用完整性错误

ORA-02266: unique/primary keys in table referenced by enabled foreign keys
ORA-14128: FOREIGN KEY constraint mismatch in ALTER TABLE EXCHANGE PARTITION

,或者如果您想要禁用或删除受影响的约束

ORA-14650: operation not supported for reference-partitioned tables

,如果您不能/不会升级到 Oracle Server 12.1+,为您提供将数据复制到存档表的功能,并且从原始表中删除是唯一的选项。

If your table is partitioned by RANGE, INTERVAL or HASH partitioning, you shouldn't have significant problems by using partition exchange:

ALTER TABLE <partitioned table>
EXCHANGE PARTITION <partition name>
WITH TABLE <non-partitioned temporary table>

With optional directives WITHOUT VALIDATION and UPDATE GLOBAL INDEXES at end.

However keep in mind that if you want to move partitions of tables that either use PARTITION BY REFERENCE or parent tables that have data used by reference in tables partitioned by reference, you must use Oracle Server version 12.1 or later. In Oracle Server 11gR2 partition exchange for tables partitioned by reference, is not exactly supported and you will stumble upon date/reference integrity errors

ORA-02266: unique/primary keys in table referenced by enabled foreign keys
ORA-14128: FOREIGN KEY constraint mismatch in ALTER TABLE EXCHANGE PARTITION

or if you want to disable or remove affected constraints

ORA-14650: operation not supported for reference-partitioned tables

Which if you can't/won't upgrade to Oracle Server 12.1+, give you the copying data to archive tables with deleting from original tables as the only option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文