/** 什么时候使用分区表: 1、表的大小超过2GB。 2、表中包含历史数据,新的数据被增加到新的分区中。 (3).表分区的优缺点 表分区有以下优点: 1、改善查询性能:对分区对象的查询可以仅搜索自己关心的分区,提高检索速度。 2、增强可用性:如果表的某个分区出现故障,表在其他分区的数据仍然可用; 3、维护方便:如果表的某个分区出现故障,需要修复数据,只修复该分区即可; 4、均衡I/O:可以把不同的分区映射到不同磁盘以平衡I/O,改善整个系统性能。 缺点: 分区表相关:已经存在的表没有方法可以直接转化为分区表。不过 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 hege values less than(70), partition lianghao values less than(80), partition youxiu values less than(maxvalue)--当值为null的时候,oracle将会将这条记录划到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); select * from graderecord order by grade desc; select * from graderecord partition(bujige); select * from graderecord partition(hege); select * from graderecord partition(lianghao); select * from graderecord partition(youxiu); --散列分区 --散列分区是根据字段的hash值进行均匀分布,尽可能的实现各分区所散列的数据相等 --drop table graderecord; create table graderecord ( sno varchar2(10), sname varchar2(20), dormitory varchar2(3), grade int ) partition by hash(sno)( partition p1, partition p2 ) 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 order by grade desc; select * from graderecord partition(p1); select * from graderecord partition(p2); --列表分区 --根据某个字段某个值来分区 drop table graderecord; create table graderecord ( sno varchar2(10), sname varchar2(20), dormitory varchar2(3), grade int ) partition by list(dormitory)( partition p229 values('229'), partition p228 values('228'), partition p240 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 order by grade desc; select * from graderecord partition(p229); select * from graderecord partition(p228); select * from graderecord partition(p240); --复合分区 --列表分区不支持多列,范围分区和散列分区支持多列 --范围-散列分区 drop table graderecord; 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 s1,subpartition s2 ), partition p2 values less than(maxvalue)( subpartition s3,subpartition s4 ) ); 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); select * from graderecord partition(p1); select * from graderecord partition(p2); select * from graderecord subpartition(s1); select * from graderecord subpartition(s2); select * from graderecord subpartition(s3); select * from graderecord subpartition(s4); --范围-列表分区 --范围分区 range(A,B)的分区法则,范围分区都是 values less than(A,B)的,通常情况下以A为准 --如果小于A的不用考虑B,直接插进去,如果等于A那么考虑B,要是满足B的话也插进去 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('200705','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('200709','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('200801','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('200701','010','04','ghk001','13800000000'); insert into MobileMessage values('200702','015','12','myx001','13633330000'); insert into MobileMessage values('200703','015','24','hjd001','13300000000'); insert into MobileMessage values('200704','010','04','ghk001','13800000000'); insert into MobileMessage values('200705','010','04','ghk001','13800000000'); insert into MobileMessage values('200705','011','18','sxl001','13222000000'); insert into MobileMessage values('200706','011','21','sxl001','13222000000'); insert into MobileMessage values('200706','012','11','tgg001','13800044400'); insert into MobileMessage values('200707','010','04','ghk001','13800000000'); insert into MobileMessage values('200708','012','24','tgg001','13800044400'); insert into MobileMessage values('200709','014','29','zjj001','13100000000'); insert into MobileMessage values('200710','014','29','zjj001','13100000000'); insert into MobileMessage values('200711','014','29','zjj001','13100000000'); insert into MobileMessage values('200711','013','30','wgc001','13444000000'); insert into MobileMessage values('200712','013','30','wgc001','13444000000'); insert into MobileMessage values('200712','010','30','ghk001','13800000000'); insert into MobileMessage values('200801','015','22','myx001','13633330000'); select * from MobileMessage partition(p1); select * from MobileMessage partition(p2); select * from MobileMessage partition(p3); select * from MobileMessage partition(shangxun1); select * from MobileMessage partition(zhongxun1); select * from MobileMessage partition(xiaxun1); --带模板的范围-列表分区 drop table MobileMessage; 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) 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_0701_010 values less than('200701','011'), partition p_0701_011 values less than('200701','012'), partition p_0701_012 values less than('200701','013'), partition p_0701_013 values less than('200701','014'), partition p_0701_014 values less than('200701','015'), partition p_0701_015 values less than('200701','016'), partition p_0702_010 values less than('200702','011'), partition p_0702_011 values less than('200702','012'), partition p_0702_012 values less than('200702','013'), partition p_0702_013 values less than('200702','014'), partition p_0702_014 values less than('200702','015'), partition p_0702_015 values less than('200702','016'), partition p_0703_010 values less than('200703','011'), partition p_0703_011 values less than('200703','012'), partition p_0703_012 values less than('200703','013'), partition p_0703_013 values less than('200703','014'), partition p_0703_014 values less than('200703','015'), partition p_0703_015 values less than('200703','016'), partition p_0704_010 values less than('200704','011'), partition p_0704_011 values less than('200704','012'), partition p_0704_012 values less than('200704','013'), partition p_0704_013 values less than('200704','014'), partition p_0704_014 values less than('200704','015'), partition p_0704_015 values less than('200704','016'), partition p_0705_010 values less than('200705','011'), partition p_0705_011 values less than('200705','012'), partition p_0705_012 values less than('200705','013'), partition p_0705_013 values less than('200705','014'), partition p_0705_014 values less than('200705','015'), partition p_0705_015 values less than('200705','016'), partition p_0706_010 values less than('200706','011'), partition p_0706_011 values less than('200706','012'), partition p_0706_012 values less than('200706','013'), partition p_0706_013 values less than('200706','014'), partition p_0706_014 values less than('200706','015'), partition p_0706_015 values less than('200706','016'), partition p_0707_010 values less than('200707','011'), partition p_0707_011 values less than('200707','012'), partition p_0707_012 values less than('200707','013'), partition p_0707_013 values less than('200707','014'), partition p_0707_014 values less than('200707','015'), partition p_0707_015 values less than('200707','016'), partition p_0708_010 values less than('200708','011'), partition p_0708_011 values less than('200708','012'), partition p_0708_012 values less than('200708','013'), partition p_0708_013 values less than('200708','014'), partition p_0708_014 values less than('200708','015'), partition p_0708_015 values less than('200708','016'), partition p_0709_010 values less than('200709','011'), partition p_0709_011 values less than('200709','012'), partition p_0709_012 values less than('200709','013'), partition p_0709_013 values less than('200709','014'), partition p_0709_014 values less than('200709','015'), partition p_0709_015 values less than('200709','016'), partition p_0710_010 values less than('200710','011'), partition p_0710_011 values less than('200710','012'), partition p_0710_012 values less than('200710','013'), partition p_0710_013 values less than('200710','014'), partition p_0710_014 values less than('200710','015'), partition p_0710_015 values less than('200710','016'), partition p_0711_010 values less than('200711','011'), partition p_0711_011 values less than('200711','012'), partition p_0711_012 values less than('200711','013'), partition p_0711_013 values less than('200711','014'), partition p_0711_014 values less than('200711','015'), partition p_0711_015 values less than('200711','016'), partition p_0712_010 values less than('200712','011'), partition p_0712_011 values less than('200712','012'), partition p_0712_012 values less than('200712','013'), partition p_0712_013 values less than('200712','014'), partition p_0712_014 values less than('200712','015'), partition p_0712_015 values less than('200712','016'), partition p_0801_010 values less than('200801','011'), partition p_0801_011 values less than('200801','012'), partition p_0801_012 values less than('200801','013'), partition p_0801_013 values less than('200801','014'), partition p_0801_014 values less than('200801','015'), partition p_0801_015 values less than('200801','016'), partition p_other values less than(maxvalue, maxvalue) ); insert into MobileMessage values('200701','010','04','ghk001','13800000000'); insert into MobileMessage values('200702','015','12','myx001','13633330000'); insert into MobileMessage values('200703','015','24','hjd001','13300000000'); insert into MobileMessage values('200704','010','04','ghk001','13800000000'); insert into MobileMessage values('200705','010','04','ghk001','13800000000'); insert into MobileMessage values('200705','011','18','sxl001','13222000000'); insert into MobileMessage values('200706','011','21','sxl001','13222000000'); insert into MobileMessage values('200706','012','11','tgg001','13800044400'); insert into MobileMessage values('200707','010','04','ghk001','13800000000'); insert into MobileMessage values('200708','012','24','tgg001','13800044400'); insert into MobileMessage values('200709','014','29','zjj001','13100000000'); insert into MobileMessage values('200710','014','29','zjj001','13100000000'); insert into MobileMessage values('200711','014','29','zjj001','13100000000'); insert into MobileMessage values('200711','013','30','wgc001','13444000000'); insert into MobileMessage values('200712','013','30','wgc001','13444000000'); insert into MobileMessage values('200712','010','30','ghk001','13800000000'); insert into MobileMessage values('200801','015','22','myx001','13633330000'); select * from MobileMessage partition(p_0701_010); select * from MobileMessage subpartition(p_0701_010_sub1); --分区的其他操作 alter table graderecord split partition jige at(75) into(partition keyi,partition lianghao);--分裂分区 alter table graderecord merge partitions keyi,lianghao into partition jige;--合并分区 alter table graderecord add partition p4;--添加分区 alter table table_name drop partition partition_name;--删除分区 alter table table_name truncate partition partition_name;--清空分区