Scille / umongo

sync/async MongoDB ODM, yes.
MIT License
446 stars 63 forks source link

How to reduce time when query doc from mongo with motor driver #360

Open luckystar1992 opened 3 years ago

luckystar1992 commented 3 years ago

Here are my doc defination:


from motor.motor_asyncio import AsyncIOMotorClient
from umongo.frameworks import MotorAsyncIOInstance

motor_instance = MotorAsyncIOInstance()
database = AsyncIOMotorClient(uri)
motor_instance.set_db(database['collection_name'])

@motor_instance.register
class ChildChildDoc(EmbeddedDocument):
    p1 = fields.Str(required=True)
    ....
    pn = fields.Str(required=True)

@motor_instance.register
class ChildDoc(EmbeddedDocument):
    p1 = fields.Str(required=True)
    ....
    p_another_child_list =  fields.ListFields(fields.EmbeddedFields(ChildChildDoc))

@motor_instance.register
Doc1(Document):
    p1 = fields.Str(required=True)
    ....
    p_child_list= fields.ListFields(fields.EmbeddedFields(ChildDoc))

When i have a query calling in my hanlder

#methods1 using the OO query:
result = Doc1().find({"p1" : {"$in": ["l1","l2","l3"]}})
for doc in await result.to_list(length=100):
     other coding

# methods2 using motor async query:

cursor = collection.find({"p1" : {"$in": ["l1","l2","l3"]}})
for doc in await result.to_list(length=100):
     other coding

Both methods return data successuful, but the first method take more time than the second method(700ms v.s. 50ms). I think the deserialization process in each iteration spends time in the first method.

Is there any problem in my code. If not, how can I change my method to reduce the running time.