顺便再提一下11g新增的其他分区策略。
针对Range Partition,11g有了一种更加灵活的方式:Interval Partition。例如,我们一些分区表是依赖于时间做的范围分区:每个月的数据存放到一个分区中。随着数据的增长,还需要有一个作业来增加新的分区以满足上述策略。而在11g中,通过Interval Parition,就无需这中人为的维护作业了,Oracle会为新的数据自动增加分区:
SQL代码
SQL> create table par_test2 (a number, b date)
2 partition by range (b)
3 interval (numtoyminterval(1,'MONTH'))
4 store in (example)
5 (
6 partition values less than (to_date('2009-09-01','yyyy-mm-dd'))
7 )
8 ;
Table created.
SQL> insert into par_test2 values(1, sysdate);
1 row created.
SQL> commit;
Commit complete.
SQL> select partition_name, high_value from dba_tab_partitions
2 where table_name = 'PAR_TEST2';
PARTITION_NAME HIGH_VALUE
------------------------------ --------------------------------------------------------------------------------
SYS_P164 TO_DATE(' 2009-09-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA '
SQL> insert into par_test2 values(1, to_date('2009-10-01','yyyy-mm-dd'));
1 row created.
SQL> commit;
Commit complete.
SQL> select partition_name, high_value from dba_tab_partitions
2 where table_name = 'PAR_TEST2';
PARTITION_NAME HIGH_VALUE
------------------------------ --------------------------------------------------------------------------------
SYS_P164 TO_DATE(' 2009-09-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA '
SYS_P165 TO_DATE(' 2009-11-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA '
SQL> insert into par_test2 values(1, sysdate - interval '1' MONTH);
1 row created.
SQL> commit;
Commit complete.
SQL> select * from par_test2;
A B
---------- ---------
1 31-AUG-09
1 31-JUL-09
1 01-OCT-09
SQL> select partition_name, high_value from dba_tab_partitions
2 where table_name = 'PAR_TEST2';
PARTITION_NAME HIGH_VALUE
------------------------------ --------------------------------------------------------------------------------
SYS_P164 TO_DATE(' 2009-09-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA '
SYS_P165 TO_DATE(' 2009-11-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA '
从上述例子可以注意到,由于Range Partition的表达式的比较操作符是Less Than,因此,Interval Partition只会对超出(分区最大Partition Key值+Interval值)的数据创建新分区。同理,在指定Interval分区时,就不能再指定less than (MAXVALUE)了,否则Interval分区就没有意义了。
11g中还引入了一种新的分区策略:关联分区。在10g和之前版本,我们在做分区表时可能会遇到这样的问题:一个主表和多个子表都需要做分区,而分区所基于的关键值是只存在与主表中的一个字段,这时,我们就需要将这个字段冗余到子表当中去才能实现主表、子表采用相同的分区策略。11g中,这个问题可以通过关联分区解决了:
SQL代码
SQL> create table par_main (pid number primary key, crt_date date)
2 partition by range (crt_date)
3 (
4 partition values less than (to_date('2009-09-01','yyyy-mm-dd')),
5 partition values less than (maxvalue)
6 );
Table created.
SQL> create table par_child (
2 cid number primary key,
3 pid number not null,
4 constraint par_main_fk foreign key (pid) references par_main(pid)
5 )
6 partition by reference (par_main_fk);
Table created.