acts_as_follower icon indicating copy to clipboard operation
acts_as_follower copied to clipboard

are the `fk_follows` and `fk_followables` indexes needed?

Open clairity opened this issue 8 years ago • 0 comments

i am going through my codebase and cleaning up extraneous indexes and wondered if the fk_follows and fk_followables indexes are needed? i ask because the migration already creates two indexes

  1. index_follows_on_followable_type_and_followable_id
  2. index_follows_on_follower_type_and_follower_id

as defined implicitly by these polymorphic references:

class ActsAsFollowerMigration < ActiveRecord::Migration
  def self.up
    create_table :follows, force: true do |t|
      t.references  :followable,  polymorphic: true,  null: false
      t.references  :follower,    polymorphic: true,  null: false
     
      ...

    end
  end

  ...
     
end

so are these two additional indexes required?

class ActsAsFollowerMigration < ActiveRecord::Migration
  def self.up
    create_table :follows, force: true do |t|
      ...
    end

    add_index :follows, ["follower_id", "follower_type"],     name: "fk_follows"
    add_index :follows, ["followable_id", "followable_type"], name: "fk_followables"
  end

  ...

end

the main difference is the order of the index elements:

  1. [:follower_type, :follower_id] rather than [:follower_id, :follower_type]
  2. [:followable_type, :followable_id] rather than [:followable_id, :followable_type]

if these two defined indexes are indeed required, then are the implicitly created ones needed? not a huge deal, but i'd thought i'd ask since i couldn't find an answer elsewhere. thanks!

clairity avatar May 18 '17 06:05 clairity