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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:22:17+00:00 2026-05-18T07:22:17+00:00

Trying to get data in the most efficient way possible for some reports, using

  • 0

Trying to get data in the most efficient way possible for some reports, using Rails 2.3 and MySQL.

Our app has Users, and Deals, and PurchasedDeals. Relationships look like this:

class User
  has_many :purchased_deals
  has_many :deals, :through => :purchased_deals
end

class Deal
  has_many :purchased_deals
  has_many :users, :through => :purchased_deals
end

class PurchasedDeal
  belongs_to :deal
  belongs_to :user
end

For the report I’m running, I need to get all users that have made a purchase (i.e. have at least one PurchasedDeal), and then the sum total of all the deals they have bought (price is attached to the Deal, not the PurchasedDeal).

Certainly I could start with a list of all users, including both deals and purchased deals. I’ve tried that, and the query is massive (30,000 users, give or take, 3,000 deals, 100,000+ purchased deals).

I could start with users, then do a .each and find the ones that have a purchased deal, split them out into their own group, and then iterate over each of those to get the total purchased amount, but that is a fair amount of queries.

Currently, both of these methods take so long that the requests are timing out. What would the most efficient way be to get the data I need? Adding columns to tables is a totally acceptable solution, btw. I have full database access to do what I need.

Thanks!

  • 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-18T07:22:17+00:00Added an answer on May 18, 2026 at 7:22 am

    To get a list of user IDs with more than one purchase, you can do the following, which will access just one table:

    user_ids = PurchasedDeal.count(:group => :user_id, :having => 'count_all > 0').keys
    

    Subsequently, you can fetch all these users with:

    users = User.find user_ids
    

    Things can be sped up with a counter cache. In your user model, add the option :counter_cache => true to the has_many association for purchased deals. You’ll need an extra integer column on your users table and initialize, which might look as follows in a migration:

    add_column :users, :purchased_deals_count, :integer, :null => false, :default => 0
    User.each { |u| User.reset_counters u, :purchased_deals }
    

    Once that’s out of the way, it becomes a lot simpler:

    users = User.all :conditions => 'purchased_deals_count > 0'
    

    Rails will keep the column up-to-date for you, with most standard operations.


    To get the total price will always involve a join. Or you can build a hash of deal prices and do the tedious processing in Ruby. I’m no SQL expert, but you can potentially get rid of the join by storing the price with the PurchasedDeal. Otherwise, here’s how to do it with a join:

    user_id_to_price = PurchasedDeal.sum 'deal.price', :include => :deal, :group => :user_id
    

    You could filter that on just the users you want by adding something like :conditions => ['user_id IN (?)', users]. (Where users can be a list of IDs, but also User objects.)

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

Sidebar

Related Questions

What is the best way of handling trying to get data from a DataReader
I am trying to get some XML data with LINQ, but running into a
I am trying to figure out the most efficient way to format this query.
What would anyone consider the most efficient way to merge two datasets using Python?
Im trying to get a completly data copy from a gridview, itryed clone(), tryed
I am trying to get the DB2 data provider from a 32-bit .Net application
I'm trying to use jQuery to get data from an ASP.NET web service (SharePoint
I'm trying to use rusage statistics in my program to get data similar to
I'm trying to get ReportViewer to display data from a BindingSource (VB.Net Winforms). I
I'm trying to call the SQL statement below but get the following error: System.Data.SqlClient.SqlException:

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.