简体   繁体   English

Rails和Postgres-恢复后重新索引

[英]Rails & postgres - reindex after restore

Recently I ran into a situation where I had to restore a backup database from Heroku to my local setup because of a configuration error I had made. 最近,由于遇到配置错误,我不得不将备份数据库从Heroku恢复到本地设置。

This was very straight-forward from a Heroku post: 这是从Heroku帖子中非常直接的:

https://devcenter.heroku.com/articles/heroku-postgres-import-export : https://devcenter.heroku.com/articles/heroku-postgres-import-export

$ heroku pgbackups:capture
$ curl -o latest.dump `heroku pgbackups:url`
$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d mydb latest.dump

The issue became that after the restore, some of the old (pre-restore) index data was still being served with the newly restored data. 问题在于,还原后,一些旧的(还原前)索引数据仍与新还原的数据一起提供。 ie it seemed that there was still some remnant index data after the restore and a db migration. 即,在还原和数据库迁移之后,似乎仍有一些剩余索引数据。 Note that before the restore, I did not clear or delete or do anything to my old db ... so obviously there was some data still there pre-restore ... the tables were gone, but the indexes may have still been around. 请注意,在还原之前,我没有清除或删除旧数据库或对其进行任何操作……因此很明显,在还原前仍然有一些数据……表已经消失了,但是索引可能仍然存在。

I suspected that if I made some changes to the table, that would cause a partial reindex of whatever I changed, and would be some type of partial fix to the problem. 我怀疑如果对表进行了某些更改,则将导致对所更改内容进行部分重新索引,并且将某种程度上解决该问题。 And sure enough, when I made a change to the record via the console, the remnant data was updated (and was now correct). 可以肯定的是,当我通过控制台对记录进行更改时,剩余数据已更新(现在是正确的)。

So my question is multi-part ... 所以我的问题是多部分的...

Why did the restore not clear everything in the db and "start from scratch"? 为什么还原无法清除数据库中的所有内容并“从头开始”? (Is there another way?) (还有其他方法吗?)

And is there a better programatic way to reindex a table via rake or console? 是否有更好的程序化方法通过rake或console重新索引表?

For searchkick I use: 对于searchkick,我使用:

rake searchkick:reindex CLASS=Blog

And within models I use something like: 在模型中,我使用类似:

blog.reindex

But I have not found a command/function way to do a basic table reindex (without some type of hack to touch every record). 但是我还没有找到一种执行基本表重新索引的命令/功能方法(没有某种类型的技巧可以触及每条记录)。

Based on OP's response to my comment, I think this is the result of applying a full DB backup onto an existing DB, resulting in some cross-pollination between the restored data and the existing data, including stale indexes. 根据OP对我的评论的回答,我认为这是将完整的数据库备份应用于现有数据库的结果,导致还原的数据与现有数据(包括陈旧索引)之间出现某种交叉授粉。

What I would recommend is, if the backup was the entire DB: 我建议的是,如果备份是整个数据库:

  1. On your local db, drop the whole thing using the dropdb command 在本地数据库上,使用dropdb命令删除整个内容
  2. Re-create a blank db using createdb 使用createdb重新创建一个空白数据库
  3. Then run your pg_restore command against the empty DB. 然后对空的数据库运行pg_restore命令。 That will restore all your data from the backup, and, providing indexes were part of that backup (which they should be in a backup that's the entire db), those should be populated with the newly restored data as well. 这将从备份中还原所有数据,并且假设索引是该备份的一部分(它们应该在整个数据库的备份中),那么索引中也应填充新还原的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM