【问题标题】:Does HABTM jointable needs index?HABTM 连接表是否需要索引?
【发布时间】:2013-08-07 11:14:56
【问题描述】:

HABTM 连接表是否需要添加索引?我使用 t.belongs_to(又名 t.references)。这是代码

class CreateCohortsUsersTable < ActiveRecord::Migration

  def change
    create_table :cohorts_users, :id => false do |t|
      t.belongs_to :cohort
      t.belongs_to :user
    end
    add_index :cohorts_users, :cohort_id # Do I need this?
    add_index :cohorts_users, :user_id  # Do I need this?
  end
end

【问题讨论】:

    标签: ruby-on-rails database ruby-on-rails-3 indexing migration


    【解决方案1】:

    在两列上添加唯一索引通常是个好主意:

    add_index :cohorts_users, [:cohort_id, :user_id], unique: true
    

    【讨论】:

      猜你喜欢
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-13
      • 2011-06-15
      相关资源
      最近更新 更多