KingbaseES 全局索引是否因为DDL操作而变为Unusable ?
前言
Oracle 在对分区做DDL操作时,会使分区全局索引失效,需要加上关键字update global indexes。KingbaseES 同样支持全局索引。那么,如果对分区表进行DDL操作,那全局索引是否会失效了?
测试验证
1、创建测试数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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 前的执行计划:
1 2 3 4 5 6 | 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 后的执行计划:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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 ,索引尺寸小了很多。
KINGBASE研究院
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!