Hacker News new | ask | show | jobs
by jawns 700 days ago
I've spent the last couple of years identifying and resolving N+1 problems in a Django codebase.

https://planetscale.com/blog/what-is-n-1-query-problem-and-h...

Aside from the performance gains, it's very satisfying to go from 1,000+ inefficient DB queries to 1-2 optimized queries.

1 comments

That's a well written article. For developers newer to relational databases, there's a heuristic at the beginning which I remember hearing elsewhere and keeping it in mind when I'm doing query work.

"You might expect that many small queries would be fast and one large, complex query will be slow. This is rarely the case. In practice, the opposite is true."

It's a great heuristic.

A big part of why it works out that way is that the query planner can only optimize for the query you give it. If you give it a bunch of small queries, it can only make relatively inconsequential micro-optimizations. The one big query gives it a lot more degrees of freedom and opportunities to make big gains.

Here's another great resource for getting more out of relational databases: https://use-the-index-luke.com/