News for hackers. Stuff that matters.
Re:How Hibernate Almost Ruined My Career by snarfy on Thursday February 16, 2017 @10:45PM (#13665756)
The N+1 problem is caused by lazy loading. It's when you have an entity, e.g. Customer which has a property Orders with a foreign key relationship to Customer. The property is not materialized until it's accessed, causing an extra (N+1) query to be run.

This can be avoided with something like e.g. EntityFramework which is based on the IQueryable interface and expression trees. In EF it would be

    var query = DbContext.Customers.Include( c => c.Orders).Where( c => c.customer_name == "Fred");
The value of query is an expression until it is iterated or converted to e.g. .ToList(); The expression is built using the relationships defined in the mapping which allows EF to use a proper SQL join, avoiding two queries and the N+1 problem.

There are no comments on this comment yet.