› 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 · Sep 22, 2021 · 2591 views
    This topic created in 1868 days ago, the information mentioned may be changed or developed.

    例如

    article文章表

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

    comment评论表

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

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

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

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

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

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

    16 replies  •  2021-09-22 16:00:43 +08:00
    dqzcwxb
    dqzcwxb
    Sep 22, 2021❤️1
    1
    循环查询改为 in 查询后在代码中 join
    westoy
    westoy
    Sep 22, 2021❤️1
    2
    不要搞复杂

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

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

    尴尬.没看懂...
    zilongzixue
    zilongzixue
    Sep 22, 2021❤️1
    6
    mybatis 一对多查询
    lin07hui
    lin07hui
    Sep 22, 2021❤️1
    7
    orm 吧
    hingbong
    hingbong
    Sep 22, 2021❤️1
    8
    ```
    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
    hingbong
    Sep 22, 2021
    9
    ```
    val sqlStep = articles.map { article ->
    select * from comment where article_id = article.id limit 3
    }.reduce { left, right ->
    left.union(right)
    }

    sqlStep.fetch()
    ```
    手误
    tangtj
    tangtj
    Sep 22, 2021❤️1
    10
    我觉得你的逻辑是 ok 的。1 + n 查询没啥问题,评论的查询也很方便上缓存。
    dqzcwxb
    dqzcwxb
    Sep 22, 2021
    11
    @zjttfs #4 那可能没法用 in 查询,只能串行改并行提升效率了.建议使用 Completablefuture 这样也方便做 cache
    wolfie
    wolfie
    Sep 22, 2021❤️1
    12
    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
    zjttfs
    OP
    Sep 22, 2021
    13
    @westoy

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

    @tangtj

    好的. 感谢.
    zjttfs
    zjttfs
    OP
    Sep 22, 2021
    14
    @zilongzixue

    我查下资料.谢谢
    zjttfs
    zjttfs
    OP
    Sep 22, 2021
    15
    @wolfie

    感谢
    zzfer
    zzfer
    Sep 22, 2021
    16
    mybatis 一对多标签 collection 可以限制查三条。但这样其实效果和你循环查询一样,只不过在一次连接查完。限制十条数据还行,查的多了速度一样很慢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1567 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 18:31 · PVG 02:31 · LAX 10:31 · JFK 13:31
    ♥ Do have faith in what you're doing.