Oracle对分区表日常维护操作子句update [global] indexes无法生效的情况
Oracle对分区表日常维护操作子句update [global] indexes无法生效的情况
其他如add,move,truncate等等之类的维护操作没有做过测试,有时间在补上。
针对drop partition操作来说,由于流水表数据只需保留几个月的数据,对不需要的数据做删除分区操作。
由于Oracle不存在一次性删除多个分区的语法,只能删除一个分区的同时使用update global indexes子句以同时维护索引,这样就增加了维护索引的成本。
因为有针对该表的停机维护时间,想着先删除掉分区数据,在最后一个分区删除的时候添加update global indexes,结果其实不行。
在index状态为unusable的情况下,update global indexes子句无法生效,最后还是需要rebuild。
脚本:
drop table ZKM.FILE_MS purge; create table ZKM.FILE_MS ( ID NUMBER NOT NULL primary key, SFILE_NAME VARCHAR2(200) , RFILE_NAME VARCHAR2(200) , RSTATUS VARCHAR2(2) , RINFO VARCHAR2(2000) , MID VARCHAR2(100) , CREATE_TIME DATE ) PARTITION BY RANGE (CREATE_TIME) INTERVAL (numtoyminterval(1, 'month')) ( partition part_t01 values less than(to_date('2019-08', 'yyyy-mm'))); create index ZKM.idx_create_time on ZKM.FILE_MS(create_time) local online; create index ZKM.idx_SFILE_NAME on ZKM.FILE_MS(SFILE_NAME) online; insert into zkm.file_ms values (1,'spfile','rpfile','y','info','mid',sysdate); insert into zkm.file_ms values (2,'spfile','rpfile','y','info','mid',to_date('2019-07-01','yyyy-mm-dd')); insert into zkm.file_ms values (3,'spfile','rpfile','y','info','mid',to_date('2019-08-01','yyyy-mm-dd')); insert into zkm.file_ms values (4,'spfile','rpfile','y','info','mid',to_date('2020-07-01','yyyy-mm-dd')); commit;
如下,先构造表以及数据:
14:16:04 SYS@testdb(485)> create table ZKM.FILE_MS 14:16:12 2 ( 14:16:12 3 ID NUMBER NOT NULL primary key, 14:16:12 4 SFILE_NAME VARCHAR2(200) , 14:16:12 5 RFILE_NAME VARCHAR2(200) , 14:16:12 6 RSTATUS VARCHAR2(2) , 14:16:12 7 RINFO VARCHAR2(2000) , 14:16:12 8 MID VARCHAR2(100) , 14:16:12 9 CREATE_TIME DATE 14:16:12 10 ) 14:16:12 11 PARTITION BY RANGE (CREATE_TIME) INTERVAL (numtoyminterval(1, 'month')) 14:16:12 12 ( partition part_t01 values less than(to_date('2019-08', 'yyyy-mm'))); Table created. Elapsed: 00:00:00.02 14:16:13 SYS@testdb(485)> create index ZKM.idx_create_time on ZKM.FILE_MS(create_time) local online; Index created. Elapsed: 00:00:00.01 14:16:19 SYS@testdb(485)> create index ZKM.idx_SFILE_NAME on ZKM.FILE_MS(SFILE_NAME) online; Index created. Elapsed: 00:00:00.00 14:16:29 SYS@testdb(485)> select index_name,wm_concat(column_name) column_names,status from (select distinct a.INDEX_NAME,a.COLUMN_NAME,b.status,a.COLUMN_POSITION from dba_ind_columns a,dba_indexes b where a.TABLE_OWNER=upper('ZKM') and a.TABLE_NAME=upper('FILE_MS') and a.index_name=b.index_name order by 1,4) t group by index_name,status; INDEX_NAME COLUMN_NAMES STATUS ------------------------------ -------------------------------------------------- ------------------------ SYS_C007175 ID VALID IDX_SFILE_NAME SFILE_NAME VALID IDX_CREATE_TIME CREATE_TIME N/A Elapsed: 00:00:00.01 14:16:55 SYS@testdb(485)> insert into zkm.file_ms values (1,'spfile','rpfile','y','info','mid',sysdate); 1 row created. Elapsed: 00:00:00.02 14:18:07 SYS@testdb(485)> insert into zkm.file_ms values (2,'spfile','rpfile','y','info','mid',to_date('2019-07-01','yyyy-mm-dd')); 1 row created. Elapsed: 00:00:00.01 14:18:10 SYS@testdb(485)> insert into zkm.file_ms values (3,'spfile','rpfile','y','info','mid',to_date('2019-08-01','yyyy-mm-dd')); 1 row created. Elapsed: 00:00:00.01 14:18:12 SYS@testdb(485)> insert into zkm.file_ms values (4,'spfile','rpfile','y','info','mid',to_date('2020-07-01','yyyy-mm-dd')); 1 row created. Elapsed: 00:00:00.02 14:18:14 SYS@testdb(485)> commit; Commit complete. Elapsed: 00:00:00.00 14:18:17 SYS@testdb(485)> select partition_name from dba_tab_partitions where TABLE_OWNER='ZKM' and table_name='FILE_MS'; PARTITION_NAME ------------------------------------------------------------------------------------------ PART_T01 SYS_P53 SYS_P54 SYS_P55 Elapsed: 00:00:00.00
接下来删除第一个分区,不带update global indexes子句,删除第二个分区则带上。
14:18:22 SYS@testdb(485)> alter table zkm.file_ms drop partition(SYS_P53); Table altered. Elapsed: 00:00:00.02 14:18:43 SYS@testdb(485)> select index_name,wm_concat(column_name) column_names,status from (select distinct a.INDEX_NAME,a.COLUMN_NAME,b.status,a.COLUMN_POSITION from dba_ind_columns a,dba_indexes b where a.TABLE_OWNER=upper('ZKM') and a.TABLE_NAME=upper('FILE_MS') and a.index_name=b.index_name order by 1,4) t group by index_name,status; INDEX_NAME COLUMN_NAMES STATUS ------------------------------ -------------------------------------------------- ------------------------ SYS_C007175 ID UNUSABLE IDX_SFILE_NAME SFILE_NAME UNUSABLE IDX_CREATE_TIME CREATE_TIME N/A Elapsed: 00:00:00.01 14:18:50 SYS@testdb(485)> alter table zkm.file_ms drop partition(SYS_P54); Table altered. Elapsed: 00:00:00.02 14:18:58 SYS@testdb(485)> select index_name,wm_concat(column_name) column_names,status from (select distinct a.INDEX_NAME,a.COLUMN_NAME,b.status,a.COLUMN_POSITION from dba_ind_columns a,dba_indexes b where a.TABLE_OWNER=upper('ZKM') and a.TABLE_NAME=upper('FILE_MS') and a.index_name=b.index_name order by 1,4) t group by index_name,status; INDEX_NAME COLUMN_NAMES STATUS ------------------------------ -------------------------------------------------- ------------------------ SYS_C007175 ID UNUSABLE IDX_SFILE_NAME SFILE_NAME UNUSABLE IDX_CREATE_TIME CREATE_TIME N/A Elapsed: 00:00:00.02
可以看出,删除第二个分区即便用上update global indexes也无法将索引重置为invalid状态。