This is a bit challenging but fun question. Consider having these tables
tweets
tweet_id | retweet_of_id | user_id
follow
user_id | followed_user_id
So we store each "retweet as a separate tweet" pointing to the original tweet's id (retweet_of_id
). This is because I want to have comments under each one separately.
If something is not a retweet then retweet_of_id
will be 0
.
How do I retrieve the following using MySQL efficiently?
- My own tweets
- All the original tweets (from users that I follow)
- And the first retweet (by a user I follow) of a tweet (from a user that I don't follow)
And that the result should a combination of both (in order) just like how twitter does it.
Please consider that there may be 1,000,000 tweets and we only need the most recent ones (e.g.: 10).
Here is an example (I'm user 1 and I follow user 2 & 3)
tweet_id | retweet_of_id | user_id
----------------------------------
1 0 4 <- EXCLUDE (I don't follow user 4)
2 0 2 <- INCLUDE (I follow user 2)
3 0 3 <- INCLUDE (I follow user 3)
4 1 2 <- INCLUDE (I follow user 2 & first RT)
5 1 3 <- EXCLUDE (I already have the first RT)
6 2 3 <- EXCLUDE (I already have the orignal)
7 0 1 <- INCLUDE (My own tweet)
So the final order should be these tweets: 7, 4, 3, 2
(starting with the most recent)