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

  • Home
  • SEARCH
  • 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 6145401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:47:23+00:00 2026-05-23T18:47:23+00:00

I am trying to achieve by reducing the numbers of queries using ActiveRecord 3.0.9.

  • 0

I am trying to achieve by reducing the numbers of queries using ActiveRecord 3.0.9. I generated about ‘dummy’ 200K customers and 500K orders.

Here’s Models:

class Customer < ActiveRecord::Base
  has_many :orders
end

class Orders < ActiveRecord::Base
  belongs_to :customer
  has_many :products
end

class Product < ActiveRecord::Base
  belongs_to :order
end

when you are using this code in the controller:

@customers = Customer.where(:active => true).paginate(page => params[:page], :per_page => 100)
# SELECT * FROM customers ...

and use this in the view (I removed HAML codes for easier to read):

@order = @customers.each do |customer|
  customer.orders.each do |order|      # SELECT * FROM orders ...
    %td= order.products.count          # SELECT COUNT(*) FROM products ...
    %td= order.products.sum(:amount)   # SELECT SUM(*) FROM products ...
  end
end

However, the page is rendered the table with 100 rows per page. The problem is that it kinda slow to load because its firing about 3-5 queries per customer’s orders. thats about 300 queries to load the page.

There’s alternative way to reduce the number of queries and load the page faster?

Notes:

1) I have attempted to use the includes(:orders), but it included more than 200,000 order_ids. that’s issue.

2) they are already indexed.

  • 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-23T18:47:24+00:00Added an answer on May 23, 2026 at 6:47 pm

    If you’re only using COUNT and SUM(amount) then what you really need is to retrieve only that information and not the orders themselves. This is easily done with SQL:

    SELECT customer_id, order_id, COUNT(id) AS order_count, SUM(amount) AS order_total FROM orders LEFT JOIN products ON orders.id=products.order_id GROUP BY orders.customer_id, products.order_id
    

    You can wrap this in a method that returns a nice, orderly hash by re-mapping the SQL results into a structure that fits your requirements:

    class Order < ActiveRecord::Base
      def self.totals
        query = "..." # Query from above
    
        result = { }
    
        self.connection.select_rows(query).each do |row|
          # Build out an array for each unique customer_id in the results
          customer_set = result[row[0].to_i] ||= [ ]
    
          # Add a hash representing each order to this customer order set
          customer_set << { :order_id => row[1].to_i, :count => row[2].to_i, :total => row[3].to_i } ]
        end
    
        result
      end
    end
    

    This means you can fetch all order counts and totals in a single pass. If you have an index on customer_id, which is imperative in this case, then the query will usually be really fast even for large numbers of rows.

    You can save the results of this method into a variable such as @order_totals and reference it when rendering your table:

    - @order = @customers.each do |customer|
      - @order_totals[customer.id].each do |order|
        %td= order[:count]
        %td= order[:total]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying achieve what it says in the title. I'm using jQuery and I
Trying to achieve the drop down effect I have on my form here once
iam trying to achieve this exact functionalty: http://jsfiddle.net/exttq/ BUT inside using my PHP, right
I am trying to achieve a gradient + text shadow effect in Chrome/Safari using
Trying to achieve something like this using recursion: if (m > n) return; Foo
I'm trying to achieve framed texts (using Windows Forms), e.g.: Height is always the
I'm trying achieve a way to display my images in a particular layout using
What I'm trying to achieve is a code checker. Only the first 4 numbers
Im trying to achieve the same goal as OP there: Downloading mp3 files using
Im trying to achieve an insert statment and return using my function in the

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.