logoalt Hacker News

dfeeyesterday at 8:02 PM1 replyview on HN

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)

Replies

kstrauseryesterday at 11:23 PM

I'm willing to look the other way when N=1, and 0..n is a small number of values on an indexed column. As others have pointed out, sometimes it's difficult to merge two queries like `get_item_list()` and `get_items()` located in different parts of the code, especially if they cross service boundaries. It's ugly, but may be a completely tolerable situation as long as timing profiles and usage patterns show that it's not going to blow things up. So there, 1+N => 1+1 which isn't inherently scary.

It's when 1+N turns into 147 queries, one for each line of a table displayed on a web page, that it really chafes.