如何优化此查询以快速加载朋友

时间:2013-01-29 11:05:32

标签: php mysql

这是我已经定义查询的附加代码。

SELECT
  is_facebook_user,
  fname,
  lname,
  profilenam,
  mem_id,
  gender,
  profile_type,
  photo_thumb,
  photo_b_thumb
FROM members
WHERE mem_id IN(SELECT DISTINCT
          mem_id            AS frnd
        FROM network
        WHERE mem_id = '$userId'
             OR frd_id = '$userId')
ORDER BY profilenam;

这里我想尽快收到朋友。我也在mem_id和frd_id上应用了索引。但仍有2500名朋友需要很长时间,而且功能的响应打印速度非常慢。

由于 火花 find code link here

2 个答案:

答案 0 :(得分:1)

加入怎么样?

SELECT
  m.is_facebook_user,
  m.fname,
  m.lname,
  m.profilenam,
  m.mem_id,
  m.gender,
  m.profile_type,
  m.photo_thumb,
  m.photo_b_thumb
FROM members m
  join network n
    on m.mem_id = n.mem_id
WHERE n.mem_id = '$userId'
     or n.frd_id = '$userId'
ORDER BY m.profilenam;

答案 1 :(得分:0)

您可以使用此类联接

SELECT
  is_facebook_user,
  fname,
  lname,
  profilenam,
  mem_id,
  gender,
  profile_type,
  photo_thumb,
  photo_b_thumb
FROM members
  left join (select
           distince   mem_id
         from network
         where mem_id = '$userId'
          OR frd_id = '$userId') as e
    on e.mem_id = members.mem_id