Best way to get distinct values

China☆狼群 提交于 2020-01-04 05:13:04

问题


I'm using Google-Appengine-NDB. And I'm tried to get distinct values from database but it's not working.

Now my code is:
  query_set = cls.query().order(cls.ls) # Getting ordered queries.
  set_of_field = set([data.field for data in query_set]) # And using this loop for differ.`

But the loop is taking too long time (over 12 sec). Please help me, how can I speed up, or how to get distinct values from ndb?


回答1:


Try the distinct query, if your field is indexed you can use this: https://developers.google.com/appengine/docs/python/ndb/queries#projection

query_set = cls.query(projection=["field"], distinct=True)
set_of_field = [data.field for data in query_set]

But if you have a huge list you can either do this in a taskqueue and store the results somewhere or just keep a distinct data in another model.



来源:https://stackoverflow.com/questions/18523770/best-way-to-get-distinct-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!