Oracle分区笔记

Oracle数据表分区笔记

When to Partition a Table什么时候需要分区表,官网的2个建议如下:

(1)Tables greater than 2GB should always be considered for partitioning.

(2)Tables containing historical data, in which new data is added into the newest partition. A typical example is a historical table where only the current month’s data is updatable and the other 11 months are read only.

在oracle 10g中最多支持:1024k-1个分区:

Tables can be partitioned into up to 1024K-1 separate partitions


分区表和分区索引对于10g,基本上可以分为:

Range(范围)分区
Hash(散列)分区
List(列表)分区
以及组合分区:Range-Hash,Range-List

11g在组合分区功能这块有所增强,又推出了range-range,list-range,list-list,list-hash, 这就相当于除hash外三种分区方式的笛卡尔形式都有了


对于索引,需要区分创建的是全局索引,或本地索引:

  1. 全局索引(global index):即可以分区,也可以不分区。即可以建range分区,也可以建hash分区,即可建于分区表,又可创建于非分区表上,就是说,全局索引是完全独立的,因此它也需要我们更多的维护操作。

  2. 本地索引(local index):其分区形式与表的分区完全相同,依赖列相同,存储属性也相同。对于本地索引,其索引分区的维护自动进行,就是说你add/drop/split/truncate表的分区时,本地索引会自动维护其索引分区。


创建分区

Range分区

create table tb1 (id number,name varchar2(50))

    partition by range(id)(

    partition tb1_part_1 values less than (10) tablespace tbspart01,

    partition tb1_part_2 values less than (20) tablespace tbspart02,

    partition tb1_part_3 values less than (30) tablespace tbspart03,

    partition tb1_part_max values less than (maxvalue) tablespace tbspart04

    );

Hash分区

create table test(

transaction_id number primary key,

item_id number(8) not null

)

partition by hash(transaction_id)(

partition part_01 tablespace tablespace01,

partition part_02 tablespace tablespace02,

partition part_03 tablespace tablespace03

);

List分区

create table custaddr(

id varchar2(15 byte) not null,

areacode varchar2(4 byte)
)

partition by list (areacode)( 
partition t_list025 values ('025'), 
partition t_list372 values ('372') , 
partition t_list510 values ('510'),
partition p_other values (default)
)

组合分区

例子:

range-hash

create table test

(

transaction_id number primary key,

transaction_date date

)

partition by range(transaction_date) subpartition by hash(transaction_id)

subpartitions 3 store in (tablespace01,tablespace02,tablespace03)

(

partition part_01 values less than(to_date(’2009-01-01’,’yyyy-mm-dd’)),

partition part_02 values less than(to_date(’2010-01-01’,’yyyy-mm-dd’)),

partition part_03 values less than(maxvalue)

);

其他组合类似。

分区索引相关:

创建本地索引(local):
create index index_name on table_name(carrier,orig,dest) local;

创建全局索引(global):
create index index_name on table_name(carrier,orig,dest) global;

删除索引:

drop index index_name;

参考资料1 参考资料2