我需要能够更快地搜索我的用户。没有memcache的搜索需要8秒。现在我的代码在Not Dot Net的帮助下使用了memcache,将搜索时间缩短到4秒。我现在的问题是,我怎样才能让它更快?
qUserSearch = Utilities.deserialize_entities(memcache.get("qUserSearch"))
if not qUserSearch:
qUserSearch = db.GqlQuery("SELECT * FROM User ORDER BY created DESC").fetch(100000)
memcache.add("qUserSearch", Utilities.serialize_entities(qUserSearch))
searchLength = len(searchData)
hits = []
gotHits = False
for u in qUserSearch:
searchEmail = u.email[0:searchLength]
if searchEmail == searchData:
hits.append( u.key() )
else:
# Since I only search for the first x chars
# will there never be hits after it's had hits
if gotHits:
break
return hits
我的第一个想法:
或者您有其他想法吗?您曾经认为我会节省大部分时间吗?
答案 0 :(得分:3)
您还可以使用两个不等式过滤器模拟您尝试完成的前缀搜索
db.GqlQuery("SELECT * FROM User WHERE email >= :1 AND email <= :2", searchData, unicode(searchData) + u"\ufffd")
注意:这个答案来自为问题Google App Engine: Is it possible to do a Gql LIKE query?提供的第二个答案。