Skip to content

Instantly share code, notes, and snippets.

@ruprict
Created November 9, 2012 16:21
Show Gist options
  • Save ruprict/4046627 to your computer and use it in GitHub Desktop.
Save ruprict/4046627 to your computer and use it in GitHub Desktop.
def insert_big_arsed_table(array_of_values, created_date)
sql = "COPY big_arsed_table (other_table_id, other_id, other_type, created_at, updated_at) from STDIN WITH DELIMITER AS ',' "
ActiveRecord::Base.connection_pool.with_connection do |conn|
rc = conn.raw_connection
begin
rc.exec(sql)
loop_time = Benchmark.ms do
array_of_values.each do |u|
# Spaces are very important here
line = "#{id},#{u.prop1},#{u.prop2},#{u.prop3},#{created_date}\n"
rc.put_copy_data(line)
end
end
Rails.logger.info "** Elapsed Loop time: #{loop_time}"
ensure
rc.put_copy_end
result_time = Benchmark.ms do
while res = rc.get_result # This takes FOREVA
end
end
Rails.logger.info "** Elapsed Result time: #{result_time}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment