İçeriğe geç

N+1 Sorgu (ARCH3050)

Anahtar typescript.orm.n-plus-one-query · Önem warning · Kategori orm

Döngü içinde her eleman için ayrı bir sorgu çalıştırmak, bir liste sorgusu + N adet ek sorgu üretir. Bu klasik ORM performans katilidir; ilişkili veriyi tek seferde getirin.

const orders = await orderRepo.find();
for (const order of orders) {
// Her sipariş için ayrı bir SELECT
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);
}

İlişkili veriyi relations/include ile eager load edip döngü içindeki tekil sorguları kaldırın.