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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:06:55+00:00 2026-06-01T03:06:55+00:00

I’m having a bit of an issue with a query and scaling that query

  • 0

I’m having a bit of an issue with a query and scaling that query for perfomance with users with high amount of friends. The goal of query is grab the top “activities’ performed by your friends in the last 30 days. Here is my query:

SELECT a.activity_id, b.activity_name, count(a.activity_id) as total_count
FROM friends as f
INNER JOIN activities as a on (a.user_id = f.friend_id 
and a.created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)
INNER JOIN activity as b on a.activity_id = b.activity_id
WHERE f.user_id = 1 and f.is_approved = 1
GROUP by a.activity_id
ORDER by total_count DESC
LIMIT 5

This query takes like 25 seconds to run for all users no matter how big or small their friends graph is. Indexes are below:

Table: activities
PRIMARY: [act_id] Other: [activity_id, user_id], [user_id, created_at], [created_at]

Table: friends
PRIMARY: [user_id, friend_id] Other: [user_id, is_approved], [friend_id]

Table: activity:
PRIMARY: [activity_id]

Any help would be greatly appreciated.

UPDATE: Here is the explain

id  select_type     table   key             key_len         ref             rows    Extra
1   SIMPLE   F  ref     friend_lookup   5   const,const     795     Using temporary; Using filesort
1   SIMPLE A    ref     user_id         4   F.friend_id     58      Using where
1   SIMPLE    B     eq_ref  PRIMARY         4   P.activty_id    1       Using where
  • 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-01T03:06:56+00:00Added an answer on June 1, 2026 at 3:06 am

    Robin is correct on the date field. If you are using a function, it will have to compute that for however many entries its scanning against. The way I have it below uses MySQL variables. I calculate it ONCE into an @StartDate and use THAT value for the join clause.

    The only additional thing I changed was adding the “STRAIGHT_JOIN” clause. On many instances, I’ve found that it has helped myself and others to optimize the query. It prevents MySQL from trying to interpret the query in another way by possibly looking at the Activity table first since its a smaller file and then back-linking from that one. “STRAIGHT_JOIN” tells the optimizer to do it in the order you’ve listed.

    SELECT STRAIGHT_JOIN
          a.activity_id, 
          b.activity_name, 
          count(a.activity_id) as total_count
       FROM 
          ( select @StartDate := date_Sub( now(), interval 30 day ) sqlvars,
          friends as f
             INNER JOIN activities as a 
                on a.user_id = f.friend_id 
               and a.created_at >= @StartDate
             INNER JOIN activity as b 
                on a.activity_id = b.activity_id
       WHERE 
              f.user_id = 1 
          and f.is_approved = 1
       GROUP by 
          a.activity_id
       ORDER by 
          total_count DESC
       LIMIT 5
    

    Per feedback

    That being the case, and having this “rolling 30 days ago” cycle, I would then resort to a nightly table creation that is nothing but a creation by user ID, activity and count and query from that instead…

    create table DailyRollupActivity
    select a.user_id,
           a.activity_id,
           count(*) total_count
       from
          ( select @StartDate := date_Sub( now(), interval 30 day ) sqlvars,
          Activities a
       where
          a.created_at >= @StartDate
       group by
          a.User_ID,
          a.Activity_ID
    

    Ensure you have an index on this daily aggregate table by the ( user ID and total count ) then query directly to this based on the friend ID ordered by total_count descending and limit 5. Small price to pay to have a nightly trigger / event / script to be run to create this ONCE. How critical is it to see activity for the current date too. Is the activity that drastic that one day activity would skew what you otherwise want to present to the user?

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT

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.