☰
金仓数据库KingbaseES sys_restore 恢复表时默认不包括表上的索引
2026/11/3 19:30:46 网站建设 项目流程

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

测试过程
查看表的有关属性: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
AI写代码
看结果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
AI写代码
我们看到索引已经恢复回来了:

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语句,如果索引占用空间很大,可能也要花费大量时间建索引。 更多信息,参见https://help.kingbase.com.cn/v8/index.html

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询