News for hackers. Stuff that matters.
Re:Migrating to Postgres by etblg on Wednesday May 14, 2025 @08:20PM (#43990564)
> It's true that Prisma currently doesn't do JOINs for relational queries. Instead, it sends individual queries and joins the data on the application level.

..........I'm sorry, what? That seems........absurd.

edit: Might as well throw in: I can't stand ORMs, I don't get why people use it, please just write the SQL.

Re:Migrating to Postgres by jjice on Wednesday May 14, 2025 @08:30PM (#43990617)
I believe it’s either released now or at least a feature flag (maybe only some systems). It’s absolutely absurd it took so long. I can’t believe it wasn’t the initial implementation.

Funny relevant story: we got an OOM from a query that we used Prisma for. I looked into it - it’s was a simple select distinct. Turns out (I believe it was changed like a year ago, but I’m not positive), event distincts were done in memory! I can’t fathom the decision making there…

Re:Migrating to Postgres by etblg on Wednesday May 14, 2025 @08:33PM (#43990628)
> event distincts were done in memory! I can’t fathom the decision making there…

This is one of those situations where I can't tell if they're operating on some kind of deep insight that is way above my experience and I just don't understand it, or if they just made really bad decisions. I just don't get it, it feels so wrong.

Re:Migrating to Postgres by wredcoll on Thursday May 15, 2025 @12:06AM (#43991751)
It really gives me flashbacks to the early days of mongodb.

Which, frankly, is a good lesson that marketing and docs and hype can make up for any amount of technical failure, and if you live long enough, you can fix the tech issues.

Re:Migrating to Postgres by Tadpole9181 on Wednesday May 14, 2025 @09:14PM (#43990867)
> I can't tell if they're operating on some kind of deep insight that is way above my experience and I just don't understand it

This is answered at the very top of the link on the post you replied to. In no unclear language, no less. Direct link here: https://github.com/prisma/prisma/discussions/19748#discussio...

> I want to elaborate a bit on the tradeoffs of this decision. The reason Prisma uses this strategy is because in a lot of real-world applications with large datasets, DB-level JOINs can become quite expensive...

> The total cost of executing a complex join is often higher than executing multiple simpler queries. This is why the Prisma query engine currently defaults to multiple simple queries in order to optimise overall throughput of the system.

> But Prisma is designed towards generalized best practices, and in the "real world" with huge tables and hundreds of fields, single queries are not the best approach...

> All that being said, there are of course scenarios where JOINs are a lot more performance than sending individual queries. We know this and that's why we are currently working on enabling JOINs in Prisma Client queries as well You can follow the development on the roadmap.

Though this isn't a complete answer still. Part of it is that Prisma was, at its start, a GraphQL-centric ORM. This comes with its own performance pitfalls, and decomposing joins into separate subqueries with aggregation helped avoid them.

Re:Migrating to Postgres by evanelias on Wednesday May 14, 2025 @11:40PM (#43991616)
It's a completely ridiculous answer though. They're linking to High Performance MySQL's 2nd edition, which came out in June 2008, and was written for users of MySQL 5.0 running on 2008-era hardware.

My take, as a MySQL expert: that advice is totally irrelevant now, and has been for quite some time. It's just plain wrong in a modern context.

Re:Migrating to Postgres by setr on Thursday May 15, 2025 @03:41AM (#43992729)
I’m not even sure it was correct for its time? The whole point of an RDBMS is to execute join operations. The only reason I’d suspect an RDBMS to be bad at its one fundamental job, in any point of time, is the N+1 query scenario or multiple left joins with unrelated dependencies, but that’s triggered by bad orm abstractions to begin with
Re:Migrating to Postgres by evanelias on Thursday May 15, 2025 @10:18AM (#43995374)
It was absolutely valid advice for its time, but only in the highly specific cases/reasons outlined in the book. The 2nd edition was written by the top Percona folks, who pretty much had more experience scaling databases for large websites than anyone else.

The Prisma answer just does not summarize correctly what the book was saying.

It mainly boiled down to sharding and external caching. Storage and memory were much smaller back then, so there was a lot of sharding and functional partitioning, and major reliance on memcached; all of those are easier if you minimize excessive JOINs.

The query planner in MySQL wasn't great at the time either, and although index hints could help, huge complex queries sometimes performed worse than multiple decomposed simpler queries. But the bigger issue was definitely enabling sharding (cross-shard joins had to be handled at the application level) and enabling external caching (do a simple range scan DB query to get a list of IDs/PKs, then do point lookups in memcached, then finally do point lookups in the DB for any that weren't in memcached).

Re:Migrating to Postgres by Tadpole9181 on Thursday May 15, 2025 @01:53PM (#43997525)
And, in light of that, the default is changing in a couple of months after the JOIN mode has had a significant period of time being tested in the real world.
Re:Migrating to Postgres by MonkeyClub on Thursday May 15, 2025 @08:09AM (#43994184)
> if they're operating on some kind of deep insight

If one's getting OOM errors from a SELECT DISTINCT, then there's no deep insight behind the choice, it's just a mistake.

Re:Migrating to Postgres by seer on Wednesday May 14, 2025 @11:21PM (#43991517)
Tbh, I once dabbled in building an ORM myself (in PHP) and I did find that in some situations it was faster to do individual queries and then join in code, to solve the N+1 problem.

Granted I was much worse in my sql knowledge and postgre/mysql had severe limitations in their query planners, so I can see how something like this could have happened. If they support multiple dbs, and even one has this problem, it might be better (for them) to do it application side.

The specific issue was doing a join with a table for a one to many, you get a lot more data from the db than you would normally need, if you do the join the naive way, and if the join is nested you get exponentially more data.

It was faster to do a query for each db separately and then stitch the results.

Now it is easy to solve in pg with nested selects and json aggregation, which pg query planner rewrites to efficient joins, but you still get only the bytes you have requested without duplication.

Re:Migrating to Postgres by pier25 on Wednesday May 14, 2025 @08:42PM (#43990674)
> I can't stand ORMs, I don't get why people use it, please just write the SQL.

I used to agree until I started using a good ORM. Entity Framework on .NET is amazing.

Re:Migrating to Postgres by bob1029 on Wednesday May 14, 2025 @09:00PM (#43990794)
> Entity Framework on .NET is amazing.

I disagree. It is probably one of the less terrible ORMs, but it is far from amazing. The object-relational impedance mismatch will always dominate for anything that isn't trivial business. EF works great until you need different views of the model. It does support some kind of view mapping technique, but it's so much boilerplate I fail to see the point.

Dapper + SqlConnection is goldilocks once you get into the nasty edges. Being able to query a result set that always exactly matches your view models is pretty amazing. The idea of the program automagically upgrading and migrating the schemas is something that was interesting to me until I saw what you could accomplish with Visual Studio's SQL Compare tool & RedGate's equivalent. I feel a lot more comfortable running manual schema migrations when working with hosted SQL providers.

Re:Migrating to Postgres by cyral on Wednesday May 14, 2025 @09:06PM (#43990823)
> It does support some kind of view mapping technique

Can you call .Select(entity => SomeSmallerModel() { Name = entity.Name }) or something like that to select what you need? If I am understanding your issue correctly.

I also agree that its one of the least worst but there are still things that annoy me.

Re:Migrating to Postgres by neonsunset on Wednesday May 14, 2025 @09:05PM (#43990819)
> EF works great until you need different views of the model

You can easily project or use views with SQL then projected onto objects. It's very convenient with `.FromSql`:

https://learn.microsoft.com/en-us/ef/core/querying/sql-queri...

Re:Migrating to Postgres by pier25 on Thursday May 15, 2025 @12:29AM (#43991855)
> I feel a lot more comfortable running manual schema migrations

Me too. I use a DB-first approach. Then EF simply rebuilds the application models automatically with a single command.

Re:Migrating to Postgres by tilne on Wednesday May 14, 2025 @08:50PM (#43990730)
Doesn’t entity framework have a huge memory footprint too?
Re:Migrating to Postgres by neonsunset on Wednesday May 14, 2025 @09:07PM (#43990826)
Do you have any links that note memory usage issues with any of the semi-recent EF Core versions?
Re:Migrating to Postgres by tilne on Thursday May 15, 2025 @07:28AM (#43993888)
No. To be clear: I wasn’t trying to say it was bad. Just repeating what I had read in a (fairly old) .net book. Should have chosen my words more carefully.
Re:Migrating to Postgres by neonsunset on Thursday May 15, 2025 @08:54AM (#43994523)
To be fair, old Entity Framework was on the heavier side. Still much faster than e.g. ActiveRecord but enough for Dapper to be made back then. The gap between them is mostly gone nowadays plus .NET itself has become massively faster and alternate more efficient alternatives got introduced since (Dapper AOT, its main goal is NAOT compatibility but it also uses the opportunity to further streamline the implementation).
Re:Migrating to Postgres by tilne on Thursday May 15, 2025 @09:47AM (#43995054)
Thanks for the context. I’m new to the ecosystem, so it’s valuable to hear thoughts like these from people with more experience with it.
Re:Migrating to Postgres by homebrewer on Thursday May 15, 2025 @06:47AM (#43993654)
If you don't do stupid things like requesting everything from the database and then filtering data client side, then no.

We have one application built on .NET 8 (and contemporary EF) with about 2000 tables, and its memory usage is okay. The one problem it has is startup time: EF takes about a minute of 100% CPU load to initialize on every application restart, before it passes execution to the rest of your program. Maybe it is solvable, maybe not, I haven't yet had the time to look into it.

Re:Migrating to Postgres by tilne on Thursday May 15, 2025 @07:26AM (#43993878)
Yeah makes sense. I should have used my language more carefully: I have very little hands-on experience with entity framework, and thus don’t have objective reasons to say it has a “huge” memory footprint.

Does the startup time/CPU usage cost vary depending on the size of the dataset you’re interacting with?

Re:Migrating to Postgres by neonsunset on Thursday May 15, 2025 @10:16AM (#43995343)
Hmm, it should not be taking this much time unless you're running into an edge case - we have a fairly complex application that needs to pre-load a lot of data from persistence (no EFC though, it's an embedded DB) and over the wire at startup, and it still takes 15 seconds on a rainy day, mostly waiting for requests to finish. If you can gather a profile it will likely shed some light on why this takes place. Worst case, if you have a repro, it's worth to file an issue at https://github.com/dotnet/efcore. When you have time that is.
Re:Migrating to Postgres by ketzo on Wednesday May 14, 2025 @09:30PM (#43990954)
Not 100% parallel, but I was debugging a slow endpoint earlier today in our app which uses Mongo/mongoose.

I removed a $lookup (the mongodb JOIN equivalent) and replaced it with, as Prisma does, two table lookups and an in-memory join

p90 response times dropped from 35 seconds to 1.2 seconds

Re:Migrating to Postgres by nop_slide on Wednesday May 14, 2025 @11:36PM (#43991595)
Maybe because mongo isn’t ideal for relational data?
Re:Migrating to Postgres by wredcoll on Thursday May 15, 2025 @12:07AM (#43991759)
Does mongodb optimize joins at all? Do they even happen server side?
Re:Migrating to Postgres by merek on Thursday May 15, 2025 @03:13AM (#43992589)
I believe a lot of Mongo's criticisms come from people modelling highly relational data on a non-relational DB.
Re:Migrating to Postgres by rwyinuse on Thursday May 15, 2025 @11:02AM (#43995792)
I'm not sure what is the point of using MongoDB these days, when you can as easily store and query jsonb in postgres.
Re:Migrating to Postgres by lelanthran on Thursday May 15, 2025 @07:23AM (#43993858)
> I removed a $lookup (the mongodb JOIN equivalent)

There is no "MongoDB JOIN equivalent" because MongoDB is not a relationalal database.

It's like calling "retrieve table results sequentially using previous table's result-set" a JOIN; it's not.

Re:Migrating to Postgres by compton93 on Wednesday May 14, 2025 @08:23PM (#43990582)
It is. But wait... it doesn't join the data on the application level of your application. You have to deploy their proxy service which joins the data on the application level.
Re:Migrating to Postgres by Tadpole9181 on Wednesday May 14, 2025 @09:03PM (#43990805)
It's pretty obvious when somebody has only heard of Prisma, but never used it.

- Using `JOIN`s (with correlated subqueries and JSON) has been around for a while now via a `relationLoadStrategy` setting.

- Prisma has a Rust service that does query execution & result aggregation, but this is automatically managed behind the scenes. All you do is run `npx prisma generate` and then run your application.

- They are in the process of removing the Rust layer.

The JOIN setting and the removing of the middleware service are going to be defaults soon, they're just in preview.

Re:Migrating to Postgres by compton93 on Wednesday May 14, 2025 @09:07PM (#43990828)
They've been saying that for 3 years. We actually had a discount for being an early adopter. But hey its obvious Ive never used it and only heard of it.
Re:Migrating to Postgres by Tadpole9181 on Wednesday May 14, 2025 @09:20PM (#43990893)
The JOIN mode has been in preview for over a year and is slated for GA release within a few months. Which has been on their roadmap.

The removal of the rust service is available in preview for Postgres as of 6.7.[1]

Rewriting significant parts of a complex codebase used by millions is hard, and pushing it to defaults requires prolonged testing periods when the worst case is "major data corruption".

[1]: https://www.prisma.io/blog/try-the-new-rust-free-version-of-...

Re:Migrating to Postgres by compton93 on Wednesday May 14, 2025 @09:54PM (#43991103)
They've had flags and work arounds for ages. Not sure what point you are trying to make? But like you said I've never used it, only heard of it lol.
Re:Migrating to Postgres by paulddraper on Wednesday May 14, 2025 @09:53PM (#43991089)
It is hard.

Harder than just doing joins.

Re:Migrating to Postgres by williamdclt on Thursday May 15, 2025 @05:19AM (#43993225)
Honestly, everything you say makes me want to stay far from prisma _more_.

All this complexity, additional abstractions and indirections, with all the bugs gootguns and gotchas that come with it... when I could just type "JOIN" instead.

Re:Migrating to Postgres by Tadpole9181 on Thursday May 15, 2025 @01:52PM (#43997514)
Okay? It's one setting that will be the default in 2 months. And you could always write type-safe SQL manually instead.

i greatly envy y'all having projects where the biggest complexity is... A single setting once, that's clearly documented. We live in very different worlds, apparently.

Re:Migrating to Postgres by lesuorac on Thursday May 15, 2025 @08:59AM (#43994574)
Can't speak about Prisma (or Postgres much).

But I've found with that you can get better performance in _few_ situations with application level joins than SQL joins when the SQL join is causing a table lock and therefore rather than slower parallel application joins you have sequential MySQL joins. (The lock also prevents other parallel DB queries which is generally the bigger deal than if this endpoint is faster or not).

Although I do reach for the SQL join first but if something is slow then metrics and optimization is necessary.

Re:Migrating to Postgres by hermanradtke on Thursday May 15, 2025 @09:39AM (#43994980)
In what cases is your join causing a table lock?