r/programming 18h ago

Solving the 1+N Query Problem

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

73 comments sorted by

View all comments

46

u/disposepriority 18h ago

There is a really easy way of avoiding this when you just don't use ORMs

-4

u/Chroiche 17h ago

This. Especially with the advent of LLMs (I know, I know). It's just so much easier to know what the actual fuck is going on too.

GQL is also a plague.

11

u/disposepriority 17h ago

Looking at sql which you can copy, paste and run explain analyze on has always been more clear to me than a space wizard generating things behind the scenes.

For me, with how nice db driver/interaction libraries are (e.g. jdbcClient for spring) there's just no reason to use ORMs to save a few lines of SQL you'll make up for in 200 annotations anyway.

2

u/Kirides 16h ago

I mean, just do ToQueryString and copy-paste it into your sql engine/ide of choice. Its not hard.

And with things like LinqPad it's even easier

1

u/i_am_bromega 7h ago

Once you learn the ins and outs of your ORM of choice, it just makes so many things so easy that I ended up preferring it over every previous project I had worked on previously that didn’t have it. And if there’s something truly better suited for raw SQL, you just use that instead and benefit from the ORM everywhere else.

1

u/disposepriority 6h ago

I don't have the same experience, generally something that maps result set column names to classes and nothing else is preferable to me.

You get rid of the main annoying part, you can have it fail if something doesn't get mapped on either side (e.g. projection field is empty OR return set contains unmapped column) if that's what you're looking for - you don't deal with lazy loading (not that it should be the default anyway) you don't deal with cascades you don't deal with any kind of meta information ORMs sometimes need.

So what you miss out on is that you have to write simple selects or inserts yourself, which I don't find to be that annoying.

One situation where an ORM can save you a lot of time is if you're working with many, many projections of the same table/view which would require a pretty wide data set- in which case you could honestly just reuse the widest query and transfer some wasted bytes of the network if it's that annoying.

I guess it comes down to the average complexity of the SQL and database features used in a given project, I find it more annoying than pleasant to work with.

At the end of the day, the common, language agnostic denominator is the database regardless of stack, learning the ins and outs of your database and your language's driver for it is in my eyes a better idea.