Skip to content

perf: fix N+1 cartesian products and add server caching#199

Open
kargig wants to merge 1 commit intomainfrom
feature/performance-optimizations-part2
Open

perf: fix N+1 cartesian products and add server caching#199
kargig wants to merge 1 commit intomainfrom
feature/performance-optimizations-part2

Conversation

@kargig
Copy link
Copy Markdown
Owner

@kargig kargig commented Apr 11, 2026

  • Replace joinedload with selectinload for One-to-Many collection relationships across chat, dive sites, and dives routers. This prevents SQLAlchemy from emitting massive Cartesian products (LEFT OUTER JOINs) that cause high RAM and CPU consumption during deduplication.
  • Implement server-side request caching using fastapi-cache2 with an InMemoryBackend.
  • Add caching to heavily requested static lists like /countries and /regions (1-hour cache) to prevent redundant SELECT DISTINCT queries.
  • Add caching to admin /statistics, /stats, and /settings endpoints (5-minute cache) to shield the database from expensive aggregation queries and configuration reads.
  • Update backend/conftest.py to initialize the cache in the test environment to prevent 500 errors during test execution.

- Replace `joinedload` with `selectinload` for
One-to-Many collection relationships across chat,
dive sites, and dives routers. This prevents
SQLAlchemy from emitting massive Cartesian
products (LEFT OUTER JOINs) that cause high RAM
and CPU consumption during deduplication.
- Implement server-side request caching using
`fastapi-cache2` with an `InMemoryBackend`.
- Add caching to heavily requested static lists
like `/countries` and `/regions` (1-hour cache)
to prevent redundant `SELECT DISTINCT` queries.
- Add caching to admin `/statistics`, `/stats`,
and `/settings` endpoints (5-minute cache) to
shield the database from expensive aggregation
queries and configuration reads.
- Update `backend/conftest.py` to initialize
the cache in the test environment to prevent 500
errors during test execution.
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.

1 participant