How to implement bulk insert in Rails 3

一个人想着一个人 提交于 2019-11-26 17:37:30

问题


I need to insert a array of emails as different records into my contacts table. How can this be done.

Eg: @email = ["a@b.com", "c@d.com", "e@f.com", ... ]

I dont want to use.

  @email.each do |email|
     @contact = Contact.new
     @contact.email = email
     @contact.save
  end

This cause n insert quires. I just need a single insert query to insert these values. How can this be done in rails 3.0.9 (and ideally MySQL). Please help


回答1:


activerecord-import implements AR#import

activerecord-import is a library for bulk inserting data using ActiveRecord.

see how it works:

books = []
10.times do |i| 
  books << Book.new(:name => "book #{i}")
end
Book.import books

Project's home is on Github and it's wiki.




回答2:


You might also try upsert, which is approximately as fast as activerecord-import, but only works (currently) with MySQL, Postgres, and SQLite3:

require 'upsert'
Upsert.batch(Contact.connection, Contact.table_name) do |upsert|
  emails.each do |email|
    upsert.row(email: email)
  end
end

Note that this involves one database query per record, but it's an "upsert," so you don't have to check if a record already exists. In your example, this isn't a concern, but in most applications it becomes one eventually.




回答3:


The simplest way without additional gem is to concat a string and execute it in one SQL insertion (http://www.electrictoolbox.com/mysql-insert-multiple-records/).

@email = ["a@b.com", "c@d.com", "e@f.com"]

time = Time.current.to_s(:db)

values = @email.map do |email|
  "('#{email}', '#{time}', '#{time}')"
end

sql = "INSERT INTO contacts (email, created_at, updated_at) VALUES #{values.join(', ')}"
Contact.connection.execute(sql)



回答4:


I just wrote a little monkey-patch for Active Record 3.2 to INSERT many new records with a single SQL query, check it out:

https://github.com/alexdowad/showcase/blob/master/activerecord/bulk_db_operations.rb



来源:https://stackoverflow.com/questions/8505263/how-to-implement-bulk-insert-in-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!