Created
February 24, 2011 16:49
-
-
Save seejohnrun/842433 to your computer and use it in GitHub Desktop.
Detect unnecessary indices in your database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'active_record' | |
# Look through each table and look for indexes that are subsets | |
# of each other. | |
ActiveRecord::Base.establish_connection(:database => 'elfcast_development', :adapter => 'mysql') | |
ActiveRecord::Base.connection.tables.each do |table| | |
# go through each index | |
indexes = ActiveRecord::Base.connection.indexes(table) | |
# look through all the indexes and look for subsets | |
indexes.each do |index| | |
indexes.each do |iindex| | |
next if index.name == iindex.name # skip ourself | |
if index.columns.slice(0, iindex.columns.count) == iindex.columns | |
puts "\e[031m`#{table}`.#{iindex.name}\e[0m is a left subset of \e[032m`#{table}`.#{index.name}\e[0m !!!" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
elfcast!