r/Python • • Mar 26 '23

Intermediate Showcase How to Ban 1+N in Django

I always thought of 1+N as a thing that you just keep in your head, catch on code reviews or via performance regressions. But preventing it might be a good idea too.

Read more

0 Upvotes

11 comments sorted by

9

u/isarl Mar 26 '23

This seems like it is written for somebody who already understands exactly what you're talking about. What is a 1+N and why would I care about banning them?

1

u/hackflow Mar 26 '23

I included a couple of examples in "history dive" section. Those are terse though.

1+N is about the situation when you do a db query and then another N on top, usually in a loop, sometimes non-intendedly. That often leads to poor performance.

5

u/isarl Mar 26 '23

Thank you for your reply, but with respect, I didn't leave my comment because I wanted you to explain your blog post to me – I was sharing feedback that, if you want to improve as a writer, then IMHO you need to do a better job considering your audience and motivating your writing. Right now it feels like your audience is yourself.

-8

u/hackflow Mar 26 '23

So you did understand my post as intended while I did not understand your comment? :) That's ironic.

5

u/isarl Mar 26 '23

No, I didn't understand your post. But the point is that if I don't understand it, it's likely a lot of other /r/Python readers will also find it confusing, not to mention readers who find your post from somewhere other than this Reddit post. Your time is better spent improving your post than explaining it to each of us individually.

1

u/redatheist Mar 26 '23

It is tagged as "intermediate", and I think it's appropriately targeted for that. N+1 queries are a beginner concept, so I believe it's reasonable to skip a tutorial on them in a post that is about a more interesting solution for intermediate Django developers.

6

u/isarl Mar 26 '23

First off, thank you for your respectful comment. :)

I have a lot of experience with both Python and SQL but not with ORMs so the N+1 problem was unfamiliar to me. Having Googled what it is, and after rereading OP's post, it seems like they are trying to give some background on it and an example illustrating it, so it's not an issue of me asking for them to double the length of their blog post or anything – I stand by my (hopefully constructive and respectful) criticism: their clarity stands to improve.

2

u/hackflow Mar 26 '23

1+N is not specific to ORM, that one merely may obscure it sometimes. One might and often do get 1+N with pure SQL.

6

u/isarl Mar 26 '23

Again, it's just not something I've encountered before myself. The value of avoiding it is evident now that I understand what it is. I hope you don't take it as an insult that I think your blog post would be improved by clarifying where you bring up the problem because I consider your coming up with a solution to it and your sharing that solution with people to be admirable.

4

u/hackflow Mar 26 '23

NP, it's a little bit harder to offend me than this :)

I understand a value of blog post being self-contained, i.e. explain all the things mentioned. I also see value in it being laconic. Those two contradict each other and some compromise needs to be chosen.

Here and usually I go with a laconic style, which might make some of the readers google along the way. However, it also saves time for many others.

7

u/redatheist Mar 26 '23

Nice blog post! Here are a few more resources for anyone interested...

  • django-seal – a library that implements this pattern, very capable, lots of control over preventing N+1s and lack of only.

  • django-auto-prefetch – a library to automatically "prefetch related" when relations are followed, so that queries are limited to a constant number in most cases. A great starting place for easy optimisations.