Multiple row insertion in a faster way

Posted by Cyborgo on Stack Overflow See other posts from Stack Overflow or by Cyborgo
Published on 2010-06-09T22:36:34Z Indexed on 2010/06/09 22:42 UTC
Read the original article Hit count: 141

Filed under:
|

Hi,

I am using rails and I am trying to insert new rows into the table. The data comes from a text file and I created hash with one of the attribute as key. Then I find all the keys that are currently not in the table. Then I need to insert all of them into the table with all the attributes that I saved in hash.

newvals.each{
    |x|
    author = Author.create(:first => x, :second => hash[x][0], :third => hash[x][1])
    author.save
}

This has lot of overhead as it makes an SQL update one at time. The newals array is usually quite big around the size of 20-30k. I know create can insert multiple objects at once, but I cant seem to figure out how. Is there any way to get this done. Thanks.

© Stack Overflow or respective owner

Related posts about sql

Related posts about ruby-on-rails