Skip to content

Instantly share code, notes, and snippets.

@aonemd
Last active September 21, 2016 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aonemd/b8f4ec2a0f4a0505883fc824813fcbb8 to your computer and use it in GitHub Desktop.
Save aonemd/b8f4ec2a0f4a0505883fc824813fcbb8 to your computer and use it in GitHub Desktop.
Check missing indices on foreign keys in your tables
# https://tomafro.net/2009/09/quickly-list-missing-foreign-key-indexes
desc "Check missing indices on foreign keys in your tables"
task missing_indices: :environment do
c = ActiveRecord::Base.connection
c.tables.collect do |t|
columns = c.columns(t).collect(&:name).select {|x| x.ends_with?("_id" || x.ends_with("_type"))}
indexed_columns = c.indexes(t).collect(&:columns).flatten.uniq
unindexed = columns - indexed_columns
unless unindexed.empty?
puts "#{t}: #{unindexed.join(", ")}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment