From e6d5f1f50de382b0d33b1574e89eb84d7ddcb303 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Wed, 16 Oct 2024 14:13:07 +0200 Subject: [PATCH] fix(unit): add some missing prefetch() calls The prefetch_bulk() and prefetch_full() do prefetch unit data, but not parent objects with assumption that these are typically called from translation.unit_set where such prefetch is not needed. But when it originates from Unit.objects, it needs to be added. --- docs/changes.rst | 1 + weblate/trans/autotranslate.py | 7 ++++++- weblate/trans/models/unit.py | 2 ++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/changes.rst b/docs/changes.rst index 7b3576fd3b73..028c26900404 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -10,6 +10,7 @@ Not yet released. **Bug fixes** * Update outdated plural definitions during the database migration. +* Reduced number of database queries when updating multiple strings. **Compatibility** diff --git a/weblate/trans/autotranslate.py b/weblate/trans/autotranslate.py index 67d618d42122..5e2d9df5bcd8 100644 --- a/weblate/trans/autotranslate.py +++ b/weblate/trans/autotranslate.py @@ -137,7 +137,12 @@ def process_others(self, source: int | None) -> None: .filter(source__in=translations.keys()) .values_list("id", flat=True) ) - units = Unit.objects.filter(pk__in=unit_ids).prefetch_bulk().select_for_update() + units = ( + Unit.objects.filter(pk__in=unit_ids) + .prefetch() + .prefetch_bulk() + .select_for_update() + ) self.progress_steps = len(units) for pos, unit in enumerate(units): diff --git a/weblate/trans/models/unit.py b/weblate/trans/models/unit.py index 6206c0a1b561..7c80c79fcefe 100644 --- a/weblate/trans/models/unit.py +++ b/weblate/trans/models/unit.py @@ -1475,6 +1475,7 @@ def variants(self) -> Iterable[Unit]: return [] return ( self.variant.unit_set.filter(translation=self.translation) + .prefetch() .prefetch_full() .order_by("context") ) @@ -1647,6 +1648,7 @@ def checksum(self): def same_source_units(self) -> UnitQuerySet: return ( Unit.objects.same(self) + .prefetch() .prefetch_full() .filter( translation__component__allow_translation_propagation=True,