Need two indexes on a HABTM join table?

前端 未结 3 1682
忘了有多久
忘了有多久 2021-01-31 15:37

A simple has_and_belongs_to_many association:

Person has_and_belongs_to_many :products
Product has_and_belongs_to_many :persons

Ar

3条回答
  •  后悔当初
    2021-01-31 16:17

    You just need one, unless you are doing unique

    add_index :person_products, :person_id
    add_index :person_products, :product_id
    

    Or for an index on both columns

    add_index :person_products, [:person_id, :product_id]
    

    This will help performance when querying on those columns on the database. It will depend on your query if it includes both columns or just one.

    http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_index

提交回复
热议问题