oracle 执行 truncate hash 分区表时,表上的本地索引、全局索引、全局分区索引均可用,无需添加 update global indexes

os: centos 7.4
db: oracle 11.2.0.4

版本

# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)


# su - oracle
Last login: Tue Oct  8 16:35:50 CST 2019 on pts/0
$ sqlplus / as sysdba;

SQL> set lines 200;
set pages 200;
select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE	11.2.0.4.0	Production
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production

分区表、本地索引、全局索引、全局分区索引

create table tmp_t0(
  c0 varchar2(100),
  c1 varchar2(100),
  c2 varchar2(100)
)
partition by hash (c0) 
partitions 4;

--本地索引
create index idx_tmp_t0_c0 on tmp_t0(c0) local;

--全局索引
create index idx_tmp_t0_c1 on tmp_t0(c1);

--全局分区索引
create index idx_tmp_t0_c2 on tmp_t0(c2) global
partition by hash (c2)
partitions 4
;


insert into tmp_t0
select level as c0,
       level as c1,
       level as c2
  from dual
  connect by level < 10000
;

commit;

select di.owner,di.index_name,di.index_type,di.table_owner,di.table_name,di.table_type,di.tablespace_name,
       di.status,--指示非分区索引是有效(VALID)的还是不可用(UNUSABLE)的。
       di.partitioned,di.global_stats,di.segment_created
from dba_indexes di
where 1=1
and di.index_name in (
'IDX_TMP_T0_C0',
'IDX_TMP_T0_C1',
'IDX_TMP_T0_C2'
)
;

在这里插入图片描述

select dip.index_owner,dip.index_name,dip.partition_name,dip.partition_position,
       dip.status,--指示索引分区是否可用(USABLE)不可用(UNUSABLE)
       dip.tablespace_name,dip.segment_created
from dba_ind_partitions dip
where 1=1
and dip.index_name in (
'IDX_TMP_T0_C0',
'IDX_TMP_T0_C1',
'IDX_TMP_T0_C2'
)
;

oracle 执行 truncate hash 分区表时,表上的本地索引、全局索引、全局分区索引均可用,无需添加 update global indexes_第1张图片

select dpi.owner,dpi.index_name,dpi.table_name,dpi.partitioning_type,dpi.subpartitioning_type,dpi.partition_count,
       dpi.locality,-- 指示是本地索引 (LOCAL) 还是全局索引 (GLOBAL)
       dpi.alignment
from dba_part_indexes dpi
where 1=1
and dpi.index_name in (
'IDX_TMP_T0_C0',
'IDX_TMP_T0_C1',
'IDX_TMP_T0_C2'
);

在这里插入图片描述

truncate 表之后查看索引状态

truncate table tmp_t0
;

insert into tmp_t0
select level as c0,
       level as c1,
       level as c2
  from dual
  connect by level < 10000
;

commit;

select di.owner,di.index_name,di.index_type,di.table_owner,di.table_name,di.table_type,di.tablespace_name,
       di.status,--指示非分区索引是有效(VALID)的还是不可用(UNUSABLE)的。
       di.partitioned,di.global_stats,di.segment_created
from dba_indexes di
where 1=1
and di.index_name in (
'IDX_TMP_T0_C0',
'IDX_TMP_T0_C1',
'IDX_TMP_T0_C2'
)
;

在这里插入图片描述

select dip.index_owner,dip.index_name,dip.partition_name,dip.partition_position,
       dip.status,--指示索引分区是否可用(USABLE)不可用(UNUSABLE)
       dip.tablespace_name,dip.segment_created
from dba_ind_partitions dip
where 1=1
and dip.index_name in (
'IDX_TMP_T0_C0',
'IDX_TMP_T0_C1',
'IDX_TMP_T0_C2'
)
;

oracle 执行 truncate hash 分区表时,表上的本地索引、全局索引、全局分区索引均可用,无需添加 update global indexes_第2张图片

select dpi.owner,dpi.index_name,dpi.table_name,dpi.partitioning_type,dpi.subpartitioning_type,dpi.partition_count,
       dpi.locality,-- 指示是本地索引 (LOCAL) 还是全局索引 (GLOBAL)
       dpi.alignment
from dba_part_indexes dpi
where 1=1
and dpi.index_name in (
'IDX_TMP_T0_C0',
'IDX_TMP_T0_C1',
'IDX_TMP_T0_C2'
);

在这里插入图片描述

此时查看执行计划对各索引的使用情况
oracle 执行 truncate hash 分区表时,表上的本地索引、全局索引、全局分区索引均可用,无需添加 update global indexes_第3张图片
oracle 执行 truncate hash 分区表时,表上的本地索引、全局索引、全局分区索引均可用,无需添加 update global indexes_第4张图片
oracle 执行 truncate hash 分区表时,表上的本地索引、全局索引、全局分区索引均可用,无需添加 update global indexes_第5张图片

truncate 分区时需要添加 update global indexes

select *
from dba_tab_partitions
where table_name='TMP_T0'
;

alter table tmp_t0 truncate partition SYS_P117 update global indexes
;

如果 truncate 分区不使用 update global indexes,则 local 类型的索引为 USABLE,全局非分区索引为 UNUSABLE,全局分区索引为 UNUSABLE。

所以,结论如下:
truncate 整个表时,所有的索引都不会失效。
truncate 某个分区时,全局索引都会失效,需要添加 update global indexes。

你可能感兴趣的:(#,oracle,table,index)