Sean Fisher
Sean Fisher

Reputation: 625

Most Efficient Way to Get a Query of Posts based upon if a User Follows Them

Right now I have two tables for getting a timeline of posts that a user follows in MySQL, timeline and follows.

In the 'follows' table, there is the 'follows.follow_From' column, the 'follows.follow_To' columns to show where the follows are from.

In the timeline table, there is a 'timeline.post_From' column containing the author's user ID of the post.

What is the most efficient method of selecting all posts from 'timeline' only where the current user has a "follow" in the 'follows' table? I am currently using a MySQL array, but I don't see it as efficient. Thank you for reading this and your help!

Upvotes: 0

Views: 52

Answers (1)

Adam M-W
Adam M-W

Reputation: 3539

I'm not an expert and it might not be the most efficient but I'd guess:

  1. SELECT follow_To FROM follows WHERE follow_From = 'user-id'
  2. SELECT * FROM timeline WHERE timeline.post_From = (the results above)

e.g. SELECT * FROM timeline WHERE timeline.post_From IN (SELECT follow_To FROM follows WHERE follow_From = 'user-id')

Doing the other way round would definitely be slower because your processing more data then cutting it down. Cutting the data down first then look up is better.

Upvotes: 1

Related Questions