Skip to content
GitLab
Next
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • GitLab GitLab
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 44,765
    • Issues 44,765
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
    • Requirements
  • Merge requests 1,329
    • Merge requests 1,329
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Container Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Metrics
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • GitLab.orgGitLab.org
  • GitLabGitLab
  • Issues
  • #32921
Closed
Open
Issue created Sep 27, 2019 by Mayra Cabrera@mayra-cabrera⚡Maintainer

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.

    • @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 or 1_000 is acceptable to generate N SQL updates?
Edited Sep 30, 2019 by Mayra Cabrera
Assignee
Assign to
Time tracking