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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:40:46+00:00 2026-05-21T23:40:46+00:00

The following query runs fairly quickly, but the series processing that needs to take

  • 0

The following query runs fairly quickly, but the series processing that needs to take place afterwards is really slowing this method down. I could use some help in refactoring.

def self.sum_amount_chart_series(start_time)
  orders_by_day = Widget.archived.not_void.
                  where(:print_datetime => start_time.beginning_of_day..Time.zone.now.end_of_day).
                  group(pg_print_date_group).
                  select("#{pg_print_date_group} as print_date, sum(amount) as total_amount")


  # THIS IS WHAT IS SLOWING THE METHOD DOWN!
  (start_time.to_date..Date.today).map do |date|
    order = orders_by_day.detect { |order| order.print_date.to_date == date }
    order && order.total_amount.to_f.round(2) || 0.0
  end

end

def self.pg_print_date_group
  "CAST((print_datetime + interval '#{tz_offset_hours} hours') AS date)"
end

I have benchmarked this method and the offending code is the series loop where it generates a series of dates and then maps out a new array with an amount for each date. This way I get a series back with amounts for every date, regardless if it has an amount or not.

When the query only returns a few dates, it runs fairly quickly. But set the start date back a year or two and it becomes impossibly slow. The real offender is the .detect method. It’s very slow at scanning the array of activerecord objects.

Is there a faster method to generates this series?

  • 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-21T23:40:47+00:00Added an answer on May 21, 2026 at 11:40 pm

    orders_by_day is grouped by “pg_print_date_group” so it should be a hash of “date” to objects. so why don’t you just do

    (start_time.to_date..Date.today).map do |date|
      order = orders_by_day[date.to_s(:db)]
      order && order.total_amount.to_f.round(2) || 0.0
    end
    

    That should seriously reduce the Big O of your run. And if I’m misunderstanding and your orders_by_day isn’t a hash, preprocess it into a hash and then run the map, you definitely don’t want to detect for every date.

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

Sidebar

Related Questions

I have the following SQL query that runs fairly well, however it seems to
I have the following query that runs in my Oracle database and I want
I have the following query that runs successfully in LinqPad: var results = from
I have the following query that when executed, it just runs for hours and
I have tested the following query in LINQPad and it runs fine, but VS2010
I have the following query that runs as part of a CRON job, which
The following query will display all Dewey Decimal numbers that have been duplicated in
The following query works on Oracle 10.2.0.1.0 on windows,but doesn't work on Oracle 10.2.0.2.0
i have the following SQL Query which runs on SQL Server CE 4 SELECT
I have the following MySQL query that I execute from a .php page SELECT

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.