Best way to get distinct values

2019-07-07 05:42发布

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条回答
ゆ 、 Hurt°
2楼-- · 2019-07-07 06:28

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.

查看更多
登录 后发表回答