一、什么是分区表
Oracle提供了分区技术以支持VLDB(Very Large DataBase)。分区表通过对分区列的判断,把分区列不同的记录,放到不同的分区中。分区完全对应用透明。
Oracle的分区表可以包括多个分区, 每个分区都是一个独立的段( SEGMENT ),可以存放到不同的表空间中 。查询时可以通过查询表来访问各个分区中的数据,也可以通过在查询时直接指定分区的方法来进行查询。
二、什么时候用分区表
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.(表中存在历史数据只能读,当前月数据可以修改)
三、分区表优点
(1)由于将数据分散到各个分区中,减少了数据损坏的可能性;
(2)可以对单独的分区进行备份和恢复;
(3)可以将分区映射到不同的物理磁盘上,来分散IO;
(4)提高可管理性、可用性和性能。
四、分区表类型
- 范围分区(range)
就是根据数据库表中某一字段的值的范围来划分分区(多用于时间,数值),例如:
create table graderecord
(
sno varchar2(10),
sname varchar2(20),
dormitory varchar2(3),
grade int
)
partition by range(grade)
(
partition bujige values less than(60), --不及格
partition jige values less than(85), --及格
partition youxiu values less than(maxvalue) --优秀
)
插入数据
insert into graderecord values('511601','魁','229',92);
insert into graderecord values('511602','凯','229',62);
insert into graderecord values('511603','东','229',26);
insert into graderecord values('511604','亮','228',77);
insert into graderecord values('511605','敬','228',47);
insert into graderecord values('511606','峰','228');
insert into graderecord values('511607','明','240',90);
insert into graderecord values('511608','楠','240',100);
insert into graderecord values('511609','涛','240',67);
insert into graderecord values('511610','博','240',75);
insert into graderecord values('511611','铮','240',60);
查询相关数据
select * from graderecord;
select * from graderecord partition(bujige);
select * from graderecord partition(jige);
select * from graderecord partition(youxiu);
说明:数据中有空值,Oracle机制会自动将其规划到maxvalue的分区中。
- 哈希分区(hash):
散列分区是根据字段的hash值进行均匀分布,尽可能的实现各分区所散列的数据相等。
还是刚才那个表,只不过把范围分区改换为散列分区,语法如下(删除表之后重建):
create table graderecord
(
sno varchar2(10),
sname varchar2(20),
dormitory varchar2(3),
grade int
)
partition by hash(sno)
(
partition p1,
partition p2,
partition p3
);
查询
select * from graderecord partition(p1);
select * from graderecord partition(p2);
select * from graderecord partition(p3);
结果
说明:散列分区即为哈希分区,Oracle采用哈希码技术分区,具体分区如何由Oracle说的算,也可能我下一次搜索就不是这个数据了。
- 列表分区(list):
列表分区明确指定了根据某字段的某个具体值进行分区,而不是像范围分区那样根据字段的值范围来划分的。
create table graderecord
(
sno varchar2(10),
sname varchar2(20),
dormitory varchar2(3),
grade int
)
partition by list(dormitory)
(
partition d229 values('229'),
partition d228 values('228'),
partition d240 values('240')
)
查询结果
select * from graderecord partition(d229);
select * from graderecord partition(d228);
select * from graderecord partition(d240);
- 范围-哈希复合分区(range-hash);
首先讲范围-散列分区。先声明一下:列表分区不支持多列,但是范围分区和哈希分区支持多列。
create table graderecord
(
sno varchar2(10),
sname varchar2(20),
dormitory varchar2(3),
grade int
)
partition by range(grade)
subpartition by hash(sno,sname)
(
partition p1 values less than(75)
(
subpartition sp1,subpartition sp2
),
partition p2 values less than(maxvalue)
(
subpartition sp3,subpartition sp4
)
);
select * from graderecord partition(p1);
select * from graderecord partition(p2);
select * from graderecord subpartition(sp1);
select * from graderecord subpartition(sp2);
select * from graderecord subpartition(sp3);
select * from graderecord subpartition(sp4);
说明:当数据量越来越大时,哈希分区的分区表中数据越来越趋于平衡