[英]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_id
和p.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.