Skip to content

Commit

Permalink
Performance improvements.
Browse files Browse the repository at this point in the history
* Serialize the column using specified coder before inserting
* Cache the ActiveRecord connection once per call to generate values SQL
* Call sequence_name last since it's more expensive
  • Loading branch information
sishen authored and zdennis committed Dec 14, 2012
1 parent ec2059e commit 1ae5122
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/activerecord-import/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,22 @@ def import_without_validations_or_callbacks( column_names, array_of_attributes,
# Returns SQL the VALUES for an INSERT statement given the passed in +columns+
# and +array_of_attributes+.
def values_sql_for_columns_and_attributes(columns, array_of_attributes) # :nodoc:
# connection gets called a *lot* in this high intensity loop.
# Reuse the same one w/in the loop, otherwise it would keep being re-retreived (= lots of time for large imports)
connection_memo = connection
array_of_attributes.map do |arr|
my_values = arr.each_with_index.map do |val,j|
column = columns[j]

if val.nil? && !sequence_name.blank? && column.name == primary_key
connection.next_value_for_sequence(sequence_name)
# be sure to query sequence_name *last*, only if cheaper tests fail, because it's costly
if val.nil? && column.name == primary_key && !sequence_name.blank?
connection_memo.next_value_for_sequence(sequence_name)
else
connection.quote(column.type_cast(val), column)
if serialized_attributes.include?(column.name)
connection_memo.quote(serialized_attributes[column.name].dump(val), column)
else
connection_memo.quote(val, column)
end
end
end
"(#{my_values.join(',')})"
Expand Down

0 comments on commit 1ae5122

Please sign in to comment.