Skip to content

Instantly share code, notes, and snippets.

@joshdvir
Created April 11, 2012 07:26
Show Gist options
  • Save joshdvir/2357573 to your computer and use it in GitHub Desktop.
Save joshdvir/2357573 to your computer and use it in GitHub Desktop.
Ruby optimize all tables in all DB's in mysql
require 'mysql'
con = Mysql.new('localhost','root', '')
rs = con.query('show databases')
databases = []
rs.each_hash do |h|
databases << h['Database']
end
databases.each do |db|
unless db == 'mysql' or db == 'performance_schema' or db == 'information_schema'
puts "on database: " + db.to_s
con = Mysql.new('localhost','root', '', db)
rs = con.query('show tables')
rs.each_hash do |t|
puts "on table: " + t['Tables_in_' + db.to_s]
rs = con.query('optimize table ' + t['Tables_in_' + db.to_s].to_s)
end
con.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment