繁体   English   中英

优化查询:选择关注者的帖子

[英]Optimize query: select followings' posts

我正在尝试使用Twitter等追踪系统构建简单的社交媒体。

我有桌子

tb_follow
    id 
    account1_id 
    account2_id

tb_post 
    id 
    account_id 
    created_at 
    message 

我有这个查询来选择我关注的用户的最新帖子。

SELECT p.message
FROM tb_post as p
JOIN tb_follow as f
ON f.account1_id = 123 and f.account2_id = p.account_id 
ORDER BY p.created_at DESC, p.id DESC 
LIMIT 20

可以以某种方式优化此查询吗?

我应该索引哪些列?

对我来说,它看起来很好,而且由于执行inner join您是否具有join on子句中的条件或where条件相同。 您应该在列f.account2_idp.account_id上创建索引,因为这两个都涉及f.account2_id = p.account_id条件f.account2_id = p.account_id ,并且在f.account1_id上也具有索引,因为它们已包含在过滤器中f.account1_id = 123

SELECT p.message
FROM tb_post as p
JOIN tb_follow as f
ON f.account1_id = 123 and f.account2_id = p.account_id 
ORDER BY p.created_at DESC, p.id DESC 
LIMIT 20

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM