Skip to content

Commit

Permalink
Try reverting prefetch changes for project/version listing views (#11621
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
agjohnson authored Sep 30, 2024
1 parent 6680d0e commit 6569f5f
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 69 deletions.
10 changes: 0 additions & 10 deletions readthedocs/builds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,6 @@ class Meta:
unique_together = [("project", "slug")]
ordering = ["-verbose_name"]

# Property used for prefetching version related fields
LATEST_BUILD_CACHE = "_latest_build"

def __str__(self):
return self.verbose_name

Expand Down Expand Up @@ -300,13 +297,6 @@ def last_build(self):

@property
def latest_build(self):
# Check if there is `_latest_build` prefetch in the Queryset.
# Used for database optimization.
if hasattr(self, self.LATEST_BUILD_CACHE):
if latest_build := getattr(self, self.LATEST_BUILD_CACHE):
return latest_build[0]
return None

return self.builds.order_by("-date").first()

@property
Expand Down
26 changes: 1 addition & 25 deletions readthedocs/builds/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import structlog
from django.db import models
from django.db.models import OuterRef, Prefetch, Q, Subquery
from django.db.models import Q
from django.utils import timezone

from readthedocs.builds.constants import (
Expand Down Expand Up @@ -141,30 +141,6 @@ def for_reindex(self):
.distinct()
)

def prefetch_subquery(self):
"""
Prefetch related objects via subquery for each version.
.. note::
This should come after any filtering.
"""
from readthedocs.builds.models import Build

# Prefetch the latest build for each project.
subquery_builds = Subquery(
Build.internal.filter(version=OuterRef("version_id"))
.order_by("-date")
.values_list("id", flat=True)[:1]
)
prefetch_builds = Prefetch(
"builds",
Build.internal.filter(pk__in=subquery_builds),
to_attr=self.model.LATEST_BUILD_CACHE,
)

return self.prefetch_related(prefetch_builds)


class VersionQuerySet(SettingsOverrideObject):
_default_class = VersionQuerySetBase
Expand Down
8 changes: 0 additions & 8 deletions readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,6 @@ class Project(models.Model):

# Property used for storing the latest build for a project when prefetching
LATEST_BUILD_CACHE = "_latest_build"
LATEST_SUCCESSFUL_BUILD_CACHE = "_latest_successful_build"

class Meta:
ordering = ("slug",)
Expand Down Expand Up @@ -921,13 +920,6 @@ def conf_dir(self, version=LATEST):

@property
def has_good_build(self):
# Check if there is `_latest_successful_build` attribute in the Queryset.
# Used for database optimization.
if hasattr(self, self.LATEST_SUCCESSFUL_BUILD_CACHE):
if build_successful := getattr(self, self.LATEST_SUCCESSFUL_BUILD_CACHE):
return build_successful[0]
return None

# Check if there is `_good_build` annotation in the Queryset.
# Used for Database optimization.
if hasattr(self, "_good_build"):
Expand Down
29 changes: 8 additions & 21 deletions readthedocs/projects/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,39 +132,26 @@ def prefetch_latest_build(self):
from readthedocs.builds.models import Build

# Prefetch the latest build for each project.
subquery_build_latest = Subquery(
subquery = Subquery(
Build.internal.filter(project=OuterRef("project_id"))
.order_by("-date")
.values_list("id", flat=True)[:1]
)
prefetch_build_latest = Prefetch(
latest_build = Prefetch(
"builds",
Build.internal.filter(pk__in=subquery_build_latest),
Build.internal.filter(pk__in=subquery),
to_attr=self.model.LATEST_BUILD_CACHE,
)

# Prefetch the latest successful build for each project.
subquery_build_successful = Subquery(
Build.internal.filter(project=OuterRef("project_id"))
.order_by("-date")
.values_list("id", flat=True)[:1]
)
prefetch_build_successful = Prefetch(
"builds",
Build.internal.filter(pk__in=subquery_build_successful),
to_attr=self.model.LATEST_SUCCESSFUL_BUILD_CACHE,
)

return self.prefetch_related(
prefetch_build_latest,
prefetch_build_successful,
)
return self.prefetch_related(latest_build)

# Aliases

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

def api(self, user=None):
return self.public(user)
Expand Down
6 changes: 1 addition & 5 deletions readthedocs/projects/views/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ def get_context_data(self, **kwargs):
queryset=versions,
project=project,
)
versions = (
self.get_filtered_queryset()
.prefetch_related("project")
.prefetch_subquery()
)
versions = self.get_filtered_queryset()
context["versions"] = versions

protocol = "http"
Expand Down

0 comments on commit 6569f5f

Please sign in to comment.