๐—ง๐—ต๐—ฒ ๐—ก+๐Ÿญ ๐—ค๐˜‚๐—ฒ๐—ฟ๐˜† ๐—ฃ๐—ฟ๐—ผ๐—ฏ๐—น๐—ฒ๐—บ

You check your logs. You see hundreds of SQL calls. You wrote one query. This is the N+1 problem.

It happens when you fetch a main list. Then you fetch details for each item one by one.

Think of a classroom. You ask for a list of 30 students. You get 30 names. Then you ask for each grade one by one. You make 31 trips to the office.

Slow approach:

Fast approach 1: Use a JOIN.

Fast approach 2: Use batching.

Why this works:

Check your code. Look for queries inside loops. Fetch data in bulk.

Source: https://dev.to/qq5yu/database-n1-query-problem-56kd