A controller action that creates multiple objects within a loop is a candidate for N+1 queries. Yet, the the following segment
shop_id = section.shop_id
section_id = section.id
serving_table_id = section.serving_table.id
range = 1..i
range.each do |i|
@cover = Cover.create(shop_id: shop_id, section_id: section_id, serving_table_id: serving_table_id, name: i )
end
event though the attributes to be saved are defined before the iteration block,
is generating via prosopite
gem an N+1 query detection,
for each of the shop, parti and serving_table objects.
Not being a query for a collection, includes
would be inapplicable.
How does one avoid N+1 queries in this instance?
2
You’re erraneously assuming that there is one single simple cure-all solution for every scenario.
N+1 queries on read are a relatively simple problem to solve as you can just load the related rows through a join or a separate query with few side effects.
Handing mass inserting/updating/upserting records is not an easy problem.
You need to carefully consider if you can forgo validations and callbacks and use insert_all
/ update_all
/ upsert_all
in a single query or if you actually need to handle each record separately.
The key factors in deciding here is if the operation is dealing with user input and can be expected to fail, how you’re going to handle errors and the perfomance requirements.
Then you have to consider if this can be offloaded into a background job so that the web thead (and the user behind the screen) doesn’t have to wait for it to complete which adds the headaches of dealing with an asyncronous process.
3
The ActiveRecord::Base#insert_all
(https://apidock.com/rails/v7.1.3.4/ActiveRecord/Persistence/ClassMethods/insert_all) can take an array of hashes to create
shop_id = section.shop_id
section_id = section.id
serving_table_id = section.serving_table.id
range = 1..i
Cover.insert_all(
range.map do |i|
{ shop_id: shop_id, section_id: section_id, serving_table_id: serving_table_id, name: i }
end
)
Be careful of statement rollback in case one is invalid.
3