暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 交换分区问题

askTom 2018-01-23
340

问题描述

嗨,

我们已经使用以下语句将着陆表的分区与历史表交换了

ALTER TABLE lnd.hist_tbl EXCHANGE PARTITION v_partname WITH TABLE lnd.curr_tbl WITHOUT VALIDATION;

v_partname - max partition name fetched by using the below query 

SELECT MAX(partition_name) KEEP (DENSE_RANK LAST ORDER BY PARTITION_POSITION) partition_name INTO v_partname
            FROM user_tab_partitions
           WHERE table_name = hist_tbl
        GROUP BY table_name
        ORDER BY 1;
复制


我们希望将当前表数据追加到历史表中,并为每次运行清空当前表。
但是对于每次运行,分区将在当前表和历史表之间交换。

在第一次运行中,数据被移动到历史分区,新数据被添加到当前表中。

在第二次运行中,数据将移至新的历史分区,并且当前数据将添加到先前的历史分区,并将其变为当前分区。

即,如果我们运行批处理1,2,3,4,5,6,则当前表包含2,4 6个批处理数据,而历史表包含1,3 5个批处理数据。

我们不希望这种情况发生。但是,同一模式中的很少表可以正常更新为当前表中的6批数据,而历史表中的1,2,3,4,5批数据。

您能否请为什么会发生这种情况,并帮助我们解决此问题。

提前谢谢。




专家解答

Exchange将命名分区中的所有行与其他表中的所有行进行分区:

create table hist (
  x int
) partition by range (x) (
  partition p0 values less than (10)
);
create table curr for exchange with table hist; --12.2 syntax

insert into curr values (1);
alter table hist exchange partition p0 with table curr;
insert into curr values (2);
alter table hist exchange partition p0 with table curr;
insert into curr values (3);
alter table hist exchange partition p0 with table curr;
insert into curr values (4);
alter table hist exchange partition p0 with table curr;

select * from hist;

X   
  2 
  4

select * from curr;

X   
  1 
  3 
复制


这几乎可以肯定,因为您获取分区的查询始终返回相同的值。假设您已按批处理 # 以将每个分区放置在自己的分区中的方式进行分区,则可以在exchange中使用 “partition for” 语法来解决此问题。这将通过分区中的值而不是名称来标识分区:

create table hist (
  x int
) partition by range (x) (
  partition p0 values less than (1),
  partition p1 values less than (2),
  partition p2 values less than (3),
  partition p3 values less than (4),
  partition p4 values less than (5)
);
create table curr (
  x int
);

insert into curr values (1);
alter table hist exchange partition for (1) with table curr;
insert into curr values (2);
alter table hist exchange partition for (2) with table curr;
insert into curr values (3);
alter table hist exchange partition for (3) with table curr;
insert into curr values (4);
alter table hist exchange partition for (4) with table curr;

select * from hist;

X   
  1 
  2 
  3 
  4 

select * from curr;

no rows selected
复制

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论