Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
B

bulk_insert

> 编程语言
Open source

Efficient bulk inserts with ActiveRecord

809 stars0 likes3 views
WebsiteGitHub

About

Efficient bulk inserts with ActiveRecord

BulkInsert

A little ActiveRecord extension for helping to insert lots of rows in a single insert statement.

Installation

Add it to your Gemfile:

ruby
gem 'bulk_insert'

Usage

BulkInsert adds a new class method to your ActiveRecord models:

ruby
class Book  "paper"

By default, the batch is always saved when the block finishes, but you can explicitly save inside the block whenever you want, by calling #save! on the worker:

ruby
Book.bulk_insert do |worker|
  worker.add(...)
  worker.add(...)

  worker.save!

  worker.add(...)
  #...
end

That will save the batch as it has been defined to that point, and then empty the batch so that you can add more rows to it if you want. Note that all records saved together will have the same created_at/updated_at timestamp (unless one was explicitly set).

Batch Set Size

By default, the size of the insert is limited to 500 rows at a time. This is called the set size. If you add another row that causes the set to exceed the set size, the insert statement is automatically built and executed, and the batch is reset.

If you want a larger (or smaller) set size, you can specify it in two ways:

ruby
# specify set_size when initializing the bulk insert...
Book.bulk_insert(set_size: 100) do |worker|
  # ...
end

# specify it on the worker directly...
Book.bulk_insert do |worker|
  worker.set_size = 100
  # ...
end

Insert Ignore

By default, when an insert fails the whole batch of inserts fail. The ignore option ignores the inserts that would have failed (because of duplicate keys or a null in column with a not null constraint) and inserts the rest of the batch.

This is not the default because no errors are raised for the bad inserts in the batch.

ruby
destination_columns = [:title, :author]

# Ignore bad inserts in the batch
Book.bulk_insert(*destination_columns, ignore: true) do |worker|
  worker.add(...)
  worker.add(...)
  # ...
end

Update Duplicates (MySQL, PostgreSQL)

If you don't want to ignore duplicate rows but instead want to update them then you can use the update_duplicates option. Set this option to true (MySQL) or list unique column names (PostgreSQL) and when a duplicate row is found the row will be updated with your new values. Default value for this option is false.

ruby
destination_columns = [:title, :author]

# Update duplicate rows (MySQL)
Book.bulk_insert(*destination_columns, update_duplicates: true) do |worker|
  worker.add(...)
  worker.add(...)
  # ...
end

# Update duplicate rows (PostgreSQL)
Book.bulk_insert(*destination_columns, update_duplicates: %w[title]) do |worker|
  worker.add(...)
  # ...
end

Return Primary Keys (PostgreSQL, PostGIS)

If you want the worker to store primary keys of inserted records, then you can use the return_primary_keys option. The worker will store a result_sets array of ActiveRecord::Result objects. Each ActiveRecord::Result object will contain the primary keys of a batch of inserted records.

ruby
worker = Book.bulk_insert(*destination_columns, return_primary_keys: true) do
|worker|
  worker.add(...)
  worker.add(...)
  # ...
end

worker.result_sets

Ruby and Rails Versions Supported

:warning: The scope of this gem may be somehow covered natively by the .insert_all API introduced by Rails 6. This gem represents the state of art for rails version further developments for more recent versions.

The current CI prevents regressions on the following versions:

ruby / rails ~>3 ~>4 ~>5 ~>6
2.2 yes yes no no
2.3 yes yes yes no
2.4 no yes yes no
2.5 no no yes yes
2.6 no no yes yes
2.7 no no yes yes

The adapters covered in the CI are:

  • sqlite
  • mysql
  • postgresql

License

BulkInsert is released under the MIT license (see MIT-LICENSE) by Jamis Buck ([email protected]).

Issues· 15 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言