Silent Performance Killer: N+1 Query Problem
dev.to·2h·
Discuss: DEV
Flag this post

The N+1 query problem is a database anti-performance pattern, where an application executes one query to fetch a lists of items (N items in 1 query) and then makes N additional individual queries to fetch related data for each item(N number of queries).

This results in N+1 total database queries for what should ideally be a single efficient or a few queries.

Analogy: The Library

Imagine you go to a library and you ask the librarian to give you a list 100 books of your favorite genre. The librarian gives you the list of 100 books(this is 1 query).

Now you have your list, but you don’t know author’s name. To know the name of the authors you go to the librarian and ask him hey Librarian what is the name of the author of this book?. You ask this question for each book …

Similar Posts

Loading similar posts...