243634473
V2EX  ›  问与答

类似这样的查询语句,如果建索引能让 sql 变得高效,大神求解!

  •  
  •   243634473 · Jun 20, 2020 · 1595 views
    This topic created in 2183 days ago, the information mentioned may be changed or developed.

    SELECT id,title,date,type_id FROM table WHERE title='v2ex' GROUP BY type_id ORDER BY date DESC LIMIT 10;

    Supplement 1  ·  Jun 20, 2020
    SELECT type_id FROM table WHERE title='v2ex' GROUP BY type_id ORDER BY date DESC LIMIT 10;
    1 replies    2020-06-20 16:58:53 +08:00
    xiangyuecn
        1
    xiangyuecn  
       Jun 20, 2020
    不在 group by 里面的字段能在一个查询里面查询出来?

    无脑建复合索引:
    1. where 条件字段排前面,内容越离散的越排前面
    2. group 条件字段排中间,按 group 顺序
    3. order 字段排后面,order 就算上了索引里面,复杂条件下对排序目测很难起到作用,不过在索引中避免额外读盘还是蛮实用

    end.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2705 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 15:20 · PVG 23:20 · LAX 08:20 · JFK 11:20
    ♥ Do have faith in what you're doing.