query optimization

Posted by Gaurav on Stack Overflow See other posts from Stack Overflow or by Gaurav
Published on 2010-02-23T10:05:39Z Indexed on 2010/06/13 18:02 UTC
Read the original article Hit count: 301

Filed under:
|
|

I have a query of the form

SELECT uid1,uid2 FROM friend WHERE uid1 IN (SELECT uid2 FROM friend WHERE uid1='.$user_id.') and uid2 IN (SELECT uid2 FROM friend WHERE uid1='.$user_id.')

The problem now is that the nested query

SELECT uid2 FROM friend WHERE uid1='.$user_id.'

returns a very large number of ids(approx. 5000).

The table structure of the friend table is uid1(int), uid2(int). This table is used to determine whether two users are linked together as friends.

Any workaround? Can I write the query in a different way? Or is there some other way to solve this issue. I'm sure I am not the first person to face such a problem.

Any help would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about sql

Related posts about query