Skip to content

Instantly share code, notes, and snippets.

@seejohnrun
Created February 24, 2011 16:49
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 seejohnrun/842433 to your computer and use it in GitHub Desktop.
Save seejohnrun/842433 to your computer and use it in GitHub Desktop.
Detect unnecessary indices in your database
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
@samuelcole
Copy link

elfcast!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment