Skip to content

[NSOC'26][GSSOC'26] Fix(db): Optimize N+1 queries using SQLAlchemy eager loading - #975

Open
diksha78dev wants to merge 2 commits into
devanshi14malhotra:mainfrom
diksha78dev:fix/issue-880
Open

diksha78dev wants to merge 2 commits into
devanshi14malhotra:mainfrom
diksha78dev:fix/issue-880

Conversation

@diksha78dev

Copy link
Copy Markdown
Contributor

Description

This PR resolves the N+1 database query performance issues reported in #880 specifically affecting the collections endpoints.

Changes Made

  • Added selectinload(Collection.items) to get_collections() to eagerly load collection items.
  • Added chained .options(selectinload(Collection.items).joinedload(CollectionItem.book)) to get_collection() to eagerly load items and their associated books simultaneously.
  • Used joinedload(Collection.user) and selectinload(Collection.items) in get_public_collections() to eliminate sequential user lookups in loop iterations.

These changes prevent Cartesian explosion while ensuring all necessary related data is fetched in a minimal number of optimized queries.

Closes #880

@netlify

netlify Bot commented May 24, 2026 •

Copy link
Copy Markdown

❌ Deploy Preview for bibliodrift-dm failed. Why did it fail? →

Name Link
🔨 Latest commit 649deea
🔍 Latest deploy log https://app.netlify.com/projects/bibliodrift-dm/deploys/6a27b895a244640008bede39

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Database]: Optimize N+1 Query Issues using SQLAlchemy Eager Loading

1 participant