Skip to content

are the fk_follows and fk_followables indexes needed? #97

@clairity

Description

@clairity

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions