获得不同值的最佳方法 [英] Best way to get distinct values
问题描述
我正在使用 Google-Appengine-NDB.我试图从数据库中获取不同的值,但它不起作用.
<前>现在我的代码是:query_set = cls.query().order(cls.ls) # 获取有序查询.set_of_field = set([data.field for data in query_set]) # 并使用此循环进行不同.`但是循环花费的时间太长(超过 12 秒).请帮助我,我怎样才能加快速度,或者如何从 ndb 中获取不同的值?
尝试不同的查询,如果您的字段已编入索引,您可以使用: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]
但是,如果您有一个庞大的列表,您可以在任务队列中执行此操作并将结果存储在某处,或者仅将不同的数据保存在另一个模型中.
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?
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.
这篇关于获得不同值的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!