subqueryload() generates redundant queries

Issue #2480 resolved
Former user created an issue

When subqueryload() is used on a relation of a polymorphic table, it generates/executes the exact same query multiple times. This hurts badly when that query is a heavy one. 0.6 and 0.7 shows the same behavior, but e.g. 0.7.0 generates just 2 identical queries in the attached example while 0.7.5 generates 3.

Comments (3)

  1. Mike Bayer repo owner
    • changed component to orm
    • marked as blocker
    • changed milestone to 0.7.8
    • assigned issue to

    great test. this is a side effect of something that might be improved within mapper loading overall at some point, but for now this is a patch that solves this issue for the case of subqueryloaders, as the "row processor" function created is much more expensive than any other.

    --- a/lib/sqlalchemy/orm/strategies.py  Fri May 04 23:18:52 2012 -0400
    +++ b/lib/sqlalchemy/orm/strategies.py  Tue May 08 10:41:17 2012 -0400
    @@ -864,7 +864,13 @@
    
             q = context.attributes[reduced_path)](('subquery',)
    
    -        collections = dict(
    +        # cache the loaded collections in the context
    +        # so that inheriting mappers don't re-load when they
    +        # call upon create_row_processor again
    +        if ('collections', reduced_path) in context.attributes:
    +            collections = context.attributes[reduced_path)](('collections',)
    +        else:
    +            collections = context.attributes[reduced_path)](('collections',) = dict(
                         (k, [v[0](v[0) for v in v]) 
                         for k, v in itertools.groupby(
                             q,
    
  2. Log in to comment