From 0fc4686643765a20e5caf06211b6ca2eb0a00770 Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 29 Sep 2023 01:49:35 +0900 Subject: [PATCH] Use exists query instead of count in clean-duplicate-history command. (#1015) * Use exists query instead of count in clean-duplicate_history command. * Improved changelog for #1015 --------- Co-authored-by: Anders <6058745+ddabble@users.noreply.github.com> --- AUTHORS.rst | 1 + CHANGES.rst | 1 + .../management/commands/clean_duplicate_history.py | 7 ++++--- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 02e644c43..fcf285f71 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -16,6 +16,7 @@ Authors - Alexander Anikeev - Amanda Ng (`AmandaCLNg `_) - Amartis Gladius (`Amartis `_) +- Anton Kulikov (`bigtimecriminal `_) - Ben Lawson (`blawson `_) - Benjamin Mampaey (`bmampaey `_) - Bheesham Persaud (`bheesham `_) diff --git a/CHANGES.rst b/CHANGES.rst index 559bc6976..0279c0dca 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -14,6 +14,7 @@ Unreleased now allowed; history records will still be created (gh-1248) - Added temporary requirement on ``asgiref>=3.6`` while the minimum required Django version is lower than 4.2 (gh-1261) +- Small performance optimization of the ``clean-duplicate_history`` command (gh-1015) 3.4.0 (2023-08-18) ------------------ diff --git a/simple_history/management/commands/clean_duplicate_history.py b/simple_history/management/commands/clean_duplicate_history.py index bbbd17e4c..786be6a11 100644 --- a/simple_history/management/commands/clean_duplicate_history.py +++ b/simple_history/management/commands/clean_duplicate_history.py @@ -74,9 +74,10 @@ def _process(self, to_process, date_back=None, dry_run=True): m_qs = history_model.objects if stop_date: m_qs = m_qs.filter(history_date__gte=stop_date) - found = m_qs.count() - self.log(f"{model} has {found} historical entries", 2) - if not found: + if self.verbosity >= 2: + found = m_qs.count() + self.log(f"{model} has {found} historical entries", 2) + if not m_qs.exists(): continue # Break apart the query so we can add additional filtering