首先建立分区表测试数据:
create table test_partition_iptvbill partition by range(starttime) (
partition p_01 values less than(date '2008-02-02') tablespace dvboss,
partition p_02 values less than(date '2008-02-03') tablespace dvboss,
partition p_03 values less than(date '2008-02-04') tablespace dvboss,
partition p_04 values less than(date '2008-02-05') tablespace dvboss,
partition p_05 values less than(date '2008-02-06') tablespace dvboss,
partition p_06 values less than(date '2008-02-07') tablespace dvboss,
partition p_07 values less than(date '2008-02-08') tablespace dvboss)
storage(initial 10m next 10m minextents 1 maxextents unlimited pctincrease 0)
nologging as
select * from ow_iptv_bill ib
where ib.starttime < date '2008-02-08'
and ib.starttime >= date '2008-02-01';
创建索引:
--starttime本地索引
create index idx_testpi_starttime on test_partition_iptvbill(starttime) tablespace indx nologging local;
--subscriberid全局索引
create index idx_testpi_subscriberid on test_partition_iptvbill(subscriberid) tablespace indx nologging;
测试内容分以下几个部分:
日常对于分区的维护需要明确分区的操作对于索引的影响
1、drop一个空分区,所谓空分区就是分区已经建立,但是尚未有对应数据的情况
--增加一个空分区
alter table test_partition_iptvbill add partition p_08 values less than(date '2008-02-09') tablespace dvboss;
--将该分区drop
alter table test_partition_iptvbill drop partition p_08;
--检查对于索引的影响
select ind.index_name, ind.status from user_indexes ind where ind.table_name = upper('test_partition_iptvbill');
可以发现索引状态处于usable,即可用状态。
2、重建索引
只能重建全局索引:
alter index IDX_TESTPI_SUBSCRIBERID rebuild;
本地索引不能整体重建:
SQL> alter index IDX_TESTPI_STARTTIME rebuild;
alter index IDX_TESTPI_STARTTIME rebuild
ORA-14086: 不可以将区索引作为整体重建
要重建,只能根据user/dba_ind_partitions里面指定的分区名进行单个重建:
SQL> select index_name, partition_name from user_ind_partitions ip where ip.index_name = 'IDX_TESTPI_STARTTIME';
INDEX_NAME PARTITION_NAME
------------------------------ ------------------------------
IDX_TESTPI_STARTTIME P_01
IDX_TESTPI_STARTTIME P_02
IDX_TESTPI_STARTTIME P_03
IDX_TESTPI_STARTTIME P_04
IDX_TESTPI_STARTTIME P_05
IDX_TESTPI_STARTTIME P_06
6 rows selected
SQL> alter index IDX_TESTPI_STARTTIME rebuild partition P_01;
Index altered
--检查索引状态已经变为有效状态:
SQL> select index_name, status from user_indexes ind where ind.index_name = 'IDX_TESTPI_SUBSCRIBERID';
INDEX_NAME STATUS
------------------------------ --------
IDX_TESTPI_SUBSCRIBERID VALID
3、drop/truncate非空分区将使全局索引的状态变成不可用状态。这个已经在很早的Blog中提过不再做实验。
对于本地索引只是将对应的索引分区删除掉。不会影响整体索引状态。
select * from user_part_indexes pi where pi.index_name = 'IDX_TESTPI_STARTTIME'
select * from user_ind_partitions ip where ip.index_name = 'IDX_TESTPI_STARTTIME'
select * from user_tab_partitions tp where tp.table_name = upper('test_partition_iptvbill')
select * from user_segments seg where seg.segment_name = upper('test_partition_iptvbill')