问题
We have a travel search website. To search for a bus, we execute a query on the bus operator entities. We use GAE Python NDB. The query fetch response time >55 seconds(on deployed version and not development server) when only ~50 entities are present.
Presently my query contains five items. If I increase to more than five, the response slows further considerably. Please suggest ways to bring down the query time to ~1 or 2 seconds or as less as possible.
Please find the relevant details below(sorry i tried to minimize the below content to some extent):
Query code:
start_time = datetime.datetime.now()
qry_1 = X.query(ndb.AND(X.active_status=="active", X.property_3==input_3, X.property_4==input_4, X.property_5==input_5, X.property_6.IN(input_6_list), X.property_20.IN(input_20_list)))
record_list = qry_1.fetch()
query_end_time = datetime.datetime.now()
query_execution_time = query_end_time - start_time
logging.info ("query_execution_time=["+str(query_execution_time)+"] ")
# input_6_list contains ~5 string items
# input_20_list contains ~5 string items
Output in Logs:
query_execution_time=[0:00:55.925250]
Entity Model:
class X(ndb.Model):
active_status = ndb.StringProperty()
name = ndb.StringProperty()
property_1 = ndb.StringProperty()
property_2 = ndb.TextProperty()
property_3 = ndb.StringProperty()
property_4 = ndb.StringProperty()
property_5 = ndb.StringProperty()
property_6 = ndb.StringProperty()
property_7 = ndb.StringProperty()
property_8 = ndb.StringProperty()
property_9 = ndb.StringProperty(repeated=True)
property_10 = ndb.StringProperty(repeated=True)
property_11 = ndb.StringProperty()
property_12 = ndb.StructuredProperty(P)
property_13 = ndb.StructuredProperty(Q)
property_14 = ndb.StringProperty()
property_15 = ndb.StructuredProperty(R, repeated=True)
property_16 = ndb.StructuredProperty(S, repeated=True)
property_17 = ndb.StringProperty()
property_18 = ndb.StringProperty(repeated=True)
property_19 = ndb.StringProperty()
property_20 = ndb.StringProperty(repeated=True)
property_21 = ndb.StringProperty(repeated=True)
property_22 = ndb.StructuredProperty(T, repeated=True)
property_23 = ndb.IntegerProperty(default=6)
property_24 = ndb.IntegerProperty(default=6)
property_25 = ndb.IntegerProperty(default=6)
property_26 = ndb.IntegerProperty(default=6)
property_27 = ndb.IntegerProperty(default=6)
property_28 = ndb.IntegerProperty(default=0)
property_29 = ndb.IntegerProperty()
date_added = ndb.DateTimeProperty(auto_now_add=True) #creation date
date_modified = ndb.DateTimeProperty(auto_now=True) #update date
property_30 = ndb.TextProperty()
property_31 = ndb.BlobKeyProperty()
property_32 = ndb.BlobKeyProperty()
property_33 = ndb.BlobKeyProperty()
property_34 = ndb.BlobKeyProperty()
property_35 = ndb.BlobKeyProperty()
property_36 = ndb.BlobKeyProperty()
property_37 = ndb.BlobKeyProperty()
property_38 = ndb.StringProperty()
property_39 = ndb.BlobKeyProperty()
property_40 = ndb.StringProperty(default="not_allowed")
While debugging this issue, I ran Appstats and had another question which I asked on SO
回答1:
Filtering on additional properties is not usually expensive. But using 'IN' is. 2 IN filters with lists of 5 items each requires 25x concurrent seeks on the backend.
Could you post the index.yaml file from your code directory? If this file does not exist, the query would require multiple JOINs, which would explain the slowness. Run the same query against the dev_appserver and it will generate the file automatically.
More here: https://developers.google.com/appengine/docs/python/config/indexconfig
By the way, using 'indexed=False' on the properties you don't intend to search on will reduce the cost puts considerably.
来源:https://stackoverflow.com/questions/22304351/gae-python-ndb-query-fetch-response-time-55-seconds-when-only-50-entities-are