KingbaseESV8R6全局临时表不能进行reindex操作

发布时间 2023-09-18 13:41:11作者: KINGBASE研究院

背景

我们经常遇到两种情况下会重建索引,reindex

1、索引崩溃,由于软件或硬件问题导致索引内数据失效而不可用。

2、索引膨胀,当索引膨胀会占用过多磁盘空间,reindex可以解决此问题。

对于临时表和全局临时表而言,临时表可以进行reindex操作,而全局临时表不能进行此操作,原因是全局临时表的表定义是全局的,也就是其他会话也可以用到全局临时表定义。

而临时表是仅针对当前session而言进行的,在会话级别有隔离属性。然而进行reindex操作时,对应索引的relfilenode值会发生改变,不能对全局临时表进行reindex,因为这会影响其他正在使用全局临时表的会话。

测试

[复制代码](javascript:void(0)?

创建全局临时表:
 create global temporary table g_temp_t1(id integer);
创建索引:
 create index g_temp_t1_temp on g_temp_t1 (id);
插入数据
 insert into g_temp_t1 values (1),(2);

reindex全局临时表时报错 原因是relfilenode值不允许改变,是固定的。
TEST=#  reindex  index g_temp_t1_temp;
警告:  skipping reindex index "g_temp_t1_temp" on a global temporary table

 TEST=# select relname ,relfilenode from sys_class where relname='g_temp_t1_temp';
    relname     | relfilenode
----------------+-------------
 g_temp_t1_temp |      109477
(1 row)


创建临时表:
create  temporary table l_temp_t1(id integer) ;

创建索引:
create index l_temp_t1_temp on l_temp_t1 (id);

插入数据:
insert into l_temp_t1 values (1),(2);


临时表索引的relfilenode此时的109481,reindex后relfilenode改变为109482,由于临时表在会话退出后不会保存表,所以会话级别隔离的属性看,这个操作不会影响到其他会话。
TEST=# select relname ,relfilenode from sys_class where relname='l_temp_t1_temp';
    relname     | relfilenode
----------------+-------------
 l_temp_t1_temp |      109481


reindex  index l_temp_t1_temp;

TEST=# select relname ,relfilenode from sys_class where relname='l_temp_t1_temp';
    relname     | relfilenode
----------------+-------------
 l_temp_t1_temp |      109482
(1 row)

[复制代码](javascript:void(0)?

总结

在此案例中,我们看到临时表和全局临时表在reindex操作时还是有差别的。更多关于临时表和全局临时表差别请查看文档《KingbaseESV8R6临时表和全局临时表》https://www.cnblogs.com/kingbase/p/16629827.html