Skip to content

Instantly share code, notes, and snippets.

@raphaelcosta
Created August 11, 2010 16:43
Show Gist options
  • Save raphaelcosta/519289 to your computer and use it in GitHub Desktop.
Save raphaelcosta/519289 to your computer and use it in GitHub Desktop.
class Import
require 'activerecord-import/mysql'
def self.import_test
names = []
models =[]
1000.times do
name = Faker::Name.name
names.push [name]
models.push User.new :name => name
end
fields = [:name]
t1 = Time.now
User.import fields,names, :validate => false
puts "AR-Import method without models and validate#{Time.now - t1}"
t1 = Time.now
result = User.import models, :validate => false
puts "AR-Import method with models and without validate#{Time.now - t1}"
t1 = Time.now
for user in models
user.save(:validate => false)
end
puts "AR save method without validate#{Time.now - t1}"
ar_con_config = ActiveRecord::Base.connection.instance_variable_get(:@config)
conn = DBI.connect("DBI:Mysql:#{ar_con_config[:database]}:#{ar_con_config[:host]}", ar_con_config[:username], ar_con_config[:password])
conn.execute(" SET NAMES 'utf8'")
t1 = Time.now
conn['AutoCommit'] = false
s = conn.prepare( "INSERT INTO users(name)VALUES (?)" )
models.each do |user|
s.execute(user.name)
end
s.finish
conn.commit
puts "PreparedStatement with DBI #{Time.now - t1}"
conn.disconnect
end
end
@raphaelcosta
Copy link
Author

AR-Import method without models and validate0.157812
AR-Import method with models and without validate0.224819
AR save method without validate6.203773
PreparedStatement with DBI 0.400432

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