oracleinsert指定分区,ORACLE区间分区
(IntervalPartitioning)
11g在分区上有了很⼤的改进,其中有⼀个特性是Interval-Partition,他是range分区的派⽣,⾃动创建指定间隔的分区。
在 11g 之前,如果数据中出现未能匹配分区条件的情况,系统会拒绝进⾏数据操作。
对时间类型和数字类型的⽀持:
1.对于采⽤date类型或TIMESTAMP类型的Interval Partitioning可以⽀持按year、month、day、hour、minute、second六种类型的定长宽度分区,分别通过如下函数转换
oracle 时间转换numtoyminterval ( n, { 'YEAR'|'MONTH'})
numtodsinterval ( n, { 'DAY'|'HOUR'|'MINUTE'|'SECOND'})
2. Interval (number) 即多少值⼀个分区
创建⽰例
1.时间类型
--创建表
SQL> CREATE TABLE cai.intervalpart1 (c1 NUMBER, c2 DATE)
2 PARTITION BY RANGE (c2)
3 INTERVAL ( NUMTOYMINTERVAL (1, 'MONTH') )
4 (PARTITION part1
5 VALUES LESS THAN (TO_DATE ('02/01/2013', 'MM/DD/YYYY'))
6 );
Table created.
--查询只有⼀个分区
SQL> select table_name, partition_name, HIGH_VALUE
2 from dba_tab_partitions t
3 where table_name = 'INTERVALPART1';
TABLE_NAME PARTITION_NAME HIGH_VALUE
---------------- ----------------- -------------------------------------------------------
INTERVALPART1 PART1 TO_DATE(' 2013-02-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIA
--增加2⾏记录⼀⾏是2⽉份的,⼀⾏是1⽉份的
SQL> insert into cai.intervalpart1 values (1,TO_DATE ('02/02/2013', 'MM/DD/YYYY'));
1 row created.
SQL> commit;
Commit complete.
SQL> insert into cai.intervalpart1 values (1,TO_DATE ('01/01/2013', 'MM/DD/YYYY'));
1 row created.
SQL> commit;
Commit complete.
SQL>
SQL> Select Count(1) From cai.intervalpart1;
COUNT(1)
----------
2
--增加了2⽉份的分区
SQL>
SQL> select table_name, partition_name, HIGH_VALUE
2 from dba_tab_partitions t
3 where table_name = 'INTERVALPART1';
TABLE_NAME PARTITION_NAME HIGH_VALUE
-------------- ----------------- -------------------------------------------------------------
INTERVALPART1 PART1 TO_DATE(' 2013-02-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIA
INTERVALPART1 SYS_P105 TO_DATE(' 2013-03-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIA
--插⼊当前时间的分区
SQL> insert into cai.intervalpart1 values (1,sysdate);
1 row created.
SQL> commit;
Commit complete.
--⼜有⼀个分区新建了,如果插⼊的时间在两个⽉以后或者更久,则Oracle只⽣成必须的分区,并不会⽣成连续分区。SQL> select table_name, partition_name, HIGH_VALUE
2 from dba_tab_partitions t
3 where table_name = 'INTERVALPART1';
TABLE_NAME PARTITION_NAME HIGH_VALUE
----------------- --------------- --------------------------------------------------------------------------------
INTERVALPART1 PART1 TO_DATE(' 2013-02-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIA
INTERVALPART1 SYS_P105 TO_DATE(' 2013-03-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
INTERVALPART1 SYS_P106 TO_DATE(' 2013-06-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
SQL>
配置多个表空间的分区存储 --会循环不断往这⼏个表空间插⼊(随机插⼊某个表空间)
create tablespace tablespace1 datafile '+DATA' size 10m autoextend on;
create tablespace tablespace2 datafile '+DATA' size 10m autoextend on;
create tablespace tablespace3 datafile '+DATA' size 10m autoextend on;
create tablespace tablespace4 datafile '+DATA' size 10m autoextend on;
create tablespace tablespace5 datafile '+DATA' size 10m autoextend on;
alter table cai.intervalpart1 set STORE IN (tablespace1,tablespace2,tablespace3,tablespace4,tables
pace5);
insert into cai.intervalpart1 values (1,TO_DATE ('04/04/2013', 'MM/DD/YYYY'));
insert into cai.intervalpart1 values (1,TO_DATE ('05/05/2013', 'MM/DD/YYYY'));
insert into cai.intervalpart1 values (1,TO_DATE ('06/06/2013', 'MM/DD/YYYY'));
insert into cai.intervalpart1 values (1,TO_DATE ('07/07/2013', 'MM/DD/YYYY'));
insert into cai.intervalpart1 values (1,TO_DATE ('08/08/2013', 'MM/DD/YYYY'));
insert into cai.intervalpart1 values (1,TO_DATE ('09/08/2013', 'MM/DD/YYYY'));
insert into cai.intervalpart1 values (1,TO_DATE ('10/08/2013', 'MM/DD/YYYY'));
SQL> select table_name, partition_name, HIGH_VALUE,TABLESPACE_NAME
2 from dba_tab_partitions t
3 where table_name = 'INTERVALPART1';
TABLE_NAME PARTITION_NAME HIGH_VALUE TABLESPACE_NAME
--------------- -------------------- ---------------------------------------------------------------------------------------------------- ------------------
INTERVALPART1 SYS_P908 TO_DATE(' 2013-11-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIA TABLESPACE5
INTERVALPART1 SYS_P907 TO_DATE(' 2013-10-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIA TABLESPACE4
INTERVALPART1 SYS_P906 TO_DATE(' 2013-09-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIA TABLESPACE3
INTERVALPART1 SYS_P905 TO_DATE(' 2013-08-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIA TABLESPACE2
INTERVALPART1 SYS_P904 TO_DATE(' 2013-07-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIA TABLESPACE1
INTERVALPART1 SYS_P903 TO_DATE(' 2013-06-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIA TABLESPACE5
INTERVALPART1 SYS_P902 TO_DATE(' 2013-05-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIA TABLESPACE4
INTERVALPART1 SYS_P901 TO_DATE(' 2015-11-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIA USERS
INTERVALPART1 SYS_P881 TO_DATE(' 2013-03-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA USERS
INTERVALPART1 PART1 TO_DATE(' 2013-02-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA USERS
2.数字类型
----创建表
SQL> CREATE TABLE cai.intervalpart2 (c1 NUMBER, c2 DATE)
2 PARTITION BY RANGE (c1)
3 INTERVAL (100)
4 (PARTITION part1
5 VALUES LESS THAN (100)
6 );
Table created.
--查询只有⼀个分区
SQL> select table_name, partition_name, HIGH_VALUE
2 from dba_tab_partitions t
3 where table_name = 'INTERVALPART2';
TABLE_NAME PARTITION_NAME HIGH_VALUE
------------------------------ ------------------------------ -------------------------
INTERVALPART2 PART1 100
SQL>
--增加2⾏记录⼀⾏是1,⼀⾏是101
SQL> insert into cai.intervalpart2 values (1,sysdate);
1 row created.
SQL> insert into cai.intervalpart2 values (101,sysdate);
1 row created.
SQL> commit;
Commit complete.
SQL>
SQL> Select Count(1) From cai.intervalpart2;
COUNT(1)
-
---------
2
--增加了101的分区
SQL> select table_name, partition_name, HIGH_VALUE
2 from dba_tab_partitions t
3 where table_name = 'INTERVALPART2';
TABLE_NAME PARTITION_NAME HIGH_VALUE
------------------------------ ------------------------------ --------------------------
INTERVALPART2 PART1 100
INTERVALPART2 SYS_P107 200
--插⼊1101数值的分区
SQL> insert into cai.intervalpart2 values (1101,sysdate);
1 row created.
SQL> commit;
Commit complete.
--⼜有⼀个分区新建了,如果插⼊的数值是差别很⼤或者更久,则Oracle只⽣成必须的分区,并不会⽣成连续分区。SQL> select table_name, partition_name, HIGH_VALUE
2 from dba_tab_partitions t
3 where table_name = 'INTERVALPART2';
TABLE_NAME PARTITION_NAME HIGH_VALUE
------------------------------ ------------------------------ ---------------------------------------
INTERVALPART2 PART1 100
INTERVALPART2 SYS_P107 200
INTERVALPART2 SYS_P108 1200
管理:
1.⾃动分区与⾃动分区的转换
--创建普通分区表
SQL> CREATE TABLE cai.intervalpart3 (c1 NUMBER, c2 DATE)
2 PARTITION BY RANGE (c1)
3 (PARTITION part1 VALUES LESS THAN (100)
4 );
Table created.
SQL> insert into cai.intervalpart3 values (1,sysdate);
1 row created.
SQL> commit;
Commit complete.
--当插⼊超出值时会报错
SQL> insert into cai.intervalpart3 values (101,sysdate);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论