Skip to content

Commit

Permalink
perf: Use exists() instead of count() https://docs.djangoproject.com/…
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Dec 3, 2021
1 parent 247ffc1 commit 99ab4c0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
2 changes: 1 addition & 1 deletion process/management/commands/base/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def _deleteStep(self, step_type=None, collection_id=None, collection_file_id=Non

processing_steps = processing_steps.filter(name=step_type)

if processing_steps.count() > 0:
if processing_steps.exists():
processing_steps.delete()
else:
self._warning(
Expand Down
10 changes: 4 additions & 6 deletions process/processors/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,23 +299,21 @@ def compilable(collection_id):
return True

if collection.store_end_at is not None:
processing_step_count = (
has_remaining_steps = (
ProcessingStep.objects.filter(collection_file__collection=collection.get_root_parent())
.filter(name=ProcessingStep.Types.LOAD)
.count()
.exists()
)

if processing_step_count == 0:
if not has_remaining_steps:
compiled_collection = collection.get_compiled_collection()
if compiled_collection.compilation_started:
# the compilation was already started
return False
else:
return True
else:
logger.debug(
"Load not finished yet for collection %s - remaining %s steps.", collection, processing_step_count
)
logger.debug("Load not finished yet for collection %s - >= 1 remaining steps.", collection)
return False
else:
logger.debug("Collection %s not completely stored yet. (store_end_at not set)", collection)
Expand Down
10 changes: 3 additions & 7 deletions process/processors/finisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def completable(collection_id):

return False

processing_step_count = ProcessingStep.objects.filter(collection=collection).count()
if processing_step_count == 0:
has_steps_remaining = ProcessingStep.objects.filter(collection=collection).exists()
if not has_steps_remaining:
real_files_count = CollectionFile.objects.filter(collection=collection).count()
if collection.expected_files_count and collection.expected_files_count > real_files_count:
logger.debug(
Expand All @@ -63,11 +63,7 @@ def completable(collection_id):

return True
else:
logger.debug(
"Processing not finished yet for collection %s - remaining %s steps.",
collection,
processing_step_count,
)
logger.debug("Processing not finished yet for collection %s - >= 1 remaining steps.", collection)
return False
else:
logger.debug("Collection %s not completely stored yet.", collection)
Expand Down

0 comments on commit 99ab4c0

Please sign in to comment.