日期:2014-05-16 浏览次数:20741 次
oracle分区表管理2009-08-30 12:29作用:将大表的数据分布到多个表分区段,不同分区彼此独立,从而提高了表的可用性和性能
种类:范围分区,散列分区(使用HASH算法),列表分区,范围/散列组合分区,范围/列表组合分区
1、范围分区表
创建范围分区表
1. create table t(v number,b number) 
2. partition by range(v) ( 
3. partition p1 values less than ('11') tablespace test1, 
4. partition p2 values less than ('21') tablespace test2); 
增加与删除分区
1. alter table t add partition p3 values less than ('31') tablespace test3; 
2. alter table t drop partition p3 
一个时间分区的例子
1. alter session set nls_data_lanage=AMERICAN; 
2. alter session set nls_data_format='DD-MON-YYYY' 
3. create table t(v_date date,b number) 
4. partition by range(v_date)( 
5. partition p1 values less than ('01-APR-2009') tablespace test1, 
6. partition p2 values less than ('01-JUN-2009') tablespace test2); 
2、散列分区表
创建
1. create table t1( 
2. v number,b number) 
3. partition by hash(v) 
4. (partition p1 tablespace test1, 
5. partition p2 tablespace test2); 
增加分区
1. alter table t add partition p3 tablespace test3; 
删除分区
1. alter table t drop coalesce partition; 
3、列表分区表
建列表分区
1. create table t( 
2. v varchar2(10), 
3. b number 
4. )partition by list(v) 
5. (partition p1 values('a','b') tablespace test1, 
6. partition p2 values('c','d') tablespace test2); 
#插入数据
1. SQL> insert into t values('a',10); 
2. SQL> insert into t values('d',20); 
#注意,插入数据时第一个字段只能为a,b,c,d 
1. SQL> insert into t values('f',30); 
2. ERROR at line 1: 
3. ORA-14400: inserted partition key does not map to any partition 
增加分区
1. alter table t add partition p3 values('31','32') tablespace test3; 
删除分区
1. alter table t drop partition p3 
4-1、范围/散列组合分区
建立散列组合分区
1. create table t( 
2. v number,b number) 
3. partition by range(v) 
4. subpartition by hash(b) subpartitions 2 
5. store in (test1,test2)( 
6. partition p1 values less than ('11'), 
7. partition p2 values less than ('21')); 
查询
1. select * from t; 
2. select * from t partition(p1); 
3. select * from t where .... 
增加主分区和子分区
1. alter table t add partition p3 values less than ('31') tablespace test3; 
2. alter table t modify partition p3 add subpartition; 
删除分区
1. alter table t coalesce partition; 
2. alter table t modify partition p1 coalesce subpartition; 
4-2、范围/列表组合分区
创建
1. create table t( 
2. v number,b number) 
3. partition by range(v) 
4. subpartition by list(b) 
5. (partition p1 values less than ('11') tablespace test1( 
6. subpartition p1_1 values('1','3'), 
7. subpartition p1_2 values('5','6') 
8. ), 
9. partition p2 values less than ('21') tablespace test2( 
10. subpartition p2_1 values('13','14'), 
11. subpartition p2_2 values('15','16') 
12. )); 
查询
1. select * from t 
2. select * from t partition(p1) 
3. select * from t subpartition(p1_1) 
4. select * from t where ..... 
5. select segment_name,partition_name,tablespace_name 
6. from user_segments where segment_name='T'; 
增加分区和子分区
1. alter table t add partition p3 values less than ('31') tablespace test3( 
2. subpartition p3_1 values('25','26'), 
3. subpartition p3_2 values('22','23')); 
4. alter table t modify partition r3 
5. add subpartition r3_3 tablespace test3 values('28','29'); 
删除分区
1. alter table t modify partition p1 coalesce subpartition; 
5、分区表的查询
1. select * from t; 
2. select * from t partition(p1); 
3. select * from t partition(p2); 
4. select * from t where v=XXX 
6、其它设置:
1. 交换分区数据 
2. alter table t exchange partition p1 with table tt; 
3. 载断分区 
4. alter table t truncate partition p1; 
5. 修改分区名 
6. alter table t rename partition p2_1 to p2; 
7. 合并分区 
8. alter table t merge partitions p1,p2 into partition p01 
9. 重组分区 
10. alter table t move partition p1 tablespace test04 
11. 为列表分区和子分区加值 
12. alter table t modify partition p