r/programming 7d ago

Solving the 1+N Query Problem

https://acadia.engineering/blog/solving-the-1-plus-N-query-problem
126 Upvotes

94 comments sorted by

View all comments

Show parent comments

16

u/ericl666 7d ago

I absolutely love EF Core.

dbContext.Book.Include(I => Publisher).Where(w => w.Title == "my book").FirstOrDefaultAsync()

You just add Include/ThenInclude to add all the joins you need. 

25

u/davehax1 7d ago

It's great for small queries and small data sets, but look up Cartesian explosion and also review the generated SQL when using .Include(). You'd be surprised at what gets generated and how quickly performance can degrade as the result set grows

15

u/AyrA_ch 7d ago

Cartesian explosion in EF can be mitigated by either adding .AsSplitQuery() to the queries where necessary, or by specifying it as the global default during context setup.

5

u/ChemicalRascal 7d ago

Yeah, it's still something you need to be aware of and correctly mentally model, though. And if you don't, or if your colleague doesn't even know about it, well, then it bites you in the ass.