Querying the database with SQLAlchemy, especially at the relationship level, can get very expensive. Sometimes you only want certain columns to be retrieved instead of the entire row. That's why functions like with_entities
and load_only
have been created.
There's already a SO question outlining the differences between the two here.
To be specific, I am talking at a relationship level. For example:
Post.comments.options(load_only('id', 'content', 'created_at')).all()
versus
Post.comments.all()
My question is how big is the performance benefit of loading only a subset of properties, versus loading the entire row?
You can assume that comments
doesn't have any other relationship attached for it to load. I'm curious how this affects at 100 comments as well as 10 million comments.
Also, ignore pagination. I'm really interested in the isolated benefit of load_only
or with_entities
.