Update several records with distinct values
Currently, we don't have a way to update multiple records with different values each. Normally the approach that is taken is:
- Make a query that fetches the records to be updated and then,
- Iterate on each one of these records and then
- Update them accordingly with a call to
update
orupdate_attributes
Example (taken from gitlab-foss!32293 (closed)):
Feedback.where_might_need_update.find_in_batches(batch_size: 500) do |feedback_batch|
feedback_batch.each do |feedback|
if feedback.needs_update?
feedback.update(attributes)
end
end
end
end
I can see two problems with this approach:
- We're generating
N
queries, beingN
the number of records to be updated. - We might generate one more query when setting the attributes (as some values might depend on another query)
I would like for us to
-
Find a way to update records with different values in a single query, similar to what we do with
Gitlab::Database#bulk_insert
.-
@abrandl
mentioned to prepare the new data in a temporary table and use this to perform batch updates on the actual target table - gitlab-foss!32293 (comment 218165279)
-
-
Define a dataset size acceptable to execute individual updates.
- Maybe if we're updating less than
500
or1_000
is acceptable to generateN
SQL updates?
- Maybe if we're updating less than
Edited by Mayra Cabrera