5. The problem 3/6
mapped = {}
fields = [:weights].concat(FoodsController::EAGER_LOADED_FIELDS)
Food.includes(*fields).find_each do |f|
mapped[f.id] = { json_data: f.as_json(methods: [:nutrient_data_basic]) }
end
Food.update(mapped.keys, mapped.values)
First iteration of the migration:
Result: Max Memory 1,1GB | 300 seconds
8. The problem 6/6
Fourth successful iteration of the migration:
Result: Max Memory 265MB | 82 seconds
fields = [:weights].concat(FoodsController::EAGER_LOADED_FIELDS)
Food.includes(*fields).find_in_batches(batch_size: 50) do |group|
group.each do |f|
f.update_columns json_data: f.as_json(methods: [:nutrient_data_basic])
end
end
9. update_columns(attributes)
Updates the attributes directly in the database issuing an
UPDATE SQL statement and sets them in the receiver.
This is the fastest way to update attributes because it goes
straight to the database, but take into account that in
consequence the regular update procedures are totally
bypassed.