Hacker News new | comments | show | ask | jobs | submit login

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.



Applications are open for YC Winter 2019

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: