Most efficient way to serialize PonyORM query or a list of objects

456 Views Asked by At

I have a FastAPI microservice and I'm using Pony as the ORM to connect to PostgreSQL and currently, I'm using Pydantic to serialize objects and lists of objects to send to the user.

But recently I have found that the way FastAPI and Pydantic serialize lists is not efficient much. The way that FastAPI handles the serialization of lists is to iterate it and serialize each object, and I understand why they chose this, but this has too much time overhead when you use PonyORM (I do not know why).

Now, I'm looking for ideas to make data (and especially queries) serialization faster. By query serialization, I mean for example getting 5 entries from DB and serializing them without making a list out of it and iterating it. I know I have to finally iterate something somewhere, but iterating lists does not seem a good solution. I also have tried to_dict() method implemented in Pony but that didn't do much.

Note: I'm not looking for cache solutions, I want to know if there is some low-level solution for such problem. At this point, I do not know if I'm asking the right question!

0

There are 0 best solutions below