Backend Developer
mediumn-plus-one-problem

What is the N+1 query problem and how do you fix it?

Answer

N+1 happens when you fetch a list (1 query) and then fetch related data per item (N queries). **Fixes:** - Use joins or eager loading - Batch queries (IN clause) - Add caching for repeated lookups **Interview tip:** mention ORMs often hide this problem; profiling and query logging are essential.

Related Topics

DatabasesPerformanceORM