我可以在oracle中对已经存在的表进行分区吗?

时间:2015-11-20 12:22:02

标签: oracle database-partitioning partition

我的oradb中有一张表,因为它已经有很多行了。

我希望按月重新划分此表。所以我不想丢失表中的数据。

我想要这种分区类型:

PARTITION BY RANGE (date_column)
INTERVAL (NUMTOYMINTERVAL (1,'MONTH')) 
         (partition transaction_old values less than (to_date('01-01-2015','DD-MM-YYYY')));

如何将此设置为现有表格?我想使用 alter table 函数,但它不起作用:

alter table mytable
    PARTITION BY RANGE (date_column)
    INTERVAL (NUMTOYMINTERVAL (1,'MONTH')) 
             ( partition transaction_old values less than (to_date('01-01-2015','DD-MM-YYYY') ));

我的问题有解决办法吗?

2 个答案:

答案 0 :(得分:4)

您不能直接分区现有的非分区表。您需要根据以下分区方法创建临时表/新表:

  • <强> DBMS_REDEFINITION

    1. 创建分区中间表
    2. 开始重新定义过程
    3. 创建约束和索引(依赖关系)
    4. 完成重新定义过程
  • <强> EXCHANGE PARTITION

    1. 创建分区目标表
    2. EXCHANGE PARTITION
    3. SPLIT PARTITION(如果需要将单个大分区拆分为较小的分区)

答案 1 :(得分:3)

可能最简单,最安全的方法是使用dbms_redefinition - 它将复制授权,索引,约束,一切。

Table P is the parent table. 

Table T1 is the existing non-partitioned table. 
Table T2 is the new partitioned table. 

At the end, we are left with P and T1 - T1 being partitioned. 


ops$tkyte%ORA10GR2> create table p ( x primary key )
  2  as
  3  select user_id from all_users;

Table created.

ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2> create table t1
  2  as
  3  select * from all_users
  4  /

Table created.

ops$tkyte%ORA10GR2> alter table t1 add constraint t1_pk primary key(user_id);

Table altered.

ops$tkyte%ORA10GR2> alter table t1 add constraint t1_fk foreign key(user_id) references 
p(x);

Table altered.

ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2> create table t2
  2  ( username varchar2(30),
  3    user_id  number,
  4    created  date
  5  )
  6  partition by hash(user_id) partitions 8;

Table created.

ops$tkyte%ORA10GR2> exec dbms_redefinition.can_redef_table( user, 'T1' );

PL/SQL procedure successfully completed.

ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2> exec dbms_redefinition.start_redef_table( user, 'T1', 'T2' );

PL/SQL procedure successfully completed.

ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2> variable nerrors number
ops$tkyte%ORA10GR2> begin
  2          dbms_redefinition.copy_table_dependents
  3          ( user, 'T1', 'T2',
  4            copy_indexes => dbms_redefinition.cons_orig_params,
  5            num_errors => :nerrors );
  6  end;
  7  /

PL/SQL procedure successfully completed.

ops$tkyte%ORA10GR2> exec dbms_redefinition.finish_redef_table( user, 'T1', 'T2' );

PL/SQL procedure successfully completed.

ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2> select dbms_metadata.get_ddl( 'TABLE', 'T1' ) from dual;

DBMS_METADATA.GET_DDL('TABLE','T1')
-------------------------------------------------------------------------------

  CREATE TABLE "OPS$TKYTE"."T1"
   (    "USERNAME" VARCHAR2(30) CONSTRAINT "SYS_C0026838" NOT NULL ENABLE NOVALIDA
TE,
        "USER_ID" NUMBER CONSTRAINT "SYS_C0026839" NOT NULL ENABLE NOVALIDATE,
        "CREATED" DATE CONSTRAINT "SYS_C0026840" NOT NULL ENABLE NOVALIDATE,
         CONSTRAINT "T1_PK" PRIMARY KEY ("USER_ID")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "USERS"  ENABLE NOVALIDATE,
         CONSTRAINT "T1_FK" FOREIGN KEY ("USER_ID")
          REFERENCES "OPS$TKYTE"."P" ("X") ENABLE NOVALIDATE
   ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
  STORAGE(
  BUFFER_POOL DEFAULT)
  TABLESPACE "USERS"
  PARTITION BY HASH ("USER_ID")
 (PARTITION "SYS_P1017"
   TABLESPACE "USERS",
 PARTITION "SYS_P1018"
   TABLESPACE "USERS",
 PARTITION "SYS_P1019"
   TABLESPACE "USERS",
 PARTITION "SYS_P1020"
   TABLESPACE "USERS",
 PARTITION "SYS_P1021"
   TABLESPACE "USERS",
 PARTITION "SYS_P1022"
   TABLESPACE "USERS",
 PARTITION "SYS_P1023"
   TABLESPACE "USERS",
 PARTITION "SYS_P1024"
   TABLESPACE "USERS")



ops$tkyte%ORA10GR2> select constraint_name, constraint_type from user_constraints where 
table_name = 'T1';

CONSTRAINT_NAME                C
------------------------------ -
SYS_C0026838                   C
SYS_C0026839                   C
SYS_C0026840                   C
T1_PK                          P
T1_FK                          R

Source