I have a block of code that looks like this,
entities = self.session.query(Entities).filter(Entities.parent_id == 0)
index_data = {}
for entity in entities:
data = entity.__dict__
data['addresses'] = [address.__dict__ for address in entity.addresses]
data['relationships'] = [relationship.__dict__ for relationship in entity.relationships_parent]
index_data[data['id']] = data
I am trying to read the data, process it a little bit as shown above and store in the NoSQL database (I have only given the reading part here).
I noticed that this loop takes a really long time to complete. I suspect it is due to the amount of data I read with the first line of code? Maybe in terms of network delays? Because the memory utilization here is fine.
I need a solution using SQLAlchemy code.
I am thinking that maybe this could be solved by reading the data in chunks? How can I do this? If the problem is something else, how can I solve it?
entity.relationships_parentprobably sends another query for every entity. Try using other load strategy docs.sqlalchemy.org/en/14/orm/…