News for hackers. Stuff that matters.
Re:Ban 1+N in Django by paulddraper on Sunday March 26, 2023 @08:01PM (#35321113)
In my experience, the far more pernicious way to get N+1 is serializers (Django REST)
Re:Ban 1+N in Django by Rapzid on Sunday March 26, 2023 @08:20PM (#35321276)
Huh, ran into same issue on a rails project; queries kicked off in inside serializes were a weed. We toyed with throwing errors if a query was made during serialization.

On top of that, everything about serialization is terribly slow in Rails :|

Re:Ban 1+N in Django by Suor on Sunday March 26, 2023 @11:36PM (#35322580)
GraphQL makes it even more fun :)
Re:Ban 1+N in Django by Maxion on Monday March 27, 2023 @02:58AM (#35323664)
Ugh yes, nested serializers in DRF are always fun.

Almost makes you want to go noSQL.

Re:Ban 1+N in Django by jorl17 on Monday March 27, 2023 @10:08AM (#35326849)
A couple of years ago I wrote a set of utility classes (which inherit from Serializer and ViewSet) which "solve" this problem by inspecting serializers at the beginning of the request and figuring out what to pass to `select_related` and `prefetch_related` "automatically". It supports nested serializers, N-N, etc. Also lets you "help it" by saying "assume attribute X of a serializer accesses fields X,Y,Z of its object", for more "sophisticated" cases.

It's a very messy piece of code but it has survived many projects since I first wrote it 6 years ago. The day I enabled it at a previous job, we reduced a page load from 20s to 800ms or so just with it.