Orm N Plus One Query (ARCH3079)
Key typescript.performance.orm-n-plus-one · Severity error · Category performance
Iterating a query result and lazily accessing each item’s related record triggers a separate database query per iteration (N+1). Relationships should be eager-loaded.
Incorrect
Section titled “Incorrect”const orders = await repo.find({ where: { active: true } });for (const order of orders) { console.log(order.customer.name); // lazy query per iteration}Correct
Section titled “Correct”const orders = await repo.find({ where: { active: true }, relations: { customer: true },});for (const order of orders) { console.log(order.customer.name);}Eager-load related data in a single query with relations / an eager join before the loop.