use either as suits the current situation.
if you need a list to pass around elsewhere, use all(). if you are consuming the results right there, iterate. burgiduroy wrote: > So which is the best practice? "for row in query" or "for row in > query.all()" > > On Thu, Jul 12, 2012 at 8:54 PM, Michael Bayer > <mike...@zzzcomputing.com>wrote: > >> >> On Jul 11, 2012, at 8:27 PM, Gunnlaugur Briem wrote: >> >> Hi burgiduroy, >> >> On Wednesday, 11 July 2012 15:24:59 UTC, burgiduroy wrote: >>> >>> Are there any performance difference between the two? >>> >>> for row in query_object.all(): >>> do_something() >>> >>> AND >>> >>> for row in query_object: >>> do_something() >>> >> >> The first fetches and processes all rows in one go before executing the >> loop body. The second streams them in. >> >> How much that really matters depends on the DBAPI (it may fetch all rows >> in one go anyway), and maybe on how heavy the object mapping is ... and >> on >> whether you end up exiting your loop early (and so avoid processing the >> rest unnecessarily). >> >> >> Well, assuming this is an ORM Query, the Query object internally >> processes >> all the rows before yielding them regardless, unless you used the >> yield_per() method to configure that it should process them in batches. >> Unfortunately the Query can't unconditionally yield rows at a time due >> to >> the fact that the same mapped object can be present multiple times >> across >> many rows, as a result of explicit joins as well as loads of related >> collections, and mapped objects are mutable. >> >> So normally there's no real difference in memory usage between >> iter(query) >> and query.all(), and none for performance except for the small overhead >> of >> the extra list(self) that query.all() uses. >> >> -- >> You received this message because you are subscribed to the Google >> Groups >> "sqlalchemy" group. >> To post to this group, send email to sqlalchemy@googlegroups.com. >> To unsubscribe from this group, send email to >> sqlalchemy+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/sqlalchemy?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To post to this group, send email to sqlalchemy@googlegroups.com. > To unsubscribe from this group, send email to > sqlalchemy+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/sqlalchemy?hl=en. > > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.