sniperking1234
V2EX  ›  问与答

mysql 中普通索引和唯一索引可以设置在同一列吗

  •  
  •   sniperking1234 · May 14, 2020 · 1829 views
    This topic created in 2231 days ago, the information mentioned may be changed or developed.

    image.png 比如上面的例子,namespace 这一列会经常当作条件被查询,所以设置了普通的 btree 索引。

    namespace 和 name 因为业务上的逻辑,组合起来是唯一的,所以设置了组合的唯一索引。

    这样设置的话会有什么不好的影响吗?

    3 replies    2020-05-14 10:43:54 +08:00
    DoubleShut
        1
    DoubleShut  
       May 14, 2020   ❤️ 2
    感觉没必要,根据组合索引的最左前缀原则,单查 namespace 也会使用索引 uni 。
    dddd1919
        2
    dddd1919  
       May 14, 2020   ❤️ 1
    冗余,没必要,直接设置一个联合索引就可以了,两种业务查询都可以命中
    sniperking1234
        3
    sniperking1234  
    OP
       May 14, 2020
    @DoubleShut
    @dddd1919
    多谢解答
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2767 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 15:22 · PVG 23:22 · LAX 08:22 · JFK 11:22
    ♥ Do have faith in what you're doing.