KingbaseES 全局索引是否因为DDL操作而变为Unusable ?

2022-11-06,,,,

前言

Oracle 在对分区做DDL操作时,会使分区全局索引失效,需要加上关键字update global indexes。KingbaseES 同样支持全局索引。那么,如果对分区表进行DDL操作,那全局索引是否会失效了?

测试验证

1、创建测试数据

create table t1_part(id integer,name text,status char(1))
partition by list(status)
(
partition p_0 values ('0'),
partition p_1 values ('1'),
partition p_default values (default)
); insert into t1_part select generate_series(1,100000),repeat('a',500),'0';
insert into t1_part select generate_series(100001,200000),repeat('a',500),'1';
insert into t1_part select generate_series(200001,300000),repeat('a',500),'2';
create unique index ind_t1_part_2 on t1_part(id) global;
analyze t1_part; set enable_globalindexscan = on;

  

2、验证SQL 执行计划

分区truncate 前的执行计划:

test=# explain select * from t1_part where id=123 and status='0';
QUERY PLAN
--------------------------------------------------------------------------------------
Global Index Scan using ind_t1_part_2 on t1_part (cost=0.38..4.40 rows=1 width=510)
Index Cond: (id = 123)
Filter: (status = '0'::bpchar)

分区truncate 后的执行计划:

test=# alter table t1_part truncate partition p_1;
ALTER TABLE test=# explain analyze select * from t1_part where id=123 and status='1';
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------
Global Index Scan using ind_t1_part_2 on t1_part (cost=0.38..4.40 rows=1 width=510) (actual time=0.096..0.097 rows=0 loops=1)
Index Cond: (id = 123)
Filter: (status = '1'::bpchar)
Rows Removed by Filter: 1
Planning Time: 0.214 ms
Execution Time: 0.128 ms
(6 rows) test=# explain analyze select * from t1_part where id=123 and status='0';
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------
Global Index Scan using ind_t1_part_2 on t1_part (cost=0.38..4.40 rows=1 width=510) (actual time=0.026..0.027 rows=1 loops=1)
Index Cond: (id = 123)
Filter: (status = '0'::bpchar)
Planning Time: 0.378 ms
Execution Time: 0.067 ms
(5 rows)

  

测试结论

truncate partition 不会导致全局索引失效。

KingbaseES 对于delete操作,只是在 tuple 上做了个标记,而索引不会进行操作。在通过索引访问tuple时,如果发现数据已经被 Deleted ,也不会报错。因此,对于truncate ,实际就相当于记录被delete。可以看到,在truncate 之后,索引的占用空间没有发生变动,而在 vacuum full ,索引尺寸小了很多。

KingbaseES 全局索引是否因为DDL操作而变为Unusable ?的相关教程结束。

《KingbaseES 全局索引是否因为DDL操作而变为Unusable ?.doc》

下载本文的Word格式文档,以方便收藏与打印。