I am trying to create many objects. Currently it is done by calling session.add()
then session.flush()
upon each object being created. This is bad because there are over 500+ objects being created so each of them performs a query which affects performance. I am trying to optimise this.
To give some background, here is how my object roughly looks like:
class Metric:
data: str
fk_document_id: int (relationship)
fk_user_id: int (realtionship)
Key thing to highlight, there are 2 relationships in this object, implemented using the relationship
property that SQLAlchemy offers.
My first go was to change the code to use bulk_save_objects()
however this method does not save the relationships and also requires a session.commit()
which I'd ideally not do because there is a chance this transaction could get rolled back after the creation of the Metric
objects.
Then I found add_all()
however this performs an add()
for each object anyways, so I do not think it will impact the current performance much.
I can't see any other approach, so my question is if there is a way in this case to use bulk_save_objects()
to persist relationships and possibly not require a commit. Alternatively, if there is another method to do this that I do not see, please let me know.