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 828623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:45:11+00:00 2026-05-15T03:45:11+00:00

I have to calculate a total for each user based on individual actions —

  • 0

I have to calculate a “total” for each user based on individual actions — that is, a User hasMany Actions and each Action has a point. So I need to basically get a sum of all the points on all the actions.

Actions are added and subtracted regularly. Since this can be a rather heavy operation, it’s not feasible that I execute the “total” each time I need it. Therefore, I am thinking of running the operation once a day for each user and storing the total points on the User as an int.

Because I have thousands of users, I am trying to figure out the best way to do this. Should I basically iterate through all the users and call User.save() each time, or is there some kind of batch update mechanism I can use in Grails / GORM?

  • 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-05-15T03:45:12+00:00Added an answer on May 15, 2026 at 3:45 am

    Is the model in your question your actual model, or a simplified version of it? If all you’re doing is User hasMany Actions and each Action has a point value (an integer?) that you’d like to sum up, that’s really the kind of thing that relational databases excel at. If you have the proper indexes on your database, I’d think this would be a very quick call, as long as you’re doing a groupBy query (or using grails projection criteria).

    Here’s an example method that will take a list of users and will return an array that pairs user.id to the number of points that user currently has:

    def calculateCurrentPoints(users) {
        Action.executeQuery('select a.user.id, sum(points) from Action a where a.user.id in (:userIds) group by a.user.id', [userIds: users.id])
    }
    

    If desired, you could easily turn this into a map of user id to points for easier look-up:

    def calculateCurrentPoints(users) {
        def result = Action.executeQuery('select a.user.id, sum(points) from Action a where a.user.id in (:userIds) group by a.user.id', [userIds: users.id])
        result.inject([:]) { map, userWithPoints -> 
            map[userWithPoints[0]] = userWithPoints[1]
            return map
        }
    }
    

    If this really is slower than I’m thinking it is, you could use the executeUpdate with HQL similiar to what I have above to update a “totalPoints” field on each user (or keep it updated as part of a service whenever you add a new Action to that user).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two system calls GetSystemTime() and GetThreadTimes() that I need to calculate the
I have this situation that I need to let users define decisions based on
I'm doing a probability calculation. I have a query to calculate the total number
I have a program that will calculate the minimal area taken by fitting rectangles
I have a LinqDataSource that I use to calculate the number of rows in
If I have a date, how do I calculate the week number for that
I have a simple HTML Form that allows the user to enter some text
I have a coding/maths problem that I need help translating into C#. It's a
I'm using the following jQuery script to calculate the total width of ul based
I have a series of datetime objects and would like to calculate the average

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.