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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:25:57+00:00 2026-05-31T21:25:57+00:00

When I make queries across my database – I’m having issues with scaling where

  • 0

When I make queries across my database – I’m having issues with scaling where if a user has 10k friends – the same query I use for users with 2k friends doesn’t scale up, meaning it takes a very long time to process.

The typical situation is that when a user’s friend count reaches a certain threshold, I am having to end up using STRAIGHT_JOIN to pull the query, however I need to write conditional statement to first see how many friend and then go from there. The more data, the slower the query to gets.

Is there a better way to scale up your queries via MySQL so they work at the same rate no matter how much data is being generated or am I living in a fantasy world?

EDIT: Query listed below:

SELECT photos.photo_id, count(distinct photo_views.ip_address) as total_count
FROM photos
INNER JOIN friends on friends.friend_id = photos.user_id
INNER JOIN photo_views on photos.photo_id = photo_views.photo_id
WHERE friends.user_id = 1 and friends.approved = 1 
and photos.created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)
GROUP by photos.photo_id
ORDER by total_count desc

Indexes on photos are:

[user_id, created_at, photo_id], [user_id], [photo_id] - PRIMARY

Indexes on friends are:

[user_id, approved], [friend_id], [user_id, friend_id] - PRIMARY

Index on photo_views are:

[photo_id] - PRIMARY
  • 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-31T21:25:58+00:00Added an answer on May 31, 2026 at 9:25 pm

    I would start with your Friends FIRST as your qualifier so it doesn’t try to look at the Photos as the basis which would cover everyone… Since you are doing an (Inner) join, I would go with the straight_join such as:

    SELECT STRAIGHT_JOIN
          P.Photo_ID, COUNT( distinct PV.IP_Address ) as Total_Count
       FROM
          Friends F
             JOIN Photos P
                ON F.Friend_ID = P.User_ID
                AND P.Created_At >= date_Sub( now(), Interval 30 day )
                JOIN Photo_Views PV
                   ON P.Photo_ID = PV.Photo_ID
       WHERE
              F.User_ID = 1
          AND F.Approved = 1
       GROUP BY 
          P.Photo_ID
       ORDER BY
          total_Count DESC
    

    This way, you are starting ONLY with those friends for the one user who are approved. From that, join to the photos of the friends AND the date is qualified… from that, get how many hits per the views table

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

Sidebar

Related Questions

How can i make multiple, composite query in oracle? for example this several queries
I need to make two queries on two different tables and the data isn't
Is it possible to make efficient queries that use the complete regular expression feature
I use functions like the following to make temporary tables out of crosstabs queries.
I am learning mysql join queries. To pratice, I decided to make tables for
To make it short: hibernate doesn't support projections and query by example? I found
I'm trying to optimize SQL queries in Akonadi and came across the following problem
In our last episode ( How I constructed a cross database query in MySQL
Confused regarding best methodology to make website usable and consistent across all devices -
I have multiple select statements from different tables on the same database. I was

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.