SQL> create table yxl01 (a number ,b number)
2 partition by range (b)(
3 partition p1 values less than (10),
4 partition p2 values less than (20),
5 partition p3 values less than (maxvalue));
Table created
SQL> insert into yxl01 values(1,9);
1 row inserted
SQL> insert into yxl01 values(2,19);
1 row inserted
SQL> commit;
Commit complete
SQL> update yxl01 set b=11 where a = 1;
update yxl01 set b=11 where a = 1
ORA-14402: 更新分区关键字列将导致分区的更改
SQL> alter table yxl01 enable row movement;
Table altered
SQL> update yxl01 set b=11 where a = 1;
1 row updated
SQL> commit;
Commit complete
SQL> alter table yxl01 disable row movement;
Table altered
十一、对已经存在的表进行分区操作。
步骤:1、用create table语句新建一个分区表。
2、drop或者rename旧表。
3、把新建的分区表rename成旧表。
SQL> create table yxl01_new
2 partition by range (b)(
3 partition p1 values less than (10),
4 partition p2 values less than (20),
5 partition p3 values less than (maxvalue))
6 nologging
7 as select * from yxl01
8 /
Table created
SQL> drop table yxl01;
Table dropped
SQL> rename yxl01_new to yxl01;
Table renamed
十二、给分区表添加分区。
1、range分区表添加分区。分区表定时
alter table f_regs add partition p2011 values less than (20120101) tablespace p_tbsp;
注:分区表定义时最大的分区表没有采用 less than (maxvalue);如果采用了,需要切分最大的分区表,而不是添加分区操作。
2、给list分区表添加分区。
alter table f_sales add partition reg_east values('GA');
注:分区表定义时没有采用default选项,如果采用了,则需要切分分区表,而不是添加分区表操作。
3、给hash分区表添加分区。
alter table browns add partition new_part tablespace p_tbsp update indexes;
注:没有没有采用update indexes选项,则需要rebuild分区表的索引。
十三、Exchanging a Partition with an Existing Table
SQL> create table f_sales
2 (sales_amt number, d_date_id number)
3 partition by range (d_date_id)
4 (partition p_2007 values less than (20080101),
5 partition p_2008 values less than (20090101),
6 partition p_2009 values less than (20100101)
7 );
Table created
SQL> create bitmap index d_date_id_fk1
2 on f_sales(d_date_id) local;
Index created
SQL> alter table f_sales add partition p_2010 values less than (20110101);
Table altered
SQL> create table workpart(sales_amt number, d_date_id number);
Table created
SQL> insert into workpart values(100,20100101);
1 row inserted
SQL> insert into workpart values(120,20100102);
1 row inserted
SQL> commit;
Commit complete
SQL> create bitmap index d_date_id_fk2 on workpart(d_date_id);
Index created
SQL> alter table f_sales exchange partition p_2010
2 with table workpart including indexes without validation;
Table altered
SQL> select * from f_sales;
SALES_AMT D_DATE_ID
---------- ----------
100 20100101
120 20100102
十四、重命名分区
SQL> alter table f_sales rename partition p_2010 to p_2010_re;
Table altered
十五、切分分区
SQL> alter table f_sales
2 split partition p_2010_re at (20100601) into
3 (partition p_2010_r,partition p_2010_b) update indexes
4 ;
Table altered
SQL> select index_name, partition_name, status from user_ind_partitions;
INDEX_NAME PARTITION_NAME STATUS
------------------------------ ------------------------------ --------
D_DATE_ID_FK1 P_2007 USABLE
D_DATE_ID_FK1 P_2008 USABLE
D_DATE_ID_FK1 P_2009 USABLE
D_DATE_ID_FK1 P_2010_B USABLE
D_DATE_ID_FK1 P_2010_R USABLE
注:如果没有update indexes选项,则需要rebuild index操作。
十六、合并分区。
SQL> alter table f_sales
2 merge partitions p_2010_r,p_2010_b into partition p_2010;
Table altered
SQL> select index_name, partition_name, status from user_ind_partitions;
INDEX_NAME PARTITION_NAME STATUS
------------------------------ ------------------------------ --------
D_DATE_ID_FK1 P_2007 USABLE
D_DATE_ID_FK1 P_2008 USABLE
D_DATE_ID_FK1 P_2009 USABLE
D_DATE_ID_FK1 P_2010 UNUSABLE
SQL> alter index D_DATE_ID_FK1 rebuild partition p_2010;
Index altered
注:当然上面使用时可以加上 update indexes选项。
十六、删除分区
alter table f_sales drop partition p_2008;
alter table f_sales drop subpartition p2_south;
十七、快速删除分区数据。
SQL> alter table f_sales truncate partition p_2009_re;
Table truncated
十八、分区统计信息
SQL> exec dbms_stats.gather_table_stats('HBJB_KF_BDGS','F_SALES','P_2008');
PL/SQL procedure successfully completed
十九、建立索引。
1.Local Index
create index f_regs_idx1 on f_regs(reg_count) local;
或者:
create index f_regs_idx1 on f_regs(reg_count) local
(partition p2009 tablespace idx1
,partition p2010 tablespace idx2);
2.Global Index
reate index f_count_idx1 on f_regs(reg_count)
global partition by hash(reg_count) partitions 3;