详解Oracle表分区:范围分区,散列分区,列表分区和复合分区
概述
当生产环境中表的数据量不断增大,查询数据的速度就会变慢,应用程序的性能就会下降,这时我们就应该考虑对表进行分区。表进行分区后,逻辑上表仍然是一张完整的表,只是将表中的数据在物理上存放到多个表空间(物理文件上),这样查询数据时,不至于每次都扫描整张表,其实就是查询的范围少了,IO也分散了。
下面介绍下oracle常见的四种分区。
一、范围分区
根据数据库表中某一字段的值的范围来划分分区,例如:
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(sno,sname,dormitory) 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); commit;
下面查询一下全部数据,然后查询各个分区数据:
select * from graderecord; select * from graderecord partition(bujige); select * from graderecord partition(jige); select * from graderecord partition(youxiu);
全部数据如下:
不及格数据如下:
及格数据如下:
优秀数据如下:
说明:因为数据中有空值,Oracle机制会自动将其规划到maxvalue的分区中。
二、散列分区
散列分区是根据字段的hash值进行均匀分布,尽可能的实现各分区所散列的数据相等。
还是刚才那个表,只不过把范围分区改换为散列分区,语法如下(删除表之后重建):
drop table graderecord purge; create table graderecord ( sno varchar2(10), sname varchar2(20), dormitory varchar2(3), grade int ) partition by hash(sno) ( partition p1, partition p2, partition p3 );
插入测试数据:
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(sno,sname,dormitory) 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); commit;
然后查询分区数据:
select * from graderecord partition(p1); select * from graderecord partition(p2); select * from graderecord partition(p3);
p1分区的数据:
p2分区的数据:
p3分区的数据:
说明:散列分区即为哈希分区,Oracle采用哈希码技术分区,具体分区如何由Oracle说的算,也可能我下一次搜索就不是这个数据了。
三、列表分区
列表分区明确指定了根据某字段的某个具体值进行分区,而不是像范围分区那样根据字段的值范围来划分的。
drop table graderecord purge; 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') )
根据宿舍来进行列表分区,插入与范围分区实验相同的数据
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(sno,sname,dormitory) 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); commit;
查询分区数据:
select * from graderecord partition(d229); select * from graderecord partition(d228); select * from graderecord partition(d240);
d229分区所得数据如下:
d228分区所得数据如下:
d240分区所得数据如下:
四、复合分区 (范围-散列分区,范围-列表分区)
1、范围-散列分区
列表分区不支持多列,但是范围分区和哈希分区支持多列。
drop table graderecord purge; 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 ) );
以grade划分范围,然后以sno和sname划分散列分区,当数据量大的时候散列分区则趋于“平均”。
插入数据:
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(sno,sname,dormitory) 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); insert into graderecord values('511612','狸','244',72); insert into graderecord values('511613','杰','244',88); insert into graderecord values('511614','萎','244',19); insert into graderecord values('511615','猥','244',65); insert into graderecord values('511616','丹','244',59); insert into graderecord values('511617','靳','244',95); commit;
查询如下:
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);
分区p1数据(75分以下)如下:
分区p2数据(75分之上包括75分)如下:
子分区sp1:
子分区sp2:
子分区sp3:
子分区sp4:
说明:当数据量越来越大时,哈希分区的分区表中数据越来越趋于平衡。
2、范围-列表分区
范围-列表分区有两种创建方式,区别在有没模板。
2.1、没有模板的创建方式
create table MobileMessage ( ACCT_MONTH VARCHAR2(6), -- 帐期 格式:年月 YYYYMM AREA_NO VARCHAR2(10), -- 地域号码 DAY_ID VARCHAR2(2), -- 本月中的第几天 格式 DD SUBSCRBID VARCHAR2(20), -- 用户标识 SVCNUM VARCHAR2(30) -- 手机号码 ) partition by range(ACCT_MONTH,AREA_NO) subpartition by list(DAY_ID) ( partition p1 values less than('201805','012') ( subpartition shangxun1 values('01','02','03','04','05','06','07','08','09','10'), subpartition zhongxun1 values('11','12','13','14','15','16','17','18','19','20'), subpartition xiaxun1 values('21','22','23','24','25','26','27','28','29','30','31') ), partition p2 values less than('201809','014') ( subpartition shangxun2 values('01','02','03','04','05','06','07','08','09','10'), subpartition zhongxun2 values('11','12','13','14','15','16','17','18','19','20'), subpartition xiaxun2 values('21','22','23','24','25','26','27','28','29','30','31') ), partition p3 values less than('201901','016') ( subpartition shangxun3 values('01','02','03','04','05','06','07','08','09','10'), subpartition zhongxun3 values('11','12','13','14','15','16','17','18','19','20'), subpartition xiaxun3 values('21','22','23','24','25','26','27','28','29','30','31') ) )
插入测试数据:
insert into MobileMessage values('201801','010','04','ghk001','13800000000'); insert into MobileMessage values('201802','015','12','myx001','13633330000'); insert into MobileMessage values('201803','015','24','hjd001','13300000000'); insert into MobileMessage values('201804','010','04','ghk001','13800000000'); insert into MobileMessage values('201805','010','04','ghk001','13800000000'); insert into MobileMessage values('201805','011','18','sxl001','13222000000'); insert into MobileMessage values('201806','011','21','sxl001','13222000000'); insert into MobileMessage values('201806','012','11','tgg001','13800044400'); insert into MobileMessage values('201807','010','04','ghk001','13800000000'); insert into MobileMessage values('201808','012','24','tgg001','13800044400'); insert into MobileMessage values('201809','014','29','zjj001','13100000000'); insert into MobileMessage values('201810','014','29','zjj001','13100000000'); insert into MobileMessage values('201811','014','29','zjj001','13100000000'); insert into MobileMessage values('201811','013','30','wgc001','13444000000'); insert into MobileMessage values('201812','013','30','wgc001','13444000000'); insert into MobileMessage values('201812','010','30','ghk001','13800000000'); insert into MobileMessage values('201901','015','22','myx001','13633330000'); commit;
查询分区数据如下:
select * from MobileMessage; select * from MobileMessage partition(p1); select * from MobileMessage partition(p2); select * from MobileMessage subpartition(xiaxun2); select * from MobileMessage partition(p3);
分区p1查询结果如下:
分区p2查询结果如下:
子分区xiaxun2查询结果如下:
说明:范围分区range(A,B)的分区法则,范围分区都是 values less than(A,B)的,通常情况下以A为准,如果小于A的不用考虑B,直接插进去,如果等于A那么考虑B,要是满足B的话也插进去。
2.2、范围-列表分区
包含模板的(比较繁琐,但是更加精确,处理海量存储数据十分必要):
create table MobileMessage_model ( ACCT_MONTH VARCHAR2(6), -- 帐期 格式:年月 YYYYMM AREA_NO VARCHAR2(10), -- 地域号码 DAY_ID VARCHAR2(2), -- 本月中的第几天 格式 DD SUBSCRBID VARCHAR2(20), -- 用户标识 SVCNUM VARCHAR2(30) -- 手机号码 ) partition by range(ACCT_MONTH,AREA_NO) subpartition by list(DAY_ID) subpartition template ( subpartition sub1 values('01'),subpartition sub2 values('02'), subpartition sub3 values('03'),subpartition sub4 values('04'), subpartition sub5 values('05'),subpartition sub6 values('06'), subpartition sub7 values('07'),subpartition sub8 values('08'), subpartition sub9 values('09'),subpartition sub10 values('10'), subpartition sub11 values('11'),subpartition sub12 values('12'), subpartition sub13 values('13'),subpartition sub14 values('14'), subpartition sub15 values('15'),subpartition sub16 values('16'), subpartition sub17 values('17'),subpartition sub18 values('18'), subpartition sub19 values('19'),subpartition sub20 values('20'), subpartition sub21 values('21'),subpartition sub22 values('22'), subpartition sub23 values('23'),subpartition sub24 values('24'), subpartition sub25 values('25'),subpartition sub26 values('26'), subpartition sub27 values('27'),subpartition sub28 values('28'), subpartition sub29 values('29'),subpartition sub30 values('30'), subpartition sub31 values('31') ) ( partition p_1801_010 values less than('201801','011'), partition p_1801_011 values less than('201801','012'), partition p_1801_012 values less than('201801','013'), partition p_1801_013 values less than('201801','014'), partition p_1801_014 values less than('201801','015'), partition p_1801_015 values less than('201801','016'), partition p_1802_010 values less than('201802','011'), partition p_1802_011 values less than('201802','012'), partition p_1802_012 values less than('201802','013'), partition p_1802_013 values less than('201802','014'), partition p_1802_014 values less than('201802','015'), partition p_1802_015 values less than('201802','016'), partition p_1803_010 values less than('201803','011'), partition p_1803_011 values less than('201803','012'), partition p_1803_012 values less than('201803','013'), partition p_1803_013 values less than('201803','014'), partition p_1803_014 values less than('201803','015'), partition p_1803_015 values less than('201803','016'), partition p_1804_010 values less than('201804','011'), partition p_1804_011 values less than('201804','012'), partition p_1804_012 values less than('201804','013'), partition p_1804_013 values less than('201804','014'), partition p_1804_014 values less than('201804','015'), partition p_1804_015 values less than('201804','016'), partition p_1805_010 values less than('201805','011'), partition p_1805_011 values less than('201805','012'), partition p_1805_012 values less than('201805','013'), partition p_1805_013 values less than('201805','014'), partition p_1805_014 values less than('201805','015'), partition p_1805_015 values less than('201805','016'), partition p_1806_010 values less than('201806','011'), partition p_1806_011 values less than('201806','012'), partition p_1806_012 values less than('201806','013'), partition p_1806_013 values less than('201806','014'), partition p_1806_014 values less than('201806','015'), partition p_1806_015 values less than('201806','016'), partition p_1807_010 values less than('201807','011'), partition p_1807_011 values less than('201807','012'), partition p_1807_012 values less than('201807','013'), partition p_1807_013 values less than('201807','014'), partition p_1807_014 values less than('201807','015'), partition p_1807_015 values less than('201807','016'), partition p_1808_010 values less than('201808','011'), partition p_1808_011 values less than('201808','012'), partition p_1808_012 values less than('201808','013'), partition p_1808_013 values less than('201808','014'), partition p_1808_014 values less than('201808','015'), partition p_1808_015 values less than('201808','016'), partition p_1809_010 values less than('201809','011'), partition p_1809_011 values less than('201809','012'), partition p_1809_012 values less than('201809','013'), partition p_1809_013 values less than('201809','014'), partition p_1809_014 values less than('201809','015'), partition p_1809_015 values less than('201809','016'), partition p_1810_010 values less than('201810','011'), partition p_1810_011 values less than('201810','012'), partition p_1810_012 values less than('201810','013'), partition p_1810_013 values less than('201810','014'), partition p_1810_014 values less than('201810','015'), partition p_1810_015 values less than('201810','016'), partition p_1811_010 values less than('201811','011'), partition p_1811_011 values less than('201811','012'), partition p_1811_012 values less than('201811','013'), partition p_1811_013 values less than('201811','014'), partition p_1811_014 values less than('201811','015'), partition p_1811_015 values less than('201811','016'), partition p_1812_010 values less than('201812','011'), partition p_1812_011 values less than('201812','012'), partition p_1812_012 values less than('201812','013'), partition p_1812_013 values less than('201812','014'), partition p_1812_014 values less than('201812','015'), partition p_1812_015 values less than('201812','016'), partition p_1901_010 values less than('201901','011'), partition p_1901_011 values less than('201901','012'), partition p_1901_012 values less than('201901','013'), partition p_1901_013 values less than('201901','014'), partition p_1901_014 values less than('201901','015'), partition p_1901_015 values less than('201901','016'), partition p_other values less than(maxvalue, maxvalue) );
这个是带有模板子分区的,模板子分区详细到月中的天。这种分区模式只要建立了分区就会自动创建子分区的。
插入上面不带模板分区实验相同的数据
insert into MobileMessage_model values('201801','010','04','ghk001','13800000000'); insert into MobileMessage_model values('201802','015','12','myx001','13633330000'); insert into MobileMessage_model values('201803','015','24','hjd001','13300000000'); insert into MobileMessage_model values('201804','010','04','ghk001','13800000000'); insert into MobileMessage_model values('201805','010','04','ghk001','13800000000'); insert into MobileMessage_model values('201805','011','18','sxl001','13222000000'); insert into MobileMessage_model values('201806','011','21','sxl001','13222000000'); insert into MobileMessage_model values('201806','012','11','tgg001','13800044400'); insert into MobileMessage_model values('201807','010','04','ghk001','13800000000'); insert into MobileMessage_model values('201808','012','24','tgg001','13800044400'); insert into MobileMessage_model values('201809','014','29','zjj001','13100000000'); insert into MobileMessage_model values('201810','014','29','zjj001','13100000000'); insert into MobileMessage_model values('201811','014','29','zjj001','13100000000'); insert into MobileMessage_model values('201811','013','30','wgc001','13444000000'); insert into MobileMessage_model values('201812','013','30','wgc001','13444000000'); insert into MobileMessage_model values('201812','010','30','ghk001','13800000000'); insert into MobileMessage_model values('201901','015','22','myx001','13633330000'); commit;
查询分区p_1801_010的数据:
select * from MobileMessage_model partition(p_1801_010);
查询子分区p_1801_010_sub4的数据:
select * from MobileMessage_model subpartition(p_1801_010_sub4);
查询分区p_1806_011的数据:
select * from MobileMessage_model partition(p_1806_011);
查询子分区p_1806_011_sub21的数据:
select * from MobileMessage_model subpartition(p87006_011_sub21);
篇幅有限,关于分区方面的一些测试实验就介绍到这了,后面抽空在介绍分区的一些特点,什么时候应该选什么分区,以及分区建立后效率是不是有提升,感兴趣的朋友可以关注下!