Code Monkey home page Code Monkey logo

active_record_bulk_insert's Introduction

BULK INSERT

Quick Example

#Gemfile
gem "active_record_bulk_insert"
users = [{:username => "foo", :firstname => "Foo", :lastname => "Bar", :age => 31},
         {:username => "j.doe", :firstname => "John", :lastname => "Doe", :age => 57}]
User.bulk_insert(users)
User.count # => 2

Insert ActiveRecord Objects

user1 = User.new({:username => "foo", :firstname => "Foo", :lastname => "Bar", :age => 31})
user2 = User.new({:username => "j.doe", :firstname => "John", :lastname => "Doe", :age => 57})
User.bulk_insert([user1, user2])
User.count # => 2

Evoke Active Record Validations (after_intialize and validation callbacks)

class User < ActiveRecord
  validates :username, :presence => true
end
users = [{:username => "foo", :firstname => "Foo", :lastname => "Bar", :age => 31},
         {:username => "j.doe", :firstname => "John", :lastname => "Doe", :age => 57},
         {:firstname => "John", :lastname => "Doe", :age => 57}]
User.bulk_insert(users, :validate => true)
# => [{:firstname => "John", :lastname => "Doe", :age => 57}]

The return value is a list of invalid records

Provide your own primary keys

users = [{:id => 200, :username => "foo", :firstname => "Foo", :lastname => "Bar", :age => 31},
         :id => 201, {:username => "j.doe", :firstname => "John", :lastname => "Doe", :age => 57}]
User.bulk_insert(users, :use_provided_primary_key => true)

note this is only available from ActiveRecord 4.0 as id was protected from mass-assignment in ActiveRecord < 4.0

Bulk insert in batches

users = 1000000.times.map do |i|
  {:username => "foo#{i}", :firstname => "Foo#{i}", :lastname => "Bar#{i}", :age => (30..70).to_a.sample}
end
User.bulk_insert_in_batches(users, :batch_size => 10000)
User.count # => 1000000

Benchmark

DB: PostgreSQL 9.1.9
OS: Debian GNU/Linux 7 (wheezy)
Processor: Intel(R) Core(TM) i7-4960HQ CPU @ 2.60GHz
Memory: 6GB
Number of Records: 10,000

                                user     system      total        real
Create with Active Record   1.970000   6.810000   8.780000 ( 14.822348)
AR with validations         0.280000   0.000000   0.280000 (  0.299018)
AR without validations      0.190000   0.000000   0.190000 (  0.207807)
Hash without validations    0.080000   0.000000   0.080000 (  0.108874)
Hash with validations       0.690000   0.000000   0.690000 (  0.669952)

License bulk_insert is released under MIT license.

active_record_bulk_insert's People

Contributors

bjhaid avatar koriroys avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.