前言
最近碰到一个案例,在使用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
看结果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语句,如果索引占用空间很大,可能也要花费大量时间建索引。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




