jeudi 7 juin 2018

Left outer join rails with eager loading on where condition

I got a system down issue while eager loading records with where condition.

I've read a blog on link. It spokes about the performance issue while using where condition with eager loading associations.

@customers = Customer.all.includes(:invoices, :receipts).where(invoices: { status: "open" })

"This generates two left outer joins. Whats going to happen is that the database is going to return a row for every unique combination of customer, invoice and receipt. So if we have 25 customers, each customer has 10 invoices (250 total) and 10 receipts (250 total). The result set that is returned is not 525 records, but something closer to 2500 records. Now Rails has to instantiate all of that into ActiveRecord objects."

I am not getting how come it generates 2500 records?

Aucun commentaire:

Enregistrer un commentaire