oracle11g之前,维护分区需要手工,11g之后使用interval来实现自动扩展分区,使得维护工作变得简单。
根据年: INTERVAL(NUMTOYMINTERVAL(1,'YEAR'))
根据月: INTERVAL(NUMTOYMINTERVAL(1,'MONTH'))
根据天: INTERVAL(NUMTODSINTERVAL(1,'DAY'))
根据时分秒: NUMTODSINTERVAL( n, { 'DAY'|'HOUR'|'MINUTE'|'SECOND'})
下面使用按月分区做个试验:
创建分区表:
create table zl_range (id number not null PRIMARY KEY, test_date date) partition by range (test_date) interval (numtoyminterval (1,'MONTH')) ( partition p_2014_01_01 values less than (to_date('2014-01-01', 'yyyy-mm-dd')) );
查看ZL_RANGE表的分区:
select partition_name from user_tab_partitions where table_name='ZL_RANGE';
可以看到只有一个分区
插入不同月份的数据:
INSERT INTO ZL_RANGE VALUES(1,TO_DATE('2013-12-01','YYYY-MM-DD')); INSERT INTO ZL_RANGE VALUES(2,TO_DATE('2014-01-01','YYYY-MM-DD')); INSERT INTO ZL_RANGE VALUES(3,TO_DATE('2014-12-01','YYYY-MM-DD')); INSERT INTO ZL_RANGE VALUES(4,TO_DATE('2014-12-10','YYYY-MM-DD')); INSERT INTO ZL_RANGE VALUES(5,TO_DATE('2013-11-10','YYYY-MM-DD')); INSERT INTO ZL_RANGE VALUES(6,TO_DATE('2014-11-10','YYYY-MM-DD')); COMMIT;
查看分区,可以看到有四个分区:
如果不喜欢分区的名字,可以自己修改:
alter table t_range rename partition SYS_P41 to p_2014_02_01; alter table t_range rename partition SYS_P42 to p_2015_01_01; alter table t_range rename partition SYS_P43 to p_2014_12_01;
查询表分区的信息
SELECT * FROM USER_SEGMENTS;
清空表分区数据:
alter table 表名 truncate partition 分区名;
直接删除分区:
alter table 表名 drop partition 分区名;