开发者

How to drop DB indices before bulk loading in seeds.rb?

开发者 https://www.devze.com 2023-04-11 02:15 出处:网络
In my rails app I have a seeds.rb script which inserts lots of rec开发者_运维问答ords. In fact I\'m trying to load 16 million of them. It\'s taking a long time.

In my rails app I have a seeds.rb script which inserts lots of rec开发者_运维问答ords. In fact I'm trying to load 16 million of them. It's taking a long time.

One thing I wanted to try to speed this up, is to drop the table indices and re-add them afterwards. If it sounds like I'm doing something insane, please let me know, but that seems to be one recommendation for bulk loading into postgres

I use add_index and remove_index commands in migrations, but the same syntax doesn't work in a seeds.rb file. Is it possible to do this outside a migration in fact? (I'm imagining it might not be best practice, because it represents a schema change)

rails v2.3.8, postgres v8.4.8


One possibility is just to indulge in a little raw SQL within seeds.rb

ActiveRecord::Base.connection.execute("DROP INDEX myindex ON mytable")

At 16 million records, I would recommend managing the whole thing via raw SQL (contained within seeds.rb if you like). Do all 16 million records go into a single table? There ought to be some PostgreSQL magic to bulk import a file (in a PostgreSQL specific format) into a table.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号