Skip to content

Profile and fix N+1 query patterns in WorldHeritageQueryService #462

@zigzagdev

Description

@zigzagdev

Goal

Confirm there are no N+1 query patterns lurking in the list or search code paths that multiply DB queries proportionally with the number of results returned.

What to Do

  1. Install / enable Laravel Debugbar or Telescope locally and load the list endpoint with per_page=20.
  2. Record total query count. Expected: 1 main query + 1 per eager-loaded relation (countries, images, descriptions) = ~4 total. Any more indicates N+1.
  3. If N+1 is found, fix with ->with([...]) eager loading or ->withCount(...) on the Eloquent query. Do NOT use lazy loading inside loops.
  4. Check buildWorldHeritagePayload() — it accesses $heritage->countries, $heritage->images, $heritage->descriptions per row. Confirm these are already loaded via eager loading and not triggering additional queries.
  5. Check the search path: searchHeritages() calls findByIdsPreserveOrder() — confirm this also eager-loads relations in a single batch.

Acceptance Criteria

  • Total query count for a list page request ≤ 5 (regardless of page size)
  • No query count grows linearly with per_page
  • Profiling results (query count before/after) documented in the PR

Parent Issue

Related: #457

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions