一、表分区的法
1、范围分区法
根据表中列值的范围进行分区,如按照年度或季度等,当数据在范围内均匀分布时,性能最好,否则
考虑其他分区方法。
2、散列(Hash)分区法
Hash实际是一种函数算法,当向表插入数据时,系统会自动根据当前分区列的值计算出Hash值之后确
定应将该行存放于哪个表空间中。
3、列表分区法
如果表中某个列的值是可枚举的,如销售表可按照地区进行分区。
4、复合分区法
复合分区分为:复合范围-列表分区 和 复合范围-Hash分区 两种。复合范围-列表分区先对列采用范
围分区,然后在子分区使用列表分区。复合范围-Hash分区先对列采用范围分区,然后在子分区使用散列分区。
查看分区表
select * from dba_part_tables;
select * from dba_tab_partitions;
select * from sale partition(Y2008);
二、建立分区表
1、表的范围(Range)分区
A、建立范围分区表
create table sale
(sale_date date,
sale_area varchar2(10),
sale_amount number(10,2)
)
partition by range(sale_date)
(partition Y2005 values less than (to_date('20060101','YYYYMMDD')) tablespace tabs1,
partition Y2006 values less than (to_date('20070101','YYYYMMDD')) tablespace tabs2,
partition Y2007 values less than (to_date('20080101','YYYYMMDD')) tablespace tabs3,
partition Y2008 values less than (to_date('20090101','YYYYMMDD')) tablespace tabs4);
B、增加分区
alter table sale
add partition Y2009 values less than (to_date('20100101','YYYYMMDD')) tablespace tabs5;
alter table sale
add partition maxvalue values less than (maxvalue) tablespace tbs1;
C、分拆分区
alter table sale split partition Y2008
at (to_date('20080701','YYYYMMDD')) into (partition Y20081,partition Y20082);
D、删除分区
alter table sale drop partition Y2005;
E、合并分区
alter table sale
merge partition Y2005,Y2006 into partition Y2006 tablespace tab1;
F、查询
select * from sale partition(Y2005);
select segment_name,partition_name,tablespace_name
from user_segments where segment_name='SALE';
2、表的散列(Hash)分区
A、建立散列(Hash)分区表
create table identity_card
(card_id varchar2(18),
name varchar2(20),
nation varchar2(30),
birthday date,
sex varchar2(2) check (sex='男' or sex='女'),
nation_place varchar2(10),
homeplace varchar2(50)
)
partition by hash(card_id)
(partition p1 tablespace tabs1,
partition p2 tablespace tabs2,
partition p3 tablespace tabs3,
partition p4 tablespace tabs4);
B、增加分区
alter table identity_card add partition p5 tablespace tabs5;
C、删除分区
alter table temp_article coalesce partition;
alter table temp_article modify partition p1 coalesce subpartition;
3、表的列表(list)分区
A、建立列表分区表
create table sale_amount
(sale_date date,
sale_area varchar2(10),
sale_amount number(10,2)
)
partition by list (sale_area)
(partition p1 values('北京','天津') tablespace tabs1;
partition p1 values('上海','南京') tablespace tabs1;
partition p1 values('广州','深圳') tablespace tabs1);
B、增加分区
alter table sale_amount add partition p4
values ('武汉','长沙') tablespace tabs4;
C、增加值
alter table sale_amount
modify partition p1 add values('大连');
alter table sale_amount
modify partition p1 drop values('大连');
D、删除分区
alter table sale_amount drop partition p1;
4、表的复合分区
A、建立范围/散列
create table temp_article
(art_no number(6),
descr varchar2(50),
price number(7,2)
)
partition by range(art_no)
subpartition by hash(descr)
subpartitions 4 store in (tabs1,tabs2,tabs3,tabs4)
(partition p1 values less than (300000),
partition p2 values less than (600000),
partition p3 values less than (maxvalue));
select * from temp_article partition(p1);
select * from temp_article subpartition(sys_subp41);
增加分区
alter table temp_article add partition p4
values less than (450000);
alter table temp_article modify partition p1 add subpartition tablespace tabs5;
alter table temp_article modify partition p2 add subpartition tablespace tabs5;
alter table temp_article modify partition p3 add subpartition tablespace tabs5;
删除分区
alter table temp_article coalesce partition;
alter table temp_article modify p1 coalesce subpartition;
B、建立范围/列表分区
create table temp_sale
(sale_date date,
sale_area varchar2(10),
sale_amount number(10,2)
)
partition by range (sale_date)
subpartition by list (sale_area)
(partition p1 values less than (to_date('20080101','YYYYMMDD')) tablespace tabs1
(subpartition p11 values ('北京','天津'),
subpartition p12 values ('上海','南京'),
subpartition p13 values ('广州','深圳')),
partition p2 values less than (to_date('20080101','YYYYMMDD')) tablespace tabs2
(subpartition p11 values ('北京','天津'),
subpartition p12 values ('上海','南京'),
subpartition p13 values ('广州','深圳'));
select * from temp_sale partition(p1);
select * from temp_sale subpartition(p11);
增加分区
alter table temp_sale add partition p3
values less than (to_date('20100101','YYYYMMDD')) tablespace tabs3
(subpartition p11 values ('北京','天津'),
subpartition p12 values ('上海','南京'),
subpartition p13 values ('广州','深圳')
);
alter table temp_sale modify partition p1
add subpartition p14 values('西安','兰州');
alter table temp_sale modify partition p2
add subpartition p24 values('西安','兰州');
增加值、删除值
alter table temp_sale
modify subpartition p11 add values ('大连')
alter table temp_sale
modify subpartition p11 drop values ('大连')
删除分区
alter table temp_sale drop partition p1;
alter table temp_sale drop subpartition p14;
5、相关操作
交换分区数据(将分区表sale的分区p1的数据交换到表sale_1)
alter table sale exchange partition p1 with table sale_1;
截断分区
alter table sale truncate partition p1;
修改分区名
alter table sale rename partition p3 to p5;
重组分区
alter table sale move partition p_12 tablespace tabs1;
三、分区索引
1、应用性能参考
1)、若表的分区键(partitioning column)是索引键(index key)的子集,应使用本地索引,否则参考2
2)、若索引为唯一索引(unique),应使用全局索引,否则参考3
3)、若用户对可管理性的要求更高,应使用本地索引,否则参考4
4)、若为OLTP,对响应时间要求较高,应使用全局索引,
若为OLAP或DSS,对数据吞吐量要求较高,应使用本地索引
2、本地分区索引
就是在所有每个区上单独创建索引,它能自动维护,在drop或truncate某个分区时不影响该索引的其
他分区索引的使用,也就是索引不会失效,维护起来比较方便,但是在查询性能稍微有点影响。
create index index_sale on sale(sale_date,sale_area) local
(partition index_Y2005 tablespace tabs1,
partition index_Y2006 tablespace tabs2,
partition index_Y2007 tablespace tabs3,
partition index_Y2008 tablespace tabs4,
partition index_Y2009 tablespace tabs5
);
create index index_sale on sale(sale_date,sale_area) local;
alter index ... rebuild partition Y2006; --重建分区索引
3、全局索引
就是在全表上创建索引,它可以创建自己的分区,可以和分区表的分区不一样,也就是它是独立的索
引。在drop或truncate某个分区时需要创建索引alter index idx_xx rebuild,也可以alter table
table_name drop partition partition_name update global indexes;实现,但是要花很长时间在重建索引上
。可以通过查询user_indexes、user_part_indexes和user_ind_partitions视图来查看索引是否有效。
a、全局不分区索引:分区表的不分区全局索引就是一般的普通索引
create [unique] index index_name on table(column);
create [unique] index index_name on table(column) global;
alter index ... rebuild; --重建全局索引
b、范围分区索引
SQL > create [unique] index index_name on table(column)
global partition by range(column)
(
partition part_idx_01 value less than(first range value) tablespace tabs1,
partition part_idx_02 value less than(second range value) tablespace tabs2,
partition part_idx_03 value less than(maxvalue) tablespace tabs3
)
c、散列分区索引
SQL > create [unique] index index_name on table(column,[column2])
global partition by hash(column,[column2])
(
partition part_idx_01 tablespace tabs1,
partition part_idx_02 tablespace tabs2,
partition part_idx_03 tablespace tabs3
)
4、相关操作
DBA查看索引分区
select * from dba_ind_partitions
USER查看索引分区
select * from user_ind_partitions
DBA查看索引分区类型
select * from dba_part_indexes
USER查看索引分区类型
select * from user_part_indexes
alter index T_INX unusable --取消索引
alter index T_INX rebuild --索引恢复正常
ENABLE和DISABLE只针对函数索引
alter index ind_t_p rebuild partition p1;
alter index ind_t_p rebuild partition p2;
Oracle最大允许存在多少个分区呢?
我们可以从Oracle的Concepts手册上找到这个信息,对于Oracle9iR2:
Tables can be partitioned into up to 64,000 separate partitions.
对于Oracle10gR2,Oracle增强了分区特性:
Tables can be partitioned into up to 1024K-1 separate partitions.
关于何时应该进行分区,Oracle有如下建议:
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.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/17012874/viewspace-693290/ ,如需转载,请注明出处,否则将追究法律责任。