1

I'm starting to learn how to use SQLAlchemy and I'm running into some efficiency problems.

I created an object mapping an existing big table on our Oracle database:

engine = create_engine(connectionString, echo=False)

class POI(object):
    def __repr__(self):
        return "{poi_id} - {title}, {city} - {uf}".format(**self.__dict__)

def loadSession():
    metadata = MetaData(engine)
    _poi = Table('tbl_ourpois', metadata, autoload = True)
    mapper(POI, _poi)
    Session = sessionmaker(bind = engine)
    session = Session()
    return session

This table have millions of registries. When I do a simple query and try to iterate over it:

 session = loadSession()
 for poi in session.query(POI):
     print poi

I noticed two things: (1) it takes some minutes for it to start printing objects on the screen, (2) memory usage starts to grow like crazy. So, my conclusion was that this code was fetching all the result set in a list and then iterating over it. Is this correct?

With cx_Oracle, when I do a query like:

conn = cx_Oracle.connect(connectionString)
cursor = conn.cursor()
cursor.execute("select * from tbl_ourpois")
for poi in cursor:
    print poi

the resulting cursor behaves as an iterator that gets results into a buffer and returns them as they are needed intead of loading the whole thing in a list. This loop starts printing results almost instantly and memory usage is pretty low and constant.

Can I get this kind of behavior wiht SQLAlchemy? Is there a way to get a constant memory iterator out of session.query(POI) instead of a list?

Rafael S. Calsaverini
  • 13,582
  • 19
  • 75
  • 132
  • ok, this seem to be duplicate with http://stackoverflow.com/questions/1145905/scanning-huge-tables-with-sqlalchemy-using-the-orm?rq=1 but if you guys have something to add to the answers given there, please do. I'll mark it as a duplicate today in the afternoon if no new information is added. – Rafael S. Calsaverini Mar 25 '13 at 13:35
  • Does this answer your question? [SQLAlchemy: Scan huge tables using ORM?](https://stackoverflow.com/questions/1145905/sqlalchemy-scan-huge-tables-using-orm) – snakecharmerb Apr 03 '22 at 11:37

0 Answers0