RANGE分区 1.1 RANGE分区 CREATE TABLE dao_partitions ( prod_id NUMBER(6), insert_time date ) PARTITION BY RANGE (insert_time) (PARTITION SALES_1998 VALUES LESS THAN (TO_DATE('01-JAN-1999','DD-MON-YYYY')), PARTITION SALES_1999 VALUES LESS THAN (TO_DATE('01-JAN-2000','DD-MON-YYYY')), PARTITION SALES_2000 VALUES LESS THAN (TO_DATE('01-JAN-2001','DD-MON-YYYY')) ); 1 无maxvalue分区下增加分区 SQL> alter table dao_range_partition add PARTITION SALES_2001 VALUES LESS THAN (TO_DATE('01-JAN-2002','DD-MON-YYYY')); Table altered. SQL> select DTP.TABLE_OWNER,DTP.TABLE_OWNER ,DTP.PARTITION_NAME 2 from dba_tab_partitions DTP 3 where table_owner='DAO' 4 AND TABLE_NAME='DAO_RANGE_PARTITION'; TABLE_OWNER TABLE_OWNER PARTITION_NAME ------------ --------------- -------------- DAO DAO SALES_1998 DAO DAO SALES_1999 DAO DAO SALES_2000 DAO DAO SALES_2001 2 增加max分区 SQL> ALTER TABLE DAO_RANGE_PARTITION ADD PARTITION SALES_MAX VALUES LESS THAN ( MAXVALUE ); Table altered. SQL> select DTP.TABLE_OWNER,DTP.TABLE_OWNER ,DTP.PARTITION_NAME 2 from dba_tab_partitions DTP 3 where table_owner='DAO' 4 AND TABLE_NAME='DAO_RANGE_PARTITION'; TABLE_OWNER TABLE_OWNER PARTITION_NAME ------------------------------------------ DAO DAO SALES_1998 DAO DAO SALES_1999 DAO DAO SALES_2000 DAO DAO SALES_2001 DAO DAO SALES_MAX 3 通过拆分maxvalue分区增加分区 SQL> alter table DAO_RANGE_PARTITION split partition SALES_MAX at (TO_DATE(' 2003-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')) into (partition SALES_2002 tablespace users ,partition SALES_MAX tablespace users ); Table altered. 4 通过drop maxvalue分区 加入分区 SQL> alter table DAO_RANGE_PARTITION drop partition SALES_MAX ; Table altered. SQL> select DTP.TABLE_OWNER,DTP.TABLE_OWNER ,DTP.PARTITION_NAME 2 from dba_tab_partitions DTP 3 where table_owner='DAO' 4 AND TABLE_NAME='DAO_RANGE_PARTITION' 5 ; TABLE_OWNER TABLE_OWNER PARTITION_NAME ------------ ------------ --------------- DAO DAO SALES_1998 DAO DAO SALES_1999 DAO DAO SALES_2000 DAO DAO SALES_2001 DAO DAO SALES_2002 SQL> alter table dao_range_partition add PARTITION SALES_2003 VALUES LESS THAN (TO_DATE('01-JAN-2004','DD-MON-YYYY')); Table altered SQL> select DTP.TABLE_OWNER,DTP.TABLE_OWNER ,DTP.PARTITION_NAME 2 from dba_tab_partitions DTP 3 where table_owner='DAO' 4 AND TABLE_NAME='DAO_RANGE_PARTITION'; TABLE_OWNER TABLE_OWNER PARTITION_NAME ----------- ------------ ---------------- DAO DAO SALES_1998 DAO DAO SALES_1999 DAO DAO SALES_2000 DAO DAO SALES_2001 DAO DAO SALES_2002 DAO DAO SALES_2003 SQL> ALTER TABLE DAO_RANGE_PARTITION ADD PARTITION SALES_MAX VALUES LESS THAN ( MAXVALUE ); Table altered. 2 分区删除 SQL> alter table DAO_RANGE_PARTITION drop partition SALES_MAX ; Table altered. SQL> select DTP.TABLE_OWNER,DTP.TABLE_OWNER ,DTP.PARTITION_NAME 2 from dba_tab_partitions DTP 3 where table_owner='DAO' 4 AND TABLE_NAME='DAO_RANGE_PARTITION' 5 ; TABLE_OWNER TABLE_OWNER PARTITION_NAME ------------ ------------ --------------- DAO DAO SALES_1998 DAO DAO SALES_1999 DAO DAO SALES_2000 DAO DAO SALES_2001 DAO DAO SALES_2002 3 分区truncate SQL> alter table DAO_RANGE_PARTITION truncate partition SALES_2002 ; Table truncated. 4 分区拆分 SQL> alter table DAO_RANGE_PARTITION split partition SALES_MAX at (TO_DATE(' 2003-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')) into (partition SALES_2002 tablespace users ,partition SALES_MAX tablespace users ); Table altered. 5 分区合并 SQL> select DTP.TABLE_OWNER, DTP.TABLE_NAME, DTP.PARTITION_NAME 2 from dba_tab_partitions DTP 3 where table_owner = 'DAO' 4 AND TABLE_NAME = 'DAO_RANGE_PARTITION'; TABLE_OWNER TABLE_NAME PARTITION_NAME ----------------------------------------- --------------- DAO DAO_RANGE_PARTITION SALES_1998 DAO DAO_RANGE_PARTITION SALES_1999 DAO DAO_RANGE_PARTITION SALES_2000 DAO DAO_RANGE_PARTITION SALES_2001 DAO DAO_RANGE_PARTITION SALES_2002 DAO DAO_RANGE_PARTITION SALES_2003 DAO DAO_RANGE_PARTITION SALES_MAX 7 rows selected. SQL> ALTER TABLE DAO_RANGE_PARTITION MERGE PARTITIONS SALES_1998,SALES_1999 INTO PARTITION SALES_1999 ; Table altered. SQL> select DTP.TABLE_OWNER,DTP.TABLE_NAME ,DTP.PARTITION_NAME from dba_tab_partitions DTP where table_owner='DAO' AND TABLE_NAME='DAO_RANGE_PARTITION'; TABLE_OWNER TABLE_NAME PARTITION_NAME --------------- ------------------------------ -------------- DAO DAO_RANGE_PARTITION SALES_1999 DAO DAO_RANGE_PARTITION SALES_2000 DAO DAO_RANGE_PARTITION SALES_2001 DAO DAO_RANGE_PARTITION SALES_2002 DAO DAO_RANGE_PARTITION SALES_2003 DAO DAO_RANGE_PARTITION SALES_MAX 6 rows selected. 6 分区交换 SQL> select DTP.TABLE_OWNER,DTP.TABLE_NAME ,DTP.PARTITION_NAME from dba_tab_partitions DTP where table_owner='DAO' AND TABLE_NAME='DAO_RANGE_PARTITION'; TABLE_OWNER TABLE_NAME PARTITION_NAME -------------- --------------------- --------------- DAO DAO_RANGE_PARTITION SALES_1999 DAO DAO_RANGE_PARTITION SALES_2000 DAO DAO_RANGE_PARTITION SALES_2001 DAO DAO_RANGE_PARTITION SALES_2002 DAO DAO_RANGE_PARTITION SALES_2003 DAO DAO_RANGE_PARTITION SALES_MAX 2 HASH分区 SQL> insert into dao_test_hash 2 select level,to_char(level)||to_char(level) 3 from dual 4 connect by level<=100; 100 rows created. SQL> commit ; Commit complete. SQL> select count(*), 'all' 2 from dao_test_hash 3 union all 4 select count(*), 'p1' 5 from dao_test_hash partition (p1) 6 union all 7 select count(*), 'p2' 8 from dao_test_hash partition (p2); COUNT(*) 'AL ---------- --- 100 all 45 p1 55 p2 1 增加分区 SQL> alter table dao_test_hash add partition p3 ; Table altered. SQL> select count(*), 'all' 2 from dao_test_hash 3 union all 4 select count(*), 'p1' 5 from dao_test_hash partition(p1) 6 union all 7 select count(*), 'p2' 8 from dao_test_hash partition(p2) 9 union all 10 select count(*), 'p3' 11 from dao_test_hash partition(p3); COUNT(*) 'AL ---------- --- 100 all 20 p1 55 p2 25 p3 SQL> alter table dao_test_hash add partition p4 ; Table altered. SQL> SQL> select count(*), 'all' 2 from dao_test_hash 3 union all 4 select count(*), 'p1' 5 from dao_test_hash partition(p1) 6 union all 7 select count(*), 'p2' 8 from dao_test_hash partition(p2) 9 union all 10 select count(*), 'p3' 11 from dao_test_hash partition(p3) 12 union all 13 select count(*), 'p4' 14 from dao_test_hash partition(p4); COUNT(*) 'AL ---------- --- 100 all 20 p1 28 p2 25 p3 27 p4 SQL> select dao_xx+1,count(*) 2 from 3 (select ora_hash(level,3,0) dao_xx 4 from dual 5 connect by level <=100 ) 6 group by dao_xx ; DAO_XX+1 COUNT(*) ---------- ---------- 2 28 3 25 4 27 20 Hash 分区数据分布函数使用的是ORA_HASH函数 3 列表分区 基本与范围分区相同,参见范围分区