Skip to content

Optimize eager loading in list endpoint — avoid over-fetching relations #459

@zigzagdev

Description

@zigzagdev

Goal

The current getAllHeritages query eager-loads three relations (countries, images, descriptions) for every item in the paginated list. For a 20-item page this means 3 extra queries minimum, and the data returned is often more than what the list view renders.

What to Do

  1. Audit what fields the list response actually exposes (WorldHeritageDtoCollectionFactorytoSummaryArray).
  2. For the images relation: the list only needs thumbnail_url which is already covered by main_image_url. If main_image_url is non-null for most records, skip eager-loading images on the list endpoint entirely (or load only when main_image_url IS NULL).
  3. For descriptions: the list uses short_description_ja. Evaluate whether this is shown in the list card. If not, remove it from the list query.
  4. For countries: only name_en, name_jp, state_party_code are needed — confirm the select is already minimal and tighten if not.
  5. Add ->withCount(...) only if aggregate counts are needed; avoid with() for unused relations.

Acceptance Criteria

  • Number of DB queries per list page request reduced (measure with Laravel Debugbar or query log)
  • List endpoint returns the same payload shape with no missing fields
  • Tests pass

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