Ir al contenido

N Plus One Query (ARCH3050)

Esta página aún no está disponible en tu idioma.

Key typescript.orm.n-plus-one-query · Severity warning · Category orm

Running a separate query for each element inside a loop produces one list query plus N follow-up queries. This is the classic ORM performance killer; fetch related data in a single round-trip.

const orders = await orderRepo.find();
for (const order of orders) {
// A separate SELECT per order
const customer = await customerRepo.findOneBy({ id: order.customerId });
console.log(customer.name);
}
const orders = await orderRepo.find({ relations: { customer: true } });
for (const order of orders) {
console.log(order.customer.name);
}

Eager-load related data with relations/include and drop the per-iteration queries.