From 99ab4c0ce350262c0ce17a6af1264c76a64d1227 Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Fri, 3 Dec 2021 15:04:47 -0500 Subject: [PATCH] perf: Use exists() instead of count() https://docs.djangoproject.com/en/3.2/topics/db/optimization/ --- process/management/commands/base/worker.py | 2 +- process/processors/compiler.py | 10 ++++------ process/processors/finisher.py | 10 +++------- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/process/management/commands/base/worker.py b/process/management/commands/base/worker.py index 885f53d2..d72161c2 100644 --- a/process/management/commands/base/worker.py +++ b/process/management/commands/base/worker.py @@ -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( diff --git a/process/processors/compiler.py b/process/processors/compiler.py index 2d1fc38a..6a1517cd 100644 --- a/process/processors/compiler.py +++ b/process/processors/compiler.py @@ -299,13 +299,13 @@ 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 @@ -313,9 +313,7 @@ def compilable(collection_id): 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) diff --git a/process/processors/finisher.py b/process/processors/finisher.py index 31332212..aeb43977 100644 --- a/process/processors/finisher.py +++ b/process/processors/finisher.py @@ -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( @@ -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)