oracle中如何创建月度分区?

问题描述 投票:0回答:1

我有一张包含 2017 年和 2018 年数据的表格。需要在该表上创建每月分区。

因此我创建了一个非分区表并加载了原始表中的所有数据。现在我正在将新表转换为每月分区表。

当我将错误更改为

ORA-14300: 分区键映射到超出最大值的分区 允许的分区数量

我的脚本是

ALTER TABLE ORDERHDR_PART MODIFY 
PARTITION BY RANGE (LASTUPDATE) INTERVAL(NUMTOYMINTERVAL(1, 'MONTH'))
  (
   PARTITION ORDERHDR_PART_JAN VALUES less than (TO_DATE('01-02-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_FEB VALUES less than (TO_DATE('01-03-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_MAR VALUES less than (TO_DATE('01-04-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_APR VALUES less than (TO_DATE('01-05-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_MAY VALUES less than (TO_DATE('01-06-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_JUN VALUES less than (TO_DATE('01-07-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_JUL VALUES less than (TO_DATE('01-08-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_AUG VALUES less than (TO_DATE('01-09-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_SEP VALUES less than (TO_DATE('01-10-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_OCT VALUES less than (TO_DATE('01-11-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_NOV VALUES less than (TO_DATE('01-12-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_DEC VALUES less than (TO_DATE('01-01-2019','DD-MM-YYYY'))
   )ONLINE;
oracle oracle12c
1个回答
0
投票

我认为你的做法是错误的。

首先创建一个分区表,例如

CREATE TABLE ORDERHDR_PART (....)
PARTITION BY RANGE (LASTUPDATE) INTERVAL (NUMTOYMINTERVAL(1, 'MONTH'))
  (
  PARTITION ORDERHDR_INITIAL VALUES less than (DATE '2000-01-01')
  );

然后将现有数据传输到新表中。

要么使用简单的

INSERT INTO ORDERHDR_PART SELECT * FROM ORDERHDR_2017;
Oracle 将根据
LASTUPDATE
值自动创建每月分区。 使用这种方法,您将复制(临时)您的数据和/或您可能会面临性能问题。

另一种方法是使用交换分区,应该是这样的

ALTER TABLE ORDERHDR_PART 
  EXCHANGE PARTITION FOR (DATE '2017-01-01')
  WITH TABLE ORDERHDR_2017
  INCLUDING INDEXES;

我不知道“PARTITION FOR (DATE '2017-01-01')”是否会自动创建,也许你必须先运行

INSERT INTO ORDERHDR_PART (LASTUPDATE) VALUES (DATE '2017-01-01'); ROLLBACK;
才能创建它。

您将获得所有月份的一个分区,之后您可以使用拆分为多个分区来拆分分区。应该是这样的:

ALTER TABLE ORDERHDR_PART SPLIT PARTITION FOR (DATE '2017-01-01') INTO ( 
   PARTITION ORDERHDR_PART_JAN VALUES less than (TO_DATE('01-02-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_FEB VALUES less than (TO_DATE('01-03-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_MAR VALUES less than (TO_DATE('01-04-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_APR VALUES less than (TO_DATE('01-05-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_MAY VALUES less than (TO_DATE('01-06-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_JUN VALUES less than (TO_DATE('01-07-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_JUL VALUES less than (TO_DATE('01-08-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_AUG VALUES less than (TO_DATE('01-09-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_SEP VALUES less than (TO_DATE('01-10-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_OCT VALUES less than (TO_DATE('01-11-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_NOV VALUES less than (TO_DATE('01-12-2018','DD-MM-YYYY')),
   PARTITION ORDERHDR_PART_DEC VALUES less than (TO_DATE('01-01-2019','DD-MM-YYYY'))
);

注意,默认情况下您无法删除 RANGE 分区表的初始分区。如果您遇到此问题,请执行:

ALTER TABLE ORDERHDR_PART SET INTERVAL ();
ALTER TABLE ORDERHDR_PART DROP PARTITION ORDERHDR_INITIAL;
ALTER TABLE ORDERHDR_PART SET INTERVAL (NUMTOYMINTERVAL(1, 'MONTH'));
© www.soinside.com 2019 - 2024. All rights reserved.