Should I partition/subpartition my table?

不羁岁月 提交于 2019-12-05 11:38:14

You can try in this way: use INTERVAL to tell oracle to create automatic partitions. You must define column (number or date) and an interval (in my example 1 month). Oracle will put in the same partition all row in the same interval (in this case in the same month). If the partition doesn't exist will be created.

create table log(
       id_dispositive    number,
       date    date,
       status  number,
       type    number
)
partition by range (date)                              
interval (numtoyminterval(1,'MONTH'))(
   partition p0701 values less than (to_date('2007-02-01','yyyy-mm-dd'))
);

The same can be done also with type column. More information: http://www.oracle.com/technetwork/articles/sql/11g-partitioning-084209.html.

Every partition can be subpartitioned using TEMPLATE key.

create table log(
       id_dispositive    number,
       date    date,
       status  number,
       type    number
)
partition by range (date) interval (numtoyminterval(1,'MONTH'))
subpartition by list (type)  
subpartition TEMPLATE (
    SUBPARTITION types1 VALUES (1, 2) TABLESPACE tbs_1,
    SUBPARTITION types2 VALUES (3, 4) TABLESPACE tbs_1
)                          
(
   partition p0701 values less than (to_date('2007-02-01','yyyy-mm-dd'))
);

In this case you can't create an automatic subpartition, if a new type will be add you have to run an alter table statment. Here more informations; https://docs.oracle.com/cd/B28359_01/server.111/b32024/part_admin.htm#i1006655.

In your example:

create table prova_log(
       id_dispositive    number,
       type       number,
       date_verification    date,
       status  number
)
partition by range (date_verification) interval (numtoyminterval(1,'MONTH'))
subpartition by list (type)  
subpartition TEMPLATE (
    SUBPARTITION type1 VALUES (1),
    SUBPARTITION type2 VALUES (2),
    SUBPARTITION type3 VALUES (3),
    SUBPARTITION type4 VALUES (4)
)                          
(
   partition p0816 values less than (to_date('01/09/2016','dd/mm/yyyy'))
);

If you try to insert:

insert into prova_log values (1,1,TO_DATE('10/10/2016','dd/mm/yyyy'),1);

you will see 2 partition on your table.

Now I've tested it!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!