An ActiveRecord extension for efficiently inserting large datasets via single SQL statements in Ruby on Rails.
BulkInsert is a Ruby gem that extends ActiveRecord to enable efficient bulk data insertion in Ruby on Rails applications. It solves the performance problem of inserting large datasets by consolidating multiple rows into a single SQL INSERT statement, dramatically speeding up data imports and batch operations.
Ruby on Rails developers working with data imports, batch processing, or any scenario requiring insertion of large numbers of records into a database.
Developers choose BulkInsert for its significant performance improvements over sequential ActiveRecord inserts, its clean and flexible API, and its robust handling of edge cases like timestamp management, duplicate conflicts, and database defaults.
Efficient bulk inserts with ActiveRecord
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
By consolidating multiple inserts into a single SQL statement, it drastically reduces database round-trips, making data imports up to hundreds of times faster than sequential ActiveRecord saves.
Offers both block-based and array-based syntax, with support for explicit column lists and hash/array row formats, catering to different coding preferences and data structures.
Automatically sets `created_at` and `updated_at` timestamps and respects database column defaults when values are omitted, reducing boilerplate code.
Provides `ignore` and `update_duplicates` options for MySQL and PostgreSQL, allowing for upsert-like behavior and graceful handling of duplicate keys.
Allows manual saving with `#save!` and configurable set sizes, enabling developers to manage transaction boundaries and memory usage effectively.
With Rails 6 introducing `.insert_all`, BulkInsert becomes redundant for many use cases in newer applications, unless specific features like automatic timestamps are needed.
Since it performs raw SQL inserts, it skips ActiveRecord validations and callbacks, which can compromise data integrity if not handled externally.
Only supports a subset of database adapters (sqlite, mysql, postgresql), and compatibility with newer Ruby/Rails versions is not guaranteed beyond tested ranges.
Errors in bulk inserts can be harder to debug, as failures might affect entire batches, and the `ignore` option silently drops problematic rows without raising exceptions.