Why is n + 1 choosing the pattern slow?

I'm pretty inexperienced with databases and just read about the "n + 1 selects issue" . My next question is: Assuming the database is on the same computer as my program, it is cached in RAM and indexed correctly, why is the query template n + 1 slow?

As an example, take the code from the accepted answer:

SELECT * FROM Cars;

/* for each car */
SELECT * FROM Wheel WHERE CarId = ?

With my mental database cache model, every request SELECT * FROM Wheel WHERE CarId = ?should need the following:

  • 1 find the Wheel table (one hash file get())
  • 1, to get a list of k wheels with the specified CarId(another hash file get())
  • k search to get wheel strings for each matching wheel (k-pointer-reverse)

Even if we multiply this by a small constant coefficient for additional overhead due to the structure of the internal memory, it should still be unobtrusively fast. Is interprocess communication a bottleneck?


Change . I just found this related article through Hacker News: Following the Select Statement through Internals Postgres. - The topic of discussion is HN .

Change 2 . To clarify, I assume it Nwill be great. Non-trivial service data will contain a noticeable delay, then yes. I ask why the overhead is not trivial in the first place, for the setup described above.

+4
source share
4

, n + 1 . , > 1 , .. , .

, , , > 0,01 (), .

, ORM, .

, n + 1, , , , n .

+5

, ,

. , , . , , . , ( ) . , , .

n + 1 ?

, , , , .

1) . 200-400 , - . CPU , (, , ).

2) " ". : . . , 76,6% http://lwn.net/Articles/255364/ ( , , RAM .)

"N + 1" "" N ( ), ( ).

"1 ", , ( ), ( ). , 10-100 , .

"1 " , , . .

"N + 1 " - N , ( ) .

TCP N.

DB N.

, "/ " N + 1, 1 .

, (, ), .

: http://blogs.msdn.com/b/oldnewthing/archive/2014/06/13/10533875.aspx

+3

; , .

. meriton. , . - , , ( ).

, .

, , , : , , .

. : , , , 100 , 10 10 ( ). SELECT 1 + N + (NxM). 100 , 1000 - . , 10000 100000.

, . , , 100, . , : , . , , : IF, .., , . , " ", , , .

, ORM N + 1: (N) Hibernate, , ( SELECT * FROM Wheels WHERE CarId=? SELECT * FROM Wheels WHERE CarId IN (?, ?, ..., ?)) ( : SELECT * FROM Wheels WHERE CarId IN (SELECT Id FROM Cars)).

, N + 1, - , , , /, , .

+1

, , . , , , , , ( ..) ..

N N . , N .

- dbms. - ( ISO, , ,...)

, .

All this largely depends on what the software is intended for. Avoiding “picking the n + 1 problem” is not always necessary; it's just an empirical rule to avoid a common trap.

0
source

All Articles