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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:00:36+00:00 2026-06-13T19:00:36+00:00

I have a model- Link – that has many hits. What I’m trying to

  • 0

I have a model- Link – that has many hits. What I’m trying to do is aggregate data on hits per hour over time from a database table – hits – with columns year, month, day, and hour.

Everything is working fine, but when I run the code below, I end up with almost 1,000 queries on the database, which seems quite excessive.

#before iteration
@hits = @link.hits.group('year, month, day, hour')
  .order('year ASC, month ASC, day ASC, hour ASC')
  .select('year, month, day, hour, count(*) as hits')


#inside iteration

#Line breaks are included here for easy reading
hits_per_hour =
  @hits.where(
    :year => t.year, 
    :month => t.month,
    :day => t.day,
    :hour => t.hour
  ).map(&:hits).first || 0

Here t is a Time object that iterates in steps of 1 hour since the first hit a link received.

I would think that rails would store the query result instead of re-querying every time. I also haven’t been able to find anything about caching the query results or anything. Am I just missing something completely, or is this really the easiest way?

Here is a sample of what the queries look like (this is what I see in my logs in blocks of a thousand):

SELECT year, month, day, hour, count(*) as hits
FROM `hits`
WHERE
  `hits`.`link_id` = 1 AND
  `hits`.`year` = 2012 AND
  `hits`.`month` = 11 AND
  `hits`.`day` = 2 AND
  `hits`.`hour` = 14
GROUP BY year, month, day, hour
ORDER BY year ASC, month ASC, day ASC, hour ASC
  • 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-13T19:00:38+00:00Added an answer on June 13, 2026 at 7:00 pm

    Ok. So @hits is going to be an ActiveRecord::Relation object – a SQL query, essentially. I believe that because each iteration calls .where on it with different parameters, and thus alters the query, Rails has decided it has to rerun the query for each hour.

    The simplest fix is probably to ‘collapse’ the Relation into an array before iterating, and then use pure Ruby to select the results you want each time:

    @hits = @link.hits.group('year, month, day, hour')
      .order('year ASC, month ASC, day ASC, hour ASC')
      .select('year, month, day, hour, count(*) as hits').all
    

    and then:

    hits_per_hour = (@hits.select{|record| record.year == t.year && record.month == t.month && record.day == t.day && record.hour == t.hour}.map(&:hits).first || 0)
    

    I don’t think this is actually likely to be the best solution, though. Depending on exactly what data you need and what you’re doing with it, you should be able to get everything done in the database.

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

Sidebar

Related Questions

I have two Apps: A SrvLinkApp that has a Link Model (a sockjs connection
I have project model that has many tasks. Both project and tasks can have
I have model Article it has field title with some text that may contain
I have a model which contains a link to sql query that fetches information
I'm trying to build a database of urls(links). I have a Category model that
I have a model: ThreadParticipation I want to build a link that send the
I am trying to use Windows Workflow and have a model that looks similar
I have a model that has a property which is a collection. I can
I have a model that has a property like SomeUrl, it's an absolute url
I have Course's model and the course consist of several file (stored only link

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.