ProductPromotion
Logo

Ruby

made by https://0x3d.site

GitHub - fatkodima/pluck_in_batches: A faster alternative to the custom use of `in_batches` with `pluck`
A faster alternative to the custom use of `in_batches` with `pluck` - fatkodima/pluck_in_batches
Visit Site

GitHub - fatkodima/pluck_in_batches: A faster alternative to the custom use of `in_batches` with `pluck`

GitHub - fatkodima/pluck_in_batches: A faster alternative to the custom use of `in_batches` with `pluck`

PluckInBatches

Build Status

ActiveRecord comes with find_each / find_in_batches / in_batches methods to batch process records from a database. ActiveRecord also has the pluck method which allows the selection of a set of fields without pulling the entire record into memory.

This gem combines these ideas and provides pluck_each and pluck_in_batches methods to allow batch processing of plucked fields from the database.

It performs half of the number of SQL queries, allocates up to half of the memory and is up to 2x faster (or more, depending on how far is your database from the application) than the available alternative:

# Before
User.in_batches do |batch| # or .find_in_batches, or .select(:email).find_each etc
  emails = batch.pluck(:emails)
  # do something with emails
end

# Now, using this gem (up to 2x faster)
User.pluck_in_batches(:email) do |emails|
  # do something with emails
end

Note: You may also find sidekiq-iteration useful when iterating over large collections in Sidekiq jobs.

Requirements

  • Ruby 2.7+
  • ActiveRecord 6+

If you need support for older versions, open an issue.

Installation

Add this line to your application's Gemfile:

gem 'pluck_in_batches'

And then execute:

$ bundle

Or install it yourself as:

$ gem install pluck_in_batches

Usage

pluck_each

Behaves similarly to find_each ActiveRecord's method, but yields each set of values corresponding to the specified columns.

# Single column
User.where(active: true).pluck_each(:email) do |email|
  # do something with email
end

# Multiple columns
User.where(active: true).pluck_each(:id, :email) do |id, email|
  # do something with id and email
end

pluck_in_batches

Behaves similarly to in_batches ActiveRecord's method, but yields each batch of values corresponding to the specified columns.

# Single column
User.where("age > 21").pluck_in_batches(:email) do |emails|
  jobs = emails.map { |email| PartyReminderJob.new(email) }
  ActiveJob.perform_all_later(jobs)
end

# Multiple columns
User.pluck_in_batches(:name, :email).with_index do |group, index|
  puts "Processing group ##{index}"
  jobs = group.map { |name, email| PartyReminderJob.new(name, email) }
  ActiveJob.perform_all_later(jobs)
end

# Custom arel column
User.pluck_in_batches(:id, Arel.sql("json_extract(users.metadata, '$.rank')")).with_index do |group, index|
  # ...
end

Both methods support the following configuration options:

  • :batch_size - Specifies the size of the batch. Defaults to 1000. Also aliased as :of.
  • :start - Specifies the primary key value to start from, inclusive of the value.
  • :finish - Specifies the primary key value to end at, inclusive of the value.
  • :error_on_ignore - Overrides the application config to specify if an error should be raised when an order is present in the relation.
  • :cursor_column - Specifies the column(s) on which the iteration should be done. This column(s) should be orderable (e.g. an integer or string). Defaults to primary key.
  • :order - Specifies the primary key order (can be :asc or :desc or an array consisting of :asc or :desc). Defaults to :asc.

Development

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/fatkodima/pluck_in_batches.

License

The gem is available as open source under the terms of the MIT License.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory