Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: gem/plugin for finding missing indexes?

Is there a gem or plugin like https://github.com/eladmeidar/rails_indexes that works for rails3?

like image 807
Mexxer Avatar asked Aug 23 '11 10:08

Mexxer


2 Answers

There's a fork of rails_indexes that has been updated to work with Rails 3 and Ruby 1.9

https://github.com/plentz/lol_dba

like image 98
Marc L Avatar answered Nov 13 '22 00:11

Marc L


You can paste following code in your console to know the missing foreign key indexes. This, however, is not as capable of the plugin that you refer to. It only searches for rails style foreign keys that have an _id at the end of their column name.

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

Source

like image 15
Waseem Avatar answered Nov 12 '22 22:11

Waseem