Skip to content

Commit 6569f5f

Browse files
authored
Try reverting prefetch changes for project/version listing views (#11621)
* Revert "Prefetch build and project on version list (#11616)" This reverts commit e5f8092. * Revert "Add support for successful build prefetch (#11613)" This reverts commit a6e1fde. * Don't use project prefetching for now * Still rename model method, without prefetch * Add comment back
1 parent 6680d0e commit 6569f5f

File tree

5 files changed

+10
-69
lines changed

5 files changed

+10
-69
lines changed

readthedocs/builds/models.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,6 @@ class Meta:
207207
unique_together = [("project", "slug")]
208208
ordering = ["-verbose_name"]
209209

210-
# Property used for prefetching version related fields
211-
LATEST_BUILD_CACHE = "_latest_build"
212-
213210
def __str__(self):
214211
return self.verbose_name
215212

@@ -300,13 +297,6 @@ def last_build(self):
300297

301298
@property
302299
def latest_build(self):
303-
# Check if there is `_latest_build` prefetch in the Queryset.
304-
# Used for database optimization.
305-
if hasattr(self, self.LATEST_BUILD_CACHE):
306-
if latest_build := getattr(self, self.LATEST_BUILD_CACHE):
307-
return latest_build[0]
308-
return None
309-
310300
return self.builds.order_by("-date").first()
311301

312302
@property

readthedocs/builds/querysets.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import structlog
55
from django.db import models
6-
from django.db.models import OuterRef, Prefetch, Q, Subquery
6+
from django.db.models import Q
77
from django.utils import timezone
88

99
from readthedocs.builds.constants import (
@@ -141,30 +141,6 @@ def for_reindex(self):
141141
.distinct()
142142
)
143143

144-
def prefetch_subquery(self):
145-
"""
146-
Prefetch related objects via subquery for each version.
147-
148-
.. note::
149-
150-
This should come after any filtering.
151-
"""
152-
from readthedocs.builds.models import Build
153-
154-
# Prefetch the latest build for each project.
155-
subquery_builds = Subquery(
156-
Build.internal.filter(version=OuterRef("version_id"))
157-
.order_by("-date")
158-
.values_list("id", flat=True)[:1]
159-
)
160-
prefetch_builds = Prefetch(
161-
"builds",
162-
Build.internal.filter(pk__in=subquery_builds),
163-
to_attr=self.model.LATEST_BUILD_CACHE,
164-
)
165-
166-
return self.prefetch_related(prefetch_builds)
167-
168144

169145
class VersionQuerySet(SettingsOverrideObject):
170146
_default_class = VersionQuerySetBase

readthedocs/projects/models.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,6 @@ class Project(models.Model):
575575

576576
# Property used for storing the latest build for a project when prefetching
577577
LATEST_BUILD_CACHE = "_latest_build"
578-
LATEST_SUCCESSFUL_BUILD_CACHE = "_latest_successful_build"
579578

580579
class Meta:
581580
ordering = ("slug",)
@@ -921,13 +920,6 @@ def conf_dir(self, version=LATEST):
921920

922921
@property
923922
def has_good_build(self):
924-
# Check if there is `_latest_successful_build` attribute in the Queryset.
925-
# Used for database optimization.
926-
if hasattr(self, self.LATEST_SUCCESSFUL_BUILD_CACHE):
927-
if build_successful := getattr(self, self.LATEST_SUCCESSFUL_BUILD_CACHE):
928-
return build_successful[0]
929-
return None
930-
931923
# Check if there is `_good_build` annotation in the Queryset.
932924
# Used for Database optimization.
933925
if hasattr(self, "_good_build"):

readthedocs/projects/querysets.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -132,39 +132,26 @@ def prefetch_latest_build(self):
132132
from readthedocs.builds.models import Build
133133

134134
# Prefetch the latest build for each project.
135-
subquery_build_latest = Subquery(
135+
subquery = Subquery(
136136
Build.internal.filter(project=OuterRef("project_id"))
137137
.order_by("-date")
138138
.values_list("id", flat=True)[:1]
139139
)
140-
prefetch_build_latest = Prefetch(
140+
latest_build = Prefetch(
141141
"builds",
142-
Build.internal.filter(pk__in=subquery_build_latest),
142+
Build.internal.filter(pk__in=subquery),
143143
to_attr=self.model.LATEST_BUILD_CACHE,
144144
)
145-
146-
# Prefetch the latest successful build for each project.
147-
subquery_build_successful = Subquery(
148-
Build.internal.filter(project=OuterRef("project_id"))
149-
.order_by("-date")
150-
.values_list("id", flat=True)[:1]
151-
)
152-
prefetch_build_successful = Prefetch(
153-
"builds",
154-
Build.internal.filter(pk__in=subquery_build_successful),
155-
to_attr=self.model.LATEST_SUCCESSFUL_BUILD_CACHE,
156-
)
157-
158-
return self.prefetch_related(
159-
prefetch_build_latest,
160-
prefetch_build_successful,
161-
)
145+
return self.prefetch_related(latest_build)
162146

163147
# Aliases
164148

165149
def dashboard(self, user):
166150
"""Get the projects for this user including the latest build."""
167-
return self.for_user(user).prefetch_latest_build()
151+
# Prefetching seems to cause some inconsistent performance issues,
152+
# disabling for now. For more background, see:
153+
# https://github.com/readthedocs/readthedocs.org/pull/11621
154+
return self.for_user(user)
168155

169156
def api(self, user=None):
170157
return self.public(user)

readthedocs/projects/views/public.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,7 @@ def get_context_data(self, **kwargs):
123123
queryset=versions,
124124
project=project,
125125
)
126-
versions = (
127-
self.get_filtered_queryset()
128-
.prefetch_related("project")
129-
.prefetch_subquery()
130-
)
126+
versions = self.get_filtered_queryset()
131127
context["versions"] = versions
132128

133129
protocol = "http"

0 commit comments

Comments
 (0)