To establish connection to MySQL database and to handle huge databases in an efficient way, try with the following type of code:
require 'rubygems'
require 'mysql'
db_connection = Mysql.new('localhost', 'mysql_username', 'mysql_passwd', 'database_name')
user_result_set = db_connection.query('SELECT id, name FROM users ORDER BY id ASC')
user_result_set.each_hash { |user|
new_sql = "SELECT id FROM profiles WHERE (user_id = '#{user['id']}' "
profile_result_set = db_connection.query(new_sql)
profile_result_set.each_hash { |profile|
db_connection.query("INSERT INTO user_profiles(user_id, profile_id) VALUES (#{user['id']}, #{profile['id']})")
}
puts "PID: #{user['id']} - #{Time.now} updated"
}
db_connection.close