logoalt Hacker News

kstrauseryesterday at 5:20 PM5 repliesview on HN

Side note: I strongly prefer referring to this as the "1+N problem" as the author did here. I didn't understand what people were grousing about when they talked about "N+1".

N+1: You're already doing N queries. Is adding 1 more that big of a deal?

1+N: This should have been 1 query, but somehow you blew it up into that one plus N more.

I'd seen that query antipattern plenty of times and knew what it was bad, but didn't realize that's what people meant by "N+1", which I thought must mean something different.


Replies

LanceHyesterday at 8:47 PM

Algebraically, it is the number of queries: n+1. That's traditionally how you write such a number, not 1+n.

It's the number of queries not a sentence of "we did 1 query, then we had to do n queries."

show 1 reply
libriayesterday at 5:47 PM

You're not the only one. I never stopped to delve into what this N+1 problem was b/c I assumed it was never an issue for me. All these years and this is the 1st time I've finally understood what they were saying.

However, after going back and forth with LLM on it just now, I feel like "1+N" is just a coding mistake, not a perplexing multi-faceted, engineering problem to be solved. Experience or a slow application would teach you to find a better way to get that info and then you move on.

show 3 replies
dfeeyesterday at 8:02 PM

N+1 is at best two queries, right? I always interpreted it as:

  query 0 = list
  query 1_0 = getItem(0)
  ...
  guest 1_n = getItem(n)
optimized to:

  query 0 = list
  query 1 = getItems(0..n)
show 1 reply
hnarayananyesterday at 6:22 PM

Thank you. I turn grumpy when my colleagues keep calling it N+1. What even.

mwigdahlyesterday at 6:54 PM

But addition is commutative! :)

show 1 reply