Lazy loading advantages and disadvantages

416 views
Skip to first unread message

Adam M Peacock

unread,
Feb 22, 2007, 12:26:25 AM2/22/07
to sqlal...@googlegroups.com
Is there a difference in the SQL executed when using lazy vs eager loading?  Specifically, if I use eager loading will everything be queried at once with a more efficient join, or will it still use the lazy style (as far as I understand it) of generating a ton extra queries as it loads each relation separately?  If it is the former, more efficient case (an eager relation uses a join) is it possible to override the loader type at query time, such as being lazy by default but being nice to the database when I know I'm going to need all the data from the relation (especially if I'm calling a  couple thousand rows for a report)?

Adam M Peacock
UConn Ballroom Webmaster


King Simon-NFHD78

unread,
Feb 22, 2007, 5:58:03 AM2/22/07
to sqlal...@googlegroups.com
Adam M Peacock wrote:
> Is there a difference in the SQL executed when using lazy vs eager
> loading? Specifically, if I use eager loading will everything be
> queried at once with a more efficient join, or will it still use the
> lazy style (as far as I understand it) of generating a ton extra
> queries as it loads each relation separately? If it is the former,
> more efficient case (an eager relation uses a join) is it possible to

> override the loader type at query time, such as being lazy by default

> but being nice to the database when I know I'm going to need all the
> data from the relation (especially if I'm calling a couple thousand
> rows for a report)?

Eager loads are performed using a join, so only a single query is
issued. You can change the eager/lazy behaviour at query time by using
the 'options' method on the query object.

See
<http://www.sqlalchemy.org/docs/datamapping.myt#datamapping_selectrelati
ons_eagerload> and
<http://www.sqlalchemy.org/docs/datamapping.myt#datamapping_selectrelati
ons_options> for more.

Hope that helps,

Simon

Adam M Peacock

unread,
Feb 23, 2007, 10:43:26 PM2/23/07
to sqlal...@googlegroups.com
Thank you, that is exactly what I was looking for.

Adam M Peacock

Reply all
Reply to author
Forward
0 new messages