Skip to content

Commit

Permalink
Prefetch build and project on version list (#11616)
Browse files Browse the repository at this point in the history
This optimizes the version listing view and removes redundant queries.

- Requires readthedocs/ext-theme#493
- Fixes readthedocs/ext-theme#463
  • Loading branch information
agjohnson authored Sep 25, 2024
1 parent fd82aeb commit e5f8092
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
16 changes: 16 additions & 0 deletions readthedocs/builds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ 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 @@ -291,6 +294,19 @@ def vcs_url(self):

@property
def last_build(self):
# TODO deprecated in favor of `latest_build`, which matches naming on
# the Project model
return self.latest_build

@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: 25 additions & 1 deletion 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 Q
from django.db.models import OuterRef, Prefetch, Q, Subquery
from django.utils import timezone

from readthedocs.builds.constants import (
Expand Down Expand Up @@ -141,6 +141,30 @@ 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
6 changes: 5 additions & 1 deletion readthedocs/projects/views/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ def get_context_data(self, **kwargs):
queryset=versions,
project=project,
)
versions = self.get_filtered_queryset()
versions = (
self.get_filtered_queryset()
.prefetch_related("project")
.prefetch_subquery()
)
context["versions"] = versions

protocol = "http"
Expand Down

0 comments on commit e5f8092

Please sign in to comment.