我必须在 Rails 中一次将 25000 条记录添加到数据库中。我也必须验证它们。
这是我现在拥有的:
# controller create action
def create
emails = params[:emails][:list].split("\r\n")
@created_count = 0
@rejected_count = 0
inserts = []
emails.each do |email|
@email = Email.new(:email => email)
if @email.valid?
@created_count += 1
inserts.push "('#{email}', '#{Date.today}', '#{Date.today}')"
else
@rejected_count += 1
end
end
return if emails.empty?
sql = "INSERT INTO `emails` (`email`, `updated_at`, `created_at`) VALUES #{inserts.join(", ")}"
Email.connection.execute(sql) unless inserts.empty?
redirect_to new_email_path, :notice => "Successfuly created #{@created_count} emails, rejected #{@rejected_count}"
end
现在很慢,由于超时,无法添加这么多的记录。
有任何想法吗?我正在使用mysql。