Skip to content

GitLab Next

  • Menu
Projects Groups Snippets
    • Loading...
  • 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 39,511
    • Issues 39,511
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 1,222
    • Merge requests 1,222
  • Requirements
    • Requirements
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Monitor
    • Monitor
    • Metrics
    • Incidents
  • Packages & Registries
    • Packages & Registries
    • Container Registry
  • Analytics
    • Analytics
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
    • Value stream
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • GitLab.org
  • GitLabGitLab
  • Issues
  • #32921

Closed
Open
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