How do you reduce a long query running up mysql resources that requires you to run multiple searches?
I want to allow users to subscribe to other users, similar to twitter. So lets say john is subscribed 开发者_如何学JAVAto updates by tom, chris, david and 10 others.
When john checks his news feed, how can I display news from everyone he's subscribed too without running a long query like this:
select * from news where user='tom' and user='chris' and user='david' so on...
Doing it this way can end up resulting in 1 long query and will run up a lot of mysql resources. Looking for the correct method, or any alternatives that are more efficient.
You need a separate matching table that has a subscriber_id and a subscribed_id.. index the table on the subscriber_id.
Then you can
Select news.*
From news n
Join subscriptions s
On s.subscribed_id = n.user_id
Where subscriber_id = <user id of john>
your logic is wrong,
there won't be any matches for user = multiple user
logically should be
user in('tom', 'chris', 'david', ....);
however, using integer column with index is efficient
like what @Nick B suggest
精彩评论