I some times use the SQLAlchemy ORM layer for read-only operations, typically for some sort of reporting task, something like:
for item in session.query(Item):
for relatedobject in item.relatedobjects:
print(item.id, item.name, relatedobject.id, relatedobject.name)
In this case:
- I never modify the entities in the unit of work, and
- The item -> relatedobject relationship is one-to-many (i.e. a relatedobject always belongs to a single item entity)
Question:
- Unless I am mistaken, all entities read from the db in the case above is tracked in memory by the SQLAlchemy session (for change tracking etc.)
- For some of these reporting loads, the python process uses a lot of memory due to this in-mem tracking (I believe)
- Given the case above, is there a way I can tell SQLAlchemy to not keep track of the entities in memory during the operation?
I do not want to use the SQLAlchemy expression layer, since I want to use the relationship functionality in the ORM layer.
question from:
https://stackoverflow.com/questions/65918721/sqlalchemy-memory-usage-can-i-tell-sqlalchemy-to-not-keep-track-of-entities-in 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…