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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:02:12+00:00 2026-06-06T06:02:12+00:00

I’m trying to figure out how to ask this – so I’ll update the

  • 0

I’m trying to figure out how to ask this – so I’ll update the question as it goes to clear things up if needed.

I have a virtual stock exchange game site I’ve been building for fun. People make tons of trades, and each trade is its own record in a table.

When showing the portfolio page, I have to calculate everything on the fly, on the table of data – i.e. How many shares the user has, total gains, losses etc.

Things have really started slowing down, when I try to segment it by trades by company by day.

I don’t really have any code to show to demonstrate this – but it just feels like I’m not approaching this correctly.

UPDATE: This code in particular is very slow

 #Returning an array of values for a total portfolio over time
  def portfolio_value_over_time
    portfolio_value_array = []

    days = self.from_the_first_funding_date

    companies = self.list_of_companies

    days.each_with_index do |day, index|

      #Starting value
      days_value = 0

      companies.each do |company|
        holdings = self.holdings_by_day_and_company(day, company)
        price = Company.find_by_id(company).day_price(day)
        days_value = days_value + (holdings * price).to_i
      end

      #Adding all companies together for that day
      portfolio_value_array[index] = days_value 
    end

The page load time can be up to 20+ seconds – totally insane. And I’ve cached a lot of the requests in Memcache.

Should I not be generating reports / charts on the live data like this? Should I be running a cron task and storing them somewhere? Whats the best practice for handling this volume of data in Rails?

  • 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-06T06:02:15+00:00Added an answer on June 6, 2026 at 6:02 am

    The Problem

    Of course it’s slow. You’re presumably looking up large volumes of data from each table, and performing multiple lookups on multiple tables on every iteration through your loop.

    One Solution (Among Many)

    You need to normalize your data, create a few new models to store expensive calculated values, and push more of the calculations onto the database or into tables.

    The fact that you’re doing a nested loop over high-volume data is a red flag. You’re making many calls to the database, when ideally you should be making as few sequential requests as possible.

    I have no idea how you need to normalize your data or optimize your queries, but you can start by looking at the output of explain. In general, though, you probably will want to eliminate any full table scans and return data in larger chunks, rather than a record at a time.

    This really seems more like a modeling or query problem than a Rails problem, per se. Hope this gets you pointed in the right direction!

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is

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.