Technical Glossary

N+1 Query Problem

Definition: Performance anti-pattern where an initial query triggers N additional queries, significantly degrading application performance.

— Source: NERVICO, Product Development Consultancy

What is the N+1 Query Problem

The N+1 query problem is a performance anti-pattern that occurs when code executes an initial query to fetch a list of N records and then executes an additional query for each of those N records to fetch related data. The result is N+1 database queries where one or two well-designed queries would have been sufficient. It is one of the most frequent causes of poor performance in applications that use ORMs.

How it works

The problem typically appears when accessing model relationships with lazy loading. For example, when fetching a list of 100 orders, the ORM executes one SELECT query to load the orders. When the code iterates over the orders and accesses each order’s customer, the ORM executes a new SELECT query per order to load the associated customer. The result is 101 queries: 1 for the orders plus 100 for the customers. With eager loading or a JOIN query, the same result is obtained in 1 or 2 queries. The performance difference can be orders of magnitude, especially with large lists or database connections with latency.

Why it matters

The N+1 problem is insidious because the code looks correct and works fine with few records in development. Problems surface in production when the list grows: 10 records generate 11 queries (imperceptible), but 1,000 records generate 1,001 queries that can take several seconds. Each individual query adds network latency, parsing time, and connection overhead. In applications with multiple levels of relationships, the problem multiplies exponentially. It is a frequent source of technical debt that manifests as progressive performance degradation as data grows.

Practical example

An e-commerce API returning a product catalog with categories takes 3.2 seconds to respond when the catalog has 500 products. The team enables SQL query logging and discovers 501 queries: one to load the products and one per product to load its category. The fix involves modifying the ORM query to use eager loading (include in Sequelize, prefetch_related in Django, include in Prisma). After the change, the API executes 2 queries (one for products, one for categories) and responds in 80 milliseconds, a 97% improvement.

  • Technical Debt - Accumulated consequence of not detecting and fixing N+1 problems in time
  • Scalability - Capability that the N+1 problem significantly degrades as data grows

Last updated: February 2026

Need help with product development?

We help you accelerate your development with cutting-edge technology and best practices.