liveBook · Manning

N + 1 query problem

As explained in ScyllaDB in Action

The N+1 problem is a performance problem in which loading a collection and related data requires one initial query plus a separate query for each item.

The N + 1 problem occurs when loading a collection requires one initial query plus a separate query for each related item. Loading 10 article summaries and then fetching each author can therefore require up to 11 database calls. section 5.1.2

Article summaries and authors

Reusing an articles table for summaries would retain each author’s ID. Loading summaries would then require one call for the articles and one author lookup per article. A separate article-summaries table avoids this pattern by storing the author’s name with each summary, alongside the fields needed for the summary view. section 5.1.2

Table 5.1 Article summaries have the same values for some fields, but other fields have subtle differences.
Articles
Article summaries
Title
Title
Author ID
Author name
Score
Score
Gallery of images
One image

Compare the fields used by full articles and article summaries to see why the summary table stores an author name rather than an author ID. section 5.1.2

How ScyllaDB in Action covers it

How deep it goes
Defines the pattern and works through a ten-summary example, then stops at schema-level denormalization.
Examples
Uses article summaries, author lookups, an articles table, and a separate denormalized summaries table.
What you can do after
Evaluate read schemas and avoid repeated related-data calls by storing view-specific fields together.
How it connects
Connects query performance with denormalization, data modeling, and read-query design.

Also explained in

Compare how 4 books explain N + 1 query problem →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ScyllaDB in Action. The section links point to the parts of the book it draws on.