Batch insertion in rails 3

前端 未结 4 1292
臣服心动
臣服心动 2020-12-04 16:44

I want to do a batch insert of few thousand records into the database (POSTGRES in my case) from within my Rails App.

What would be the \"Rails way\" of doing it? So

4条回答
  •  北海茫月
    2020-12-04 17:20

    You can do it the fast way or the Rails way ;) The best way in my experience to import bulk data to Postgres is via CSV. What will take several minutes the Rails way will take several seconds using Postgres' native CSV import capability.

    http://www.postgresql.org/docs/9.2/static/sql-copy.html

    It even triggers database triggers and respects database constraints.

    Edit (after your comment): Gotcha. In that case you have correctly described your two options. I have been in the same situation before, implemented it using the Rails 1000 save! strategy because it was the simplest thing that worked, and then optimized it to the 'append a huge query string' strategy because it was an order of magnitude better performing.

    Of course, premature optimization is the root of all evil, so perhaps do it the simple slow Rails way, and know that building a big query string is a perfectly legit technique for optimization at the expense of maintainabilty. I feel your real question is 'is there a Railsy way that doesn't involve 1000's of queries?' - unfortunately the answer to that is no.

提交回复
热议问题