KingbaseES sys_restore 恢复表时默认不包括表上的索引

前言

最近碰到一个案例,在使用sys_restore恢复指定表时,默认不恢复表上的索引,如果想恢复需要单独指定。

测试过程

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

查看表的有关属性:test=# \d+ t
                                     Table "public.t"
 Column |  Type   | Collation | Nullable | Default | Storage | Stats target | Description
--------+---------+-----------+----------+---------+---------+--------------+-------------
 x      | integer |           |          |         | plain   |              |
Indexes:
    "t_inx" btree (x)
Check constraints:
    "check_x" CHECK (x < 10000000)
Access method: heap

备份指定t表:

[kingbase2@localhost V8]$ sys_dump -U system -d test -Fc -t t > t.dump

删除t表及其依赖:

test=# drop table t cascade;
DROP TABLE

恢复t表:

[kingbase2@localhost V8]$ sys_restore -U system -Fc -d test -t t t.dump

看结果check约束恢复回来,但是索引没有恢复回来。这里需要重建索引,或者在sys_restore中指定索引:

test=# \d+ t
Table "public.t"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+---------+-----------+----------+---------+---------+--------------+-------------
x | integer | | | | plain | |
Check constraints:
"check_x" CHECK (x < 10000000)
Access method: heap

恢复指定索引:

[kingbase2@localhost V8]$ sys_restore -U system -Fc -d test -I t_inx t.dump

我们看到索引已经恢复回来了:

test=# \d+ t
Table "public.t"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+---------+-----------+----------+---------+---------+--------------+-------------
x | integer | | | | plain | |
Indexes:
"t_inx" btree (x)
Check constraints:
"check_x" CHECK (x < 10000000)
Access method: heap

总结

使用sys_restore恢复表时,如果不在命令中指定索引,默认不包括索引,需要通过-I单独指定索引导入。当然也可以导入表以后在数据库内重建索引,不过这种方式建议不要在业务期间执行,因为即使使用INDEX CONCURRENTLY不阻塞DML语句,如果索引占用空间很大,可能也要花费大量时间建索引。

posted @ 2023-03-02 15:49  KINGBASE研究院  阅读(74)  评论(0编辑  收藏  举报