On Dec 14, 2008, at 2:45 PM, robert rottermann wrote:

>
> Hi there,
>
> I have a query like:
>
> sesion.query(MytableTable)
>
> which returns a huge numer of recors.
> now I would like to iterate trough the records.
> What method should I use?
>
> sesion.query(MytableTable).all() returns all
> and next or fetchone is not defined.
>
> thanks for your help
> robert


The Query is already an iterator and can be used as such.  However it  
prefetches the full result by default, since the ORM must complete its  
bookkeeping on any ORM-mapped object instance before returning it, and  
it's very common that a single instance will appear multiple times in  
a result set - such as if the query is joining multiple tables  
together, multiple rows which contain the same primary key columns  
will be translated to a single object instance in the identity map.

If you'd like to iterate through a very large result set without  
memory growing to the size of the result, you can use  
query.yield_per(<some number of rows>) and iterate that - "some number  
of rows" will be prefetched at a time.   When using this option, you  
will have to ensure that either A. the query returns only individual  
columns and not ORM mapped objects or B. the query's statement is  
guaranteed to represent each returned ORM instance uniquely - once an  
ORM mapped instance is yielded, if the Query encounters it again it  
will process it as though it were new and may cause undefined behavior.

As an alternative, use LIMIT/OFFSET options to limit the size of the  
result set represented by a single statement execution/result iteration.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to