我需要知道是否有优化这种数据库模型的最佳方法:
这是我的桌子:
[category]
idCategory
name
[postCategory] (a post can be in more than 1 category)
idCategory
idPost
[post]
idPost
post
[comment]
idComment
idPost
inputDate
comment
我将不得不显示特定时间范围内特定类别的所有帖子(时间来自“评论”)。时间范围是固定的(1 天、1 周、1 个月、1 年)。这是我带来的:
SELECT DISTINCT(post.idPost), post.post
from post
INNER JOIN comment ON post.idPost = comment.idPost
INNER JOIN postCategory ON postCategory.idPost = post.idPost
WHERE postCategory.idCategory = <myCategoryId>
AND comment.inputDate >= <today - time range>
假设我希望支持 10k 帖子和 500k 评论......有没有办法优化这个(除了使用索引)?您会使用存储过程、带有临时表的查询、在某处添加“预先计算”的字段...?
非常感谢!:)