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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:58:33+00:00 2026-06-01T20:58:33+00:00

I don’t know if this is the right place to ask question like this,

  • 0

I don’t know if this is the right place to ask question like this, but here it goes:

I have an intranet-like Rails 3 application managing about 20k users which are in nested-set (preordered tree – http://en.wikipedia.org/wiki/Nested_set_model).
Those users enter stats (data, just plain numeric values). Entered stats are assigned to category (we call it Pointer) and a week number.

Those data are further processed and computed to Results.
Some are computed from users activity + result from some other category… etc.
What user enters isn’t always the same what he sees in reports.

Those computations can be very tricky, some categories have very specific formulae.

But the rest is just “give me sum of all entered values for this category for this user for this week/month/year”.

Problem is that those stats needs also to be summed for a subset of users under selected user (so it will basically return sum of all values for all users under the user, including self).

This app is in production for 2 years and it is doing its job pretty well… but with more and more users it’s also pretty slow when it comes to server-expensive reports, like “give me list of all users under myself and their statistics. One line for summed by their sub-group and one line for their personal stats”). Of course, users wants (and needs) their reports to be as actual as possible, 5 mins to reflect newly entered data is too much for them. And this specific report is their favorite :/
To stay realtime, we cannot do the high-intensive sqls directly… That would kill the server. So I’m computing them only once via background process and frontend just reads the results.
Those sqls are hard to optimize and I’m glad I’ve moved from this approach… (caching is not an option. See below.)

Current app goes like this:

  • frontend: when user enters new data, it is saved to simple mysql table, like [user_id, pointer_id, date, value] and there is also insert to the queue.

  • backend: then there is calc_daemon process, which every 5 seconds checks the queue for new “recompute requests”. We pop the requests, determine what else needs to be recomputed along with it (pointers have dependencies… simplest case is: when you change week stats, we must recompute month and year stats…). It does this recomputation the easy way.. we select the data by customized per-pointer-different sqls generated by their classes.

  • those computed results are then written back to mysql, but to partitioned tables (one table per year). One line in this table is like [user_id, pointer_id, month_value, w1_value, w2_value, w3_value, w4_value]. This way, the tables have ~500k records (I’ve basically reduced 5x # of records).
  • when frontend needs those results it does simple sums on those partitioned data, with 2 joins (because of the nested set conds).

The problem is that those simple sqls with sums, group by and join-on-the-subtree can take like 200ms each… just for a few records.. and we need to run a lot of these sqls… I think they are optimized the best they can, according to explain… but they are just too hard for it.

So… The QUESTION:

Can I rewrite this to use Redis (or other fast key-value store) and see any benefit from it when I’m using Ruby and Rails? As I see it, if I’ll rewrite it to use redis, I’ll have to run much more queries against it than I have to with mysql, and then perform the sum in ruby manually… so the performance can be hurt considerably… I’m not really sure if I could write all the possible queries I have now with redis… Loading the users in rails and then doing something like “redis, give me sum for users 1,2,3,4,5…” doesn’t seem like right idea… But maybe there is some feature in redis that could make this simpler?)…
Also the tree structure needs to be like nested set, i.e. it cannot have one entry in redis with list of all child-ids for some user (something like children_for_user_10: [1,2,3]) because the tree structure changes frequently… That’s also the reason why I can’t have those sums in those partitioned tables, because when the tree changes, I would have to recompute everything.. That’s why I perform those sums realtime.)

Or would you suggest me to rewrite this app to different language (java?) and to compute the results in memory instead? 🙂 (I’ve tried to do it SOA-way but it failed on that I end up one way or another with XXX megabytes of data in ruby… especially when generating the reports… and gc just kills it…) (and a side effect is that one generating report blocks the whole rails app :/ )

Suggestions are welcome.

  • 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-01T20:58:34+00:00Added an answer on June 1, 2026 at 8:58 pm

    Redis would be faster, it is an in-memory database, but can you fit all of that data in memory? Iterating over redis keys is not recommended, as noted in the comments, so I wouldn’t use it to store the raw data. However, Redis is often used for storing the results of sums (e.g. logging counts of events), for example it has a fast INCR command.

    I’m guessing that you would get sufficient speed improvement by using a stored procedure or a faster language than ruby (eg C-inline or Go) to do the recalculation. Are you doing group-by in the recalculation? Is it possible to change group-bys to code that orders the result-set and then manually checks when the ‘group’ changes. For example if you are looping by user and grouping by week inside the loop, change that to ordering by user and week and keep variables for the current and previous values of user and week, as well as variables for the sums.

    This is assuming the bottleneck is the recalculation, you don’t really mention which part is too slow.

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

Sidebar

Related Questions

Don't know if this is the right place to ask this, but I will
don't know better title for this, but here's my code. I have class user
Don't know if I worded the question right, but basically what I want to
I don't know if this question is trivial or not. But after a couple
I don't know if this has been asked before, but what i'd like to
Don't know why but I can't find a solution to this. I have 3
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this has been asked before, so point me to another question
Don't know a better title but here is what im trying to do. I
Don't ask why but I have the requirement to draw a border around certain

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.