Skip to content

Instantly share code, notes, and snippets.

@ethangunderson
Created September 3, 2014 14:39
Show Gist options
  • Save ethangunderson/1d08b4cef049266727e6 to your computer and use it in GitHub Desktop.
Save ethangunderson/1d08b4cef049266727e6 to your computer and use it in GitHub Desktop.
Find potential missing foreign key indexes
ActiveRecord::Base.logger = nil
connection = ActiveRecord::Base.connection; nil
connection.tables.each do |table|
columns = connection.columns(table).map(&:name).select {|x| x.ends_with?("_id")}
indexed_columns = connection.indexes(table).map(&:columns).flatten.uniq
unindexed = columns - indexed_columns
unless unindexed.empty?
puts "#{table}: #{unindexed.join(", ")}"
end
nil
end; nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment