首先,回顾一下Oracle的表空间创建和表创建。
--表空间创建 create tablespace user_temp tempfile 'D:\oracle\oradata\Oracle9i\user_temp1.dbf'size 50m, 'D:\oracle\oradata\Oracle9i\user_temp1.dbf'size 50m --指出表空间包含什么文件,默认大小 autoextend on --是否自动扩展 next 50m maxsize 20480m --每次扩大多少,扩张到最大是多少 extent management local ; --创建表 create table X_SMALL_AREA ( SMALL_AREA_ID NUMBER(10) not null ) tablespace TBSL_SDDQ --表段X_SMALL_AREA放在表空间TBSL_SDDQ中 pctfree 10 --指定一个百分比 比如说10% 那么当某个数据块使用率超过百分之80的时候系统就会停止往这个数据块里 --插入新的数据 剩下10% 空间留给将来对数据的更新使用 这样可以防止迁移行和链接行的出现 initrans 1 --初始化事务槽的个数 maxtrans 255 --指定最多有多少个事务可以并发操作此数据块 storage --存储参数 ( initial 64k --区段(extent)一次扩展64k minextents 1 --最小区段数 maxextents unlimited --最大区段无限制 );
表空间和表建好了,就要正式开始我们的分区攻略了。
范围分区:
drop table DEPT_RNG; CREATE TABLE DEPT_RNG (DEPTNO NUMBER(2), DEPTNAME VARCHAR2(30)) PARTITION BY RANGE(DEPTNO) (PARTITION D1 VALUES LESS THAN (10) tablespace ts1, PARTITION D2 VALUES LESS THAN (20), PARTITION D3 VALUES LESS THAN (30) tablespace ts3); select segment_name,partition_name,tablespace_name from dba_segments where owner='TEST' and segment_name='DEPT_RNG'; select table_name,partition_name,high_value from user_tab_partitions where table_name='DEPT_RNG'; insert into DEPT_RNG values(1,'Fin'); insert into DEPT_RNG values(10,'Tech'); insert into DEPT_RNG values(25,'HR'); commit; select * from DEPT_RNG; select * from DEPT_RNG partition (d1); select * from DEPT_RNG partition (d2); select * from DEPT_RNG partition (d3); --如果插入的数据在规定分区以外就会出现 ORA-1440 insert into DEPT_RNG values(null,'Office'); --OR insert into DEPT_RNG values(40,'Office'); ERROR at line 1: ORA-14400: inserted partition key does not map to any partition --这个时候就要求添加一个分区: alter table DEPT_RNG add partition Dmax values less than (maxvalue); --为了更好第解决这种分区问题,Oracle 11g推出了Interval Partition(区间分区) CREATE TABLE DEPT_NEW2 (DEPTNO NUMBER(2), DEPT_NAME VARCHAR2(30)) PARTITION BY RANGE(DEPTNO) INTERVAL(10) (PARTITION D1 VALUES LESS THAN (10), PARTITION D2 VALUES LESS THAN (20), PARTITION D3 VALUES LESS THAN (30)) ; 这时候,再插入 insert into DEPT_RNG values(40,'Office'); 通过查询: select segment_name, partition_name from dba_segments where segment_name = 'DEPT_NEW2' 就会由dbms自动创建一个SYS_开头的分区,根据INTERVAL(10)的设置,RANGE每增加,新增一个分区。 根据日期的范围分区使用以下: interval (numtoyminterval (1,’month’)) --每一个月份增加一个分区 interval (numtoyminterval (1,’year’)) --每一年增加一个分区 这样创建的表分区会默认分配在SYSTEM表空间。 可以使用store in (test,PERFSTAT)放在interval语句后面,如: INTERVAL(10) store in (test,PERFSTAT) --则会均匀地放在预设的表空间里面 ( PARTITION D1 VALUES LESS THAN (10), PARTITION D2 VALUES LESS THAN (20), PARTITION D3 VALUES LESS THAN (30) )
Hash 分区:
drop table product; CREATE TABLE product (id NUMBER(5), name VARCHAR2(30)) PARTITION BY HASH(id) PARTITIONS 16 STORE IN (ts1,ts2,ts3,ts4); insert into product values(10,0); insert into product values(11,1); insert into product values(22,2); insert into product values(33,3); insert into product values(44,4); insert into product values(55,5); insert into product values(66,6); insert into product values(77,7); insert into product values(88,8); insert into product values(99,9); insert into product values(10,10); insert into product values(11,11); commit; select segment_name,partition_name,tablespace_name from dba_segments where owner='TEST' and segment_name='PRODUCT'; select * from product partition(SYS_P67); select * from product partition(SYS_P68); select * from product partition(SYS_P66);
列分区:
CREATE TABLE DEPT_LIST ( DeptNo NUMBER(2), Dname VARCHAR2(14), Loc VARCHAR2(13)) PARTITION BY LIST (Loc) (PARTITION D_East VALUES ('Shanghai', 'Nanjing'), PARTITION D_West VALUES ('Chengdu', 'Chongqing'), PARTITION D_South VALUES ('Guangzhou', 'Fuzhou'), PARTITION D_North VALUES ('Beijing', 'Shenyang') ); insert into DEPT_LIST values (1,'GZ','Guangzhou'); insert into DEPT_LIST values (1,'GZ','GuangZhou'); insert into DEPT_LIST values (2,'BJ','Beijing'); select * from DEPT_LIST; select * from DEPT_LIST partition(D_South);