为了简化数据库大表的管理,例如在数据仓库中一般都是TB级的数量级.ORACLE8以后推出了分区选项.分区将表分离在若于不同的表空间上,用分而治之的方法来支撑元限膨胀的大表,组大表在物理一级的可管理性.将大表分割成较小的分区可以改善表的维护、备份、恢复、事务及查询性能。分区的优点:1、 增强可用性:如果表的一个分区由于系统故障而不能使用,表的其余好的分区仍可以使用;2、 减少关闭时间:如果系统故障只影响表的一部份分区,那么只有这部份分区需要修复,矿能比整个大表修复花的时间更少;3、 维护轻松:如果需要得建表,独产管理每个公区比管理单个大表要轻松得多;4、 均衡I/O:可以把表的不同分区分配到不同的磁盘来平衡I/O改善性能;5、 改善性能:对大表的查询、增加、修改等操作可以分解到表的不同分区来并行执行,可使运行速度更快,在数据仓库的TP查询特别有用。6、 分区对用户透明,最终用户感觉不到分区的存在。create tablespace dw1datafile 'D:/oracle/oradata/ora9/dw11.ora' size 50Mcreate tablespace dw2datafile 'D:/oracle/oradata/ora9/dw21.ora' size 50M一、按范围分区:固名思义就是按一定range来分区,看下面的例子:SQL> set linesize 1000SQL> create table niegc_part2 (3 part_id integer primary key,4 part_date date,5 part_dec varchar2(100)6 )7 partition by range(part_date)8 (9 partition part_01 values less than(to_date('2006-01-01','yyyy-mm-dd')) tablespace dw1,10 partition part_02 values less than(to_date('2007-01-01','yyyy-mm-dd')) tablespace dw2,11 partition part_03 values less than(maxvalue) tablespace dw112 );表已创建。SQL>SQL> insert into niegc_part values(1,to_date('2005-12-30','yyyy-mm-dd'),'less 2006-01-01');已创建 1 行。SQL> commit;提交完成。SQL> insert into niegc_part values(2,to_date('2006-01-01','yyyy-mm-dd'),'equal 2007-01-01');已创建 1 行。SQL> commit;提交完成。SQL> insert into niegc_part values(3,sysdate,'sysdate');已创建 1 行。SQL> commit;提交完成。SQL>SQL>SQL> select * from niegc_part partition(part_01); PART_ID PART_DATE PART_DEC---------- ---------- ---------------------------------------------------------------------------------------------------- 1 30-12月-05 less 2006-01-01SQL>相信只要对oracle 有点熟,都能知道上面的range分区的意思了.两个字段以上的range分区大同小异,请看下面的例子:create table niegc_part(part_id integer primary key,part_date date,part_dec varchar2(100))partition by range(part_id,part_date)(partition part_01 values less than(1,to_date('2006-01-01','yyyy-mm-dd')) tablespace dw,partition part_02 values less than(10,to_date('2007-01-01','yyyy-mm-dd')) tablespace dw,partition part_03 values less than(maxvalue,maxvalue) tablespace dw);二、Hash分区(散列分区)。 散列分区通过指定分区编号来均匀分布数据的一种分区类型,因为通过在I/O设备上进行散列分区,使行这些分区大小一致。如将part_id的数据根据自身的情况散列地存放在指定的三个表空间中:create table niegc_part(part_id integer primary key,part_date date,part_dec varchar2(100))partition by hash(part_id)(partition part_01 tablespace dw1,partition part_02 tablespace dw2);系统将按part_id将记录散列地插入三个分区中,这里也就是二个不同的表空间中。三、复合分区。根据范围分区后,每个分区内的数据再散列地分布在几个表空间中,这样我们就要使用复合分区。复合分区是先使用范围分区,然后在每个分区同再使用散列分区的一种分区方法,如将part_date的记录按时间分区,然后每个分区中的数据分三个子分区,将数据散列地存储在三个指定的表空间中:create table niegc_part(part_id integer primary key,part_date date,part_dec varchar2(100))partition by range(part_date) subpartition by hash(part_id)subpartitions 2 store in(dw1,dw2)(partition part_01 values less than(to_date('2006-01-01','yyyy-mm-dd')) tablespace dw1,partition part_02 values less than(to_date('2007-01-01','yyyy-mm-dd')) tablespace dw2,partition part_03 values less than(maxvalue) tablespace dw1);先根据part_date进行范围分区,然后根据交易的ID将记录散列地存储在二个表空间中。四、索引分区:注意: 对某个字段已做了分区了,是不允许再建立索引分区的。这一点要非常注意。全局索引建立时global子句允许指定索引的范围值,这个范围值为索引字段的范围值:create index idx_part_id on niegc_part(part_dec)global partition by range(part_dec)(partition idx_1 values less than('1000') tablespace dw,partition idx_2 values less than(maxvalue) tablespace dw)局部索引分区的建立:(注意:表必须存在分区,此分区的个数必须和分区表的分区个数一样,不然是建立不起来的)create index idx_part_id on niegc_part(part_dec)local(partition idx_1 tablespace dw1,partition idx_2 tablespace dw2)五、分区维护:(只对范围分区)(1)、增加一个分区:分区范围只能往上增,不能增加一个少于原有的分区:alter table niegc_part add partition part_03 values less than(maxvalue)(2)、合并分区:(合并后的分区必须指下最后一个大value的分区)alter table niegc_part merge partitions part_02,part_03 into partition part_03(3)、删除一个分区:alter table niegc_part drop partition part_01六、总结:需要说明的是,本文在举例说名分区表事务操作的时候,都指定了分区,因为指定了分区,系统在执行的时候则只操作该分区的记录,提高了数据处理的速度。不要指定分区直接操作数据也是可以的。在分区表上建索引及多索引的使用和非分区表一样。此外,因为在维护分区的时候可能对分区的索引会产生一定的影响,可能需要在维护之后重建索引,相关内容请google分区表索引部分的文档Oracle分区命令集-- Create table(创建分区表)create table BILL_MONTHFEE_ZERO(SERV_ID NUMBER(20) not null,BILLING_CYCLE_MONTH NUMBER(6) not null,DATE_TYPE NUMBER(1),ACC_NBR VARCHAR2(80))partition by range (BILLING_CYCLE_MONTH)(partition p_200407 values less than (200407) tablespace TS_ZIKEN storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0), partition p_200408 values less than (200408) tablespace TS_ZIKEN storage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0)) ;create index idx_bill_monthfee_zero_idx01 on bill_monthfee_zero(billing_cycle_month)tablespace TS_ZIKEN_idxstorage(initial 100k next 100k minextents 1 maxextents unlimited pctincrease 0) nologging;grant all on bill_monthfee_zero to dxsq_dev;--增加分区表alter table BILL_MONTHFEE_ZERO add Partition p_200409values less than (200409) tablespace ts_ziken;--删除一分区alter table part_tbl drop Partition part_tbl_08;--将一个分区分为两个分区alter table bill_monthfee_zero split Partition p_200409 at (200409)into (Partition p_200409_1 tablespace ts_ziken,Partition p_200409_2 tablespace ts_ziken_idx);--合并分区ALTERTABLE bill_monthfee_zero MERGE PARTITIONS p_200408, p_200409 INTOPARTITION p_all--将分区改名altertable bill_monthfee_zero rename Partition p_200408 to p_fee_200408--将分区改表空间altertable bill_monthfee_zero move Partition p_200409tablespace ts_ziken_01 nologging--查询特定分区select count(*) from BILL_MONTHFEE_ZERO partition (p_200407);--添加数据insert into bill_monthfee_zero select * from bill_monthfee_zero partition (p_200407)--分区表的导出userid=dxsq/teledoone@jndxsq154buffer=102400tables=bill_monthfee:P_200401,file=E:"exp_para"exp_dxsq_tables.dmplog=E:"exp_para"exp_dxsq_tables.log技巧:删除表中一个字段:alter table bill_monthfee_zero set unused column date_type;添加一个字段:alter table bill_monthfee_zero add date_type number(1);显示分区表信息显示当前用户可访问的所有分区表信息:ALL_PART_TABLES显示当前用户所有分区表的信息:USER_PART_TABLES显示表分区信息 显示数据库所有分区表的详细分区信息:DBA_TAB_PARTITIONS显示当前用户可访问的所有分区表的详细分区信息:ALL_TAB_PARTITIONS显示当前用户所有分区表的详细分区信息:USER_TAB_PARTITIONS显示子分区信息 显示数据库所有组合分区表的子分区信息:DBA_TAB_SUBPARTITIONS显示当前用户可访问的所有组合分区表的子分区信息:ALL_TAB_SUBPARTITIONS显示当前用户所有组合分区表的子分区信息:USER_TAB_SUBPARTITIONS显示分区列 显示数据库所有分区表的分区列信息:DBA_PART_KEY_COLUMNS显示当前用户可访问的所有分区表的分区列信息:ALL_PART_KEY_COLUMNS显示当前用户所有分区表的分区列信息:USER_PART_KEY_COLUMNS显示子分区列 显示数据库所有分区表的子分区列信息:DBA_SUBPART_KEY_COLUMNS显示当前用户可访问的所有分区表的子分区列信息:ALL_SUBPART_KEY_COLUMNS显示当前用户所有分区表的子分区列信息:USER_SUBPART_KEY_COLUMNS