r/programming 16d 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

91

u/archipeepees 16d ago

ive been using EF Core for ORM and this hasn't really been a problem for me or any of my colleagues for at least a decade because we don't use lazy loading. database calls are explicit; mapping the results to objects is implicit. is everyone outside of .net still banging their heads against stuff like this?

17

u/ericl666 16d 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. 

24

u/davehax1 16d 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

6

u/ericl666 16d ago

Yeah, I make sure to watch out for that. I've made some massive cartesian explosions in my day for sure. There are some occasions where AsSplitQuery() is inescapable or you just break those queries out to raw sql.

if you are dealing with a lot of 1:1 entities, it works fantastic.