ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kris Jenkins <krisajenk...@yahoo.co.uk>
Subject Re: What is N+1
Date Wed, 09 Feb 2005 12:21:16 GMT
Kris Jenkins wrote:

> friendVU admin wrote:
>
>> What is n+1?
>> .V
>
>
>
> n++!   :-P
>
> It's when each child object in a complex query has to be selected 
> seperately.  If 'parent' has 10 'children', you end up with 10+1 
> select statements being issued.  See the developer's guide under 
> 'Avoiding N+1 selects' for the full lowdown.  Version 2.0.9 comes with 
> new groupBy goodness to banish the N+1 problem.
>
> Kris

Yikes!  I'm not thinking straight this morning.  Sorry!

The N+1 selects problem is when you query for n parents, and you need 
one extra query for the children.  Internally, this happens:

    List parents = sqlMap.queryForList( "getParents", parameter );
    for ( Iterator i = parents.iterator(); i.hasNext(); ) {
       Parent p = (Parent) i.next();
       List children = sqlMap.queryForList( "getChild", p.getId() );
       p.setChildren( children );
    }

So you end up with 1 query for the parent list, plus one extra for each 
parent to get the children = n + 1.  Rather inefficient.
This is the problem that 2.0.9 fixes.

Sorry for the confusion.
Kris

-- 
Kris Jenkins
Email:  kris@jenkster.com
Blog:   http://cafe.jenkster.com/
Wiki:   http://wiki.jenkster.com/



Mime
View raw message