列表查询+n 条附属结果,请教怎么查询比较优化 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
zjttfs
V2EX    MySQL

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

  •  
  •   zjttfs 2021-09-22 14:53:17 +08:00 2092 次点击
    这是一个创建于 1547 天前的主题,其中的信息可能已经有所发展或是发生改变。

    例如

    article文章表

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

    comment评论表

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

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

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

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

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

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

    16 条回复    2021-09-22 16:00:43 +08:00
    dqzcwxb
        1
    dqzcwxb  
       2021-09-22 14:56:52 +08:00   1
    循环查询改为 in 查询后在代码中 join
    westoy
        2
    westoy  
       2021-09-22 15:00:30 +08:00   1
    不要搞复杂

    你现在这种其实 SQL Cache 命中率更高, 也更容易在后期做 k-v cache.........
    Bigglesworth
        3
    Bigglesworth  
       2021-09-22 15:00:49 +08:00   1
    减少次数,就 join 起来查?
    zjttfs
        4
    zjttfs  
    OP   2021-09-22 15:06:49 +08:00
    @dqzcwxb
    @Bigglesworth

    请教下怎么 join?
    文章的评论可能很多大于 3 条.
    没有 limit 限制,不是会查询很多出来吗?
    zjttfs
        5
    zjttfs  
    OP
       2021-09-22 15:07:07 +08:00
    @westoy

    尴尬.没看懂...
    zilongzixue
        6
    zilongzixue  
       2021-09-22 15:17:51 +08:00   1
    mybatis 一对多查询
    lin07hui
        7
    lin07hui  
       2021-09-22 15:23:04 +08:00   1
    orm 吧
    hingbong
        8
    hingbong  
       2021-09-22 15:33:28 +08:00   1
    ```
    val sqlStep = articles.map { article ->
    select * from comment where article_id = article1.id limit 3
    }.reduce { left, right ->
    left.union(right)
    }

    sqlStep.fetch()
    ```
    我日常用 kotlin + jooq,这个需求大概就是这个思路查询
    hingbong
        9
    hingbong  
       2021-09-22 15:34:15 +08:00
    ```
    val sqlStep = articles.map { article ->
    select * from comment where article_id = article.id limit 3
    }.reduce { left, right ->
    left.union(right)
    }

    sqlStep.fetch()
    ```
    手误
    tangtj
        10
    tangtj  
       2021-09-22 15:35:56 +08:00   1
    我觉得你的逻辑是 ok 的。1 + n 查询没啥问题,评论的查询也很方便上缓存。
    dqzcwxb
        11
    dqzcwxb  
       2021-09-22 15:50:25 +08:00
    @zjttfs #4 那可能没法用 in 查询,只能串行改并行提升效率了.建议使用 Completablefuture 这样也方便做 cache
    wolfie
        12
    wolfie  
       2021-09-22 15:52:04 +08:00   1
    select from comment where id = 1 order by id desc limit 3
    union all
    select from comment where id = 2 order by id desc limit 3

    语法可能错误,用子查询包一下
    zjttfs
        13
    zjttfs  
    OP
       2021-09-22 15:53:10 +08:00
    @westoy

    刚才忙没太细看,再看了一次. 懂您的意思了.

    @tangtj

    好的. 感谢.
    zjttfs
        14
    zjttfs  
    OP
       2021-09-22 15:55:00 +08:00
    @zilongzixue

    我查下资料.谢谢
    zjttfs
        15
    zjttfs  
    OP
       2021-09-22 15:57:28 +08:00
    @wolfie

    感谢
    zzfer
        16
    zzfer  
       2021-09-22 16:00:43 +08:00
    mybatis 一对多标签 collection 可以限制查三条。但这样其实效果和你循环查询一样,只不过在一次连接查完。限制十条数据还行,查的多了速度一样很慢
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2994 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 13:16 PVG 21:16 LAX 05:16 JFK 08:16
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86