Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9210927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:11:05+00:00 2026-06-18T01:11:05+00:00

If I had to update 50,000 users, how would I go about it in

  • 0

If I had to update 50,000 users, how would I go about it in a way that is best with a background processing library and not a N+1 issue?

I have users, membership, and points.

Memberships are related to total point values. If the membership is modified with point values I have to run through all of the users to update their proper membership. This is what I need to queue so the server isn’t hanging for 30+ minutes.

Right now I have in a controller action

def update_memberberships
  User.find_each do |user|
    user.update_membership_level! # looks for a Membership defined by x points and assigns it to user. Then Saves the user.
  end
end

This is a very expensive operation. How would I optimize for processing and in background so the post is near instantaneous from the form?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:11:06+00:00Added an answer on June 18, 2026 at 1:11 am

    You seem to be after how to get this done with Resque or delayed_job. I’ll give an example with delayed_job.

    To create the job, add a method to app/models/user.rb:

    def self.process_x_update
      User.where("z = 1").find_each(:batch_size => 5000) do |user|
        user.x = user.y + 3
        user.save
      end
    end
    handle_asynchronously :process_x_update
    

    This will update all User records where z = 1, setting user.x = user.y + 3. This will complete this in batches of 5,000, so that performance is a bit more linear.

    This will cause User.process_x_update to complete very quickly. To actually process the job, you should be running rake jobs:work in the background or start a cluster of daemons with ./script/delayed_job start

    One other thing: can you move this logic to one SQL statement? That way you could have one statement that’s fast and atomic. You’d still want to do this in the background as it could take some time to process. You could do something like:

    def process_x_update
      User.where("z = 1").update_all("x = y + 3")
    end
    handle_asynchronously :process_x_update
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with about 100.000 rows that used to look more or
UPDATE!!! Suggested answer is NOT correct, my mistake. The #container DIV should've had float:left;.
I recently made an update to JasperReports library version 4.1.3. Previously, I had a
I have a table with ~800k rows. I ran an update users set hash
Its properly because I do not understand completely, but I have had these issues
I have a website that calculates a users involvement/activity using multiple MySQL queries. For
I have a hefty PHP script. So much so that I have had to
I have a monitoring app that receives processing updates from other applications via WCF.
I am managing a website which has about 140.000 documents. This system had been
I have used this query on many occasions and never had an issue. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.