Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

python - SQLAlchemy memory usage: Can I tell SQLAlchemy to NOT keep track of entities in the session?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...