Skip to content

n+1 database queries when fetching /content_nodes and /camps endpoints #8123

@carlobeltrame

Description

@carlobeltrame

There is an n+1 problem in the API (this one has nothing to do with the frontend).
When fetching the /content_nodes endpoint, there is a separate SQL statement executed for each material node (loading the connected material items) and for each checklist node (loading the connected checklist items).
When fetching the /camps endpoint, there are 3 separate SQL statements executed for each camp, loading the periods, camp collaborations and material lists.

This can be reproduced locally by visiting http://localhost:3000/api/content_nodes or http://localhost:3000/api/camps and afterwards http://localhost:3000/api/_profiler/empty/search/results?limit=10, clicking on the topmost Token and then on the Doctrine section in the left navigation.

It originally became evident during #8004, when new content node fixtures were added and the query counts in the EndpointPerformanceTest (for content nodes and for camps) went up.

This could potentially be solved by using Doctrine's fetch: EAGER feature. According to the documentation and some rudimentary tests, for one-to-many relations, doctrine can query the top-level entity and then query the related entities in a second query (or in batches of 100 by default, if there are many). But eager fetching like this can potentially cause performance impacts in other endpoints.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions