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, one query for each subproject (constant). It used to add 3 queries for each subproject.

@read-the-docs-community
Copy link

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

Documentation build overview

📚 docs | 🛠️ Build #30574161 | 📁 Comparing 0da45ff against latest (92b69eb)


🔍 Preview build

Show files changed (1 files in total): 📝 1 modified | ➕ 0 added | ➖ 0 deleted
File Status
reference/git-integration.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
Copilot finished reviewing on behalf of stsewd December 4, 2025 21:11
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Needs review

Development

Successfully merging this pull request may close these issues.

2 participants