Newyorkcity
V2EX  ›  问与答

mysql:请问列 a 有普通索引,列 b,c 是一个组合索引. where a = xx and b= xx and c=xx 会用上索引吗?

  •  
  •   Newyorkcity · Mar 26, 2020 · 2432 views
    This topic created in 2414 days ago, the information mentioned may be changed or developed.
    有且仅有这两个索引.
    如果列 a 的索引变为主键索引或者唯一索引,会有不同吗?

    谢谢
    7 replies  •  2020-03-26 11:29:32 +08:00
    lhx2008
    lhx2008
    Mar 26, 2020 via Android
    1
    只会用 a 索引,没不同
    lhx2008
    lhx2008
    Mar 26, 2020 via Android❤️1
    2
    看错了,应该要看优化器怎么选用哪一个
    ylsc633
    ylsc633
    Mar 26, 2020❤️1
    3
    题目里 是看优化器

    详情里 a 变成主键 走 a
    fareware
    fareware
    Mar 26, 2020
    4
    explain
    Jooooooooo
    Jooooooooo
    Mar 26, 2020
    6
    看优化器

    还有可能走两个索引的交叉(但并不一定会更快)
    auxox
    auxox
    Mar 26, 2020❤️1
    7
    主要看优化器选择哪一条执行路径。a 和 b,c 这两个索引哪个散粒度更高,优化器大概率会选择用哪一个。但是还要考虑顺序 IO 和随机 IO 的问题,如果索引不是聚簇的,那么 rdbms 在检索数据的时候需要回表,造成大量的随机 IO,极端情况下还不如全表扫描来得快
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5170 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 06:31 · PVG 14:31 · LAX 22:31 · JFK 01:31
    ♥ Do have faith in what you're doing.