Skip to content

Conversation

@stsewd
Copy link
Member

@stsewd stsewd commented Dec 4, 2025

On .com having organizations introduces some extra queries. Since all subprojects share the same organization as the parent project, we can cache that organization, so it's shared across all subprojects.

This also allows caching the .organization property, and skips querying organizations if we are on .org.

Results:

  • One less query for searches on projects with organizations
  • When searching across subprojects, two queries for each subproject (constant). It used to add 4 queries for each subproject.

I had to move tests to .com, so they are more accurate with production, as in .com we do several overrides.

@read-the-docs-community
Copy link

read-the-docs-community bot commented Dec 4, 2025

Documentation build overview

📚 docs | 🛠️ Build #30629615 | 📁 Comparing d2c2b72 against latest (9f74278)


🔍 Preview build

Show files changed (8 files in total): 📝 7 modified | ➕ 0 added | ➖ 1 deleted
File Status
addons.html 📝 modified
build-customization.html 📝 modified
builds.html 📝 modified
custom-script.html ➖ deleted
support.html 📝 modified
about/index.html 📝 modified
reference/git-integration.html 📝 modified
tutorial/index.html 📝 modified

@stsewd stsewd marked this pull request as ready for review December 4, 2025 21:04
@stsewd stsewd requested a review from a team as a code owner December 4, 2025 21:04
@stsewd stsewd requested a review from ericholscher December 4, 2025 21:04
@stsewd stsewd requested a review from Copilot December 4, 2025 21:06
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes database queries when searching projects with organizations by implementing strategic caching and prefetching. The key optimization is that since all subprojects share the same organization as their parent project, the organization can be cached and reused across all related projects.

Key Changes

  • Converted Project.organization from @property to @cached_property to cache organization lookups
  • Added prefetch_organization() queryset method for efficient bulk loading of organizations
  • Implemented manual organization caching in search executor for subprojects via _organizations attribute
  • Replaced all project.organizations.first() calls with project.organization throughout the codebase
  • Added resolver caching for organization-level feature checks (custom domains)

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
readthedocs/projects/models.py Converted organization property to cached_property with RTD_ALLOW_ORGANIZATIONS check
readthedocs/projects/querysets.py Added prefetch_organization() method for bulk loading with select_related support
readthedocs/search/api/v3/executor.py Manually cache organization for subprojects by setting _organizations attribute; added prefetch_organization to project queries
readthedocs/search/api/v2/serializers.py Reuse single Resolver instance across projects to leverage organization caching
readthedocs/core/resolver.py Added organization-level caching for custom domain checks with @cache decorator
readthedocs/telemetry/models.py Updated to use project.organization property
readthedocs/core/permissions.py Updated to use project.organization property
readthedocs/builds/querysets.py Updated to use project.organization property
readthedocs/organizations/tasks.py Updated to use project.organization property
readthedocs/audit/models.py Updated to use project.organization property
readthedocs/api/v3/tests/test_subprojects.py Removed erroneous organization.projects.add() line
readthedocs/search/api/v3/tests/test_api.py Added comprehensive tests validating query counts with organizations
readthedocs/rtd_tests/tests/test_project_querysets.py Added @override_settings decorator and cache clearing for organization tests
readthedocs/builds/tests/test_build_queryset.py Added @override_settings decorator for organization tests
readthedocs/invitations/tests/test_views.py Added @override_settings decorator for organization tests
readthedocs/audit/tests/test_tasks.py Added @override_settings decorator for organization tests
readthedocs/audit/tests/test_models.py Added @override_settings decorator for organization tests

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@stsewd stsewd moved this to Needs review in 📍Roadmap Dec 4, 2025
Copy link
Member

@ericholscher ericholscher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good with a couple questions.

@read-the-docs-community
Copy link

Documentation build overview

📚 dev | 🛠️ Build #30629614 | 📁 Comparing d2c2b72 against latest (9f74278)


🔍 Preview build

Show files changed (4 files in total): 📝 4 modified | ➕ 0 added | ➖ 0 deleted
File Status
contribute.html 📝 modified
install.html 📝 modified
search-integration.html 📝 modified
style-guide.html 📝 modified

@stsewd stsewd merged commit 6d0eb9e into main Dec 9, 2025
11 checks passed
@stsewd stsewd deleted the search-optimize-subprojects-com branch December 9, 2025 18:54
@github-project-automation github-project-automation bot moved this from Needs review to Done in 📍Roadmap Dec 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants