2

列表查询+n 条附属结果,请教怎么查询比较优化

 2 years ago
source link: https://www.v2ex.com/t/803388
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  MySQL

列表查询+n 条附属结果,请教怎么查询比较优化

  zjttfs · 1 小时 29 分钟前 · 296 次点击

article文章表

字段 注释 id 文章主键 title 文章标题

comment评论表

字段 注释 id 评论主键 article_id 文章主键 content 评论内容

现查询文章列表 , 要求查询 10 篇文章,每篇文章附上最新的 3 条评论.

我现在的做法是查询出 10 篇文章.然后在代码里循环查询每篇文章的最新 3 条评论.

总计 1+10 次查询....

请教有什么更好的办法,减少查询次数吗?

子查询?或者再 mysql 中做函数 /存储过程?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK