优化SQL JOIN查询

时间:2013-12-30 12:30:49

标签: mysql sql

我有两个数据库表'friends'和'published_post'

`friends` --- contains 650K rows 
--------------------------------------------------------
| fb_id_user(bigint)  |   fb_id_friend(bigint) | fb_username(text) |
---------------------------------------------------------


`published_post` ----- contains 6K rows
-------------------------------------------------------------------------------
| id(primary_key)(int)  |   fb_id(bigint) | post(text) |  post_date(DATE_TIME) |
-------------------------------------------------------------------------------

假设用户的脸书ID为“12345”。我希望以“post_time”的降序获得他的朋友(form published_post table)给出的所有帖子(from friends table where fb_id_user = '12345')。 任何有关如何有效完成此查询的建议都会非常有用。

1 个答案:

答案 0 :(得分:3)

SELECT
    pp.*
FROM published_post pp
LEFT JOIN friends f ON f.fb_id_user = pp.fb_id
WHERE pp.fb_id_friend = '12345'
ORDER BY f.post_date DESC