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 or update_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, being N 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.

  • Define a dataset size acceptable to execute individual updates.

    • Maybe if we're updating less than 500 or 1_000 is acceptable to generate N SQL updates?
Edited by Mayra Cabrera