Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve bulk import performance #1539

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,4 @@ The following is a list of much appreciated contributors:
* mpasternak (Michał Pasternak)
* nikatlas (Nikos Atlas)
* cocorocho (Erkan Çoban)
# Ptosiek (Antonin)
30 changes: 16 additions & 14 deletions import_export/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ def after_import_instance(self, instance, new, row_number=None, **kwargs):
"""
pass

def import_row(self, row, instance_loader, using_transactions=True, dry_run=False, raise_errors=False, **kwargs):
def import_row(self, row, instance_loader, using_transactions=True, dry_run=False, **kwargs):
matthewhegarty marked this conversation as resolved.
Show resolved Hide resolved
"""
Imports data from ``tablib.Dataset``. Refer to :doc:`import_workflow`
for a more complete description of the whole import process.
Expand Down Expand Up @@ -762,17 +762,6 @@ def import_row(self, row, instance_loader, using_transactions=True, dry_run=Fals
tb_info = traceback.format_exc()
row_result.errors.append(self.get_error_result_class()(e, tb_info, row))

if self._meta.use_bulk:
# persist a batch of rows
# because this is a batch, any exceptions are logged and not associated
# with a specific row
if len(self.create_instances) == self._meta.batch_size:
self.bulk_create(using_transactions, dry_run, raise_errors, batch_size=self._meta.batch_size)
if len(self.update_instances) == self._meta.batch_size:
self.bulk_update(using_transactions, dry_run, raise_errors, batch_size=self._meta.batch_size)
if len(self.delete_instances) == self._meta.batch_size:
self.bulk_delete(using_transactions, dry_run, raise_errors)

return row_result

def import_data(self, dataset, dry_run=False, raise_errors=False,
Expand Down Expand Up @@ -852,16 +841,29 @@ def import_data_inner(
result.add_dataset_headers(dataset.headers)

for i, row in enumerate(dataset.dict, 1):
with atomic_if_using_transaction(using_transactions, using=db_connection):
with atomic_if_using_transaction(using_transactions and not self._meta.use_bulk, using=db_connection):
matthewhegarty marked this conversation as resolved.
Show resolved Hide resolved
row_result = self.import_row(
row,
instance_loader,
using_transactions=using_transactions,
dry_run=dry_run,
row_number=i,
raise_errors=raise_errors,
**kwargs
)
if self._meta.use_bulk:
# persist a batch of rows
# because this is a batch, any exceptions are logged and not associated
# with a specific row
if len(self.create_instances) == self._meta.batch_size:
with atomic_if_using_transaction(using_transactions, using=db_connection):
self.bulk_create(using_transactions, dry_run, raise_errors, batch_size=self._meta.batch_size)
if len(self.update_instances) == self._meta.batch_size:
with atomic_if_using_transaction(using_transactions, using=db_connection):
self.bulk_update(using_transactions, dry_run, raise_errors, batch_size=self._meta.batch_size)
if len(self.delete_instances) == self._meta.batch_size:
with atomic_if_using_transaction(using_transactions, using=db_connection):
self.bulk_delete(using_transactions, dry_run, raise_errors)

result.increment_row_result_total(row_result)

if row_result.errors:
Expand Down