代码之家  ›  专栏  ›  技术社区  ›  calyxofheld

如果向多个列添加唯一性约束,是否还需要为各个列编制索引?

  •  2
  • calyxofheld  · 技术社区  · 7 年前

    如果我有桌子:

    create_table :blobs do |t|
     t.belongs_to :user, index: true
     t.belongs_to :item, index: true
     ...etc...
    end
    

    但我需要对这两个 :user :item 我应该 组合列的索引?或者这是多余的?

    add_index :blobs, [:user_id, :item_id], unique: true

    编辑:这不是一个重复的问题。这是关于当单个列本身也被索引时,添加多列约束是否是多余的。

    0 回复  |  直到 7 年前