Skip to content
Browse files

Performance improvements.

* 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...
1 parent ec2059e commit 1ae51226423b0566bce21390e5ad98deaf84694d @sishen sishen committed with Dec 14, 2012
Showing with 11 additions and 3 deletions.
  1. +11 −3 lib/activerecord-import/import.rb
View
14 lib/activerecord-import/import.rb
@@ -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(',')})"

0 comments on commit 1ae5122

Please sign in to comment.
Something went wrong with that request. Please try again.