orm - What is N+1 SELECT query issue? -
select n+1 stated problem in object-relational mapping (orm) discussions, , understand has having make lot of database queries seems simple in object world.
does have more detailed explanation of problem?
let's have collection of car
objects (database rows), , each car
has collection of wheel
objects (also rows). in other words, car
-> wheel
1-to-many relationship.
now, let's need iterate through cars, , each one, print out list of wheels. naive o/r implementation following:
select * cars;
and for each car
:
select * wheel carid = ?
in other words, have 1 select cars, , n additional selects, n total number of cars.
alternatively, 1 wheels , perform lookups in memory:
select * wheel
this reduces number of round-trips database n+1 2. orm tools give several ways prevent n+1 selects.
reference: java persistence hibernate, chapter 13.
Comments
Post a Comment