Batched queries in rails | Ram Laxman Yadav

Batched queries in rails

However in practice we need to perform certain task on each record and there are lots of records in the table. For this purpose we use batched queries in rails. Here is an example for the same:

What is batched queries:

Batched queries helps to select the records in batches. For example:

Order.find_each(batch_size: 50).map{|o| o.to_json }
#=> It will select all orders in batches like first batch will be 1-50 and second 51-100 etc.

You can use select_in_batches also for the same purpose.

Thanks for the reading !!!

Ram Laxman Yadav
Ram Laxman Yadav
Ruby on Rails Developer