user761479
user761479

Reputation: 145

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 to 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.

Upvotes: 0

Views: 61

Answers (2)

ajreal
ajreal

Reputation: 47321

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

Upvotes: 0

Nick B
Nick B

Reputation: 1101

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>

Upvotes: 4

Related Questions