I'm running a keys_only query, which fetches 20 results.
result_keys, cursor, more = ActivityIndex.query(cls.followers == key)\
.order(-cls.date_created)\
.fetch_page(num_results,
start_cursor = cursor,
keys_only=True)
I then get the parents of the activityIndex objects:
keys = []
for k in result_keys:
for pair in k.parent().pairs():
keys.append(ndb.Key(pairs=[pair]))
activities_related = ndb.get_multi(keys)
I thought this would be quick, because I was getting a batch of objects by key. However, the query seems call datastore_v3.Next
, which according to the appstats docs is "bad", and takes up a significant portion of the execution time.
Avoiding unnecessary Next calls may speed up your app!
Appstats for the above query (with get_multi call)
Appstats for the above query but without call get_multi (short time for next() to respond).
Why does the datastore_v3.next()
call take so long to execute when calling get_multi()
? Does it depend on the number of results get_multi will return? Some of the objects returned in get_multi have list properties (with max 10 items in a list), would this play a role in the performance?
To avoid this issue, would it be better to change the design and fetch the required entities in tasklets? Any other suggestions?
Edit:
A bit more information about what I am trying to do:
I have an activity stream in my application, which displays all user activity e.g. Rob commented on Picture etc.
To display this information, I thought I'd need the User object and the Picture Object to build up the activity description and information to display. I set all these objects' keys as parents of ActivityIndex
. So from the above query, ndb.get_mult(keys)
would be fetching the Activity, User and Picture objects. The keys list could contain 50 keys or more, so would this possibly be the cause of the long datastore_v3.Next
call.