SELECT CONCAT('optimize table ', table_schema, '.', table_name, ';') FROM information_schema.tables WHERE table_schema NOT IN ('information_schema','mysql') AND data_free > 0 ORDER BY table_schema, data_free DESC;