-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Prefetch build and project on version list #11616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ( | ||
|
@@ -141,6 +141,30 @@ def for_reindex(self): | |
.distinct() | ||
) | ||
|
||
def prefetch_subquery(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't we call it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That method shouldn't be called |
||
""" | ||
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need the first
if
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started to write it like that but we do need to test for
hasattr
first, as this attribute only exists after prefetch and it's what shortcuts this method to avoid thereturn self.builds....
below.If this was just
getattr
, we'd always skip thereturn self.builds...
below.