petelin
V2EX  ›  问与答

请教数据库建立索引

  •  
  •   petelin · Sep 8, 2017 · 1692 views
    This topic created in 3194 days ago, the information mentioned may be changed or developed.

    有两个列, status(5 中状态), create_time

    查询的时候 select * from - where status=1 and create_time > '-';

    因为 status = 1 的记录比较少,所以想 status 在前(这样直接选出几天记录然后在筛选), 但是书上说应该给选择性多的列建立索引, 像性别这种就不适合.

    请问, 索引顺序是 status 在前好, 还是 create_time 在前好?

    2 replies    2017-09-14 18:09:52 +08:00
    mm163
        1
    mm163  
       Sep 8, 2017
    create_time 建索引,查询的时候 create_time 在前。
    status 建索引没意义。
    petelin
        2
    petelin  
    OP
       Sep 14, 2017
    @mm163 我举个例子吧, 如果有 100 万个 status 为 0 的记录, 有一条 stattus 为 1 的记录, 那么查找 status 为 1 的记录能不能直接命中呢? 如果能的话, 那性能提升就很明显.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2806 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 12:15 · PVG 20:15 · LAX 05:15 · JFK 08:15
    ♥ Do have faith in what you're doing.