Skip to content

Instantly share code, notes, and snippets.

@mmcdermott
mmcdermott / potentially_missing_indexes.rb
Created July 9, 2012 14:49 — forked from tomafro/example output
Run this in the rails console to list find missing indices.
c = ActiveRecord::Base.connection
str = ""
c.tables.collect do |t|
columns = c.columns(t).collect(&:name).select {|x| x.ends_with?("_id" || x.ends_with("_type"))}
indexed = c.indexes(t).collect(&:columns).flatten.uniq
unindexed = columns - indexed
str += "\n#{t}: #{unindexed.join(", ")}" unless unindexed.empty?
end