所以这段代码:
all_nodes = Nodes.query()
country_nodes = []
for n in all_nodes:
country_nodes.append([n.country, all_nodes.filter(Nodes.country == n.country).count()])
刚刚取消了我的数据存储小型操作
配额,甚至没有完成操作?
获取上述列表的正确方法是什么?
最佳答案
在 GAE 中,当您编写新记录时,最好跟踪每个国家/地区的总数。然后您可以通过一次阅读来找出国家/地区的总数。例如,您可以添加新的模型类型:
class Country(db.Model):
name = db.StringProperty()
count = db.IntegerProperty()
然后,当添加新节点时,就可以得到对应的Country
记录并递增其 count
属性。
在您的示例中,当您执行 all_nodes.filter(...)
时,您正在为每个 n
运行一个新查询在all_nodes
。以下应该是计算总数的更便宜的方法。但当您编写新记录时,它可能比跟踪国家/地区总数更昂贵。
from collections import defaultdict
country_nodes = defaultdict(int)
for n in Nodes.query():
country_nodes[n.country] += 1
关于python - 意外用完 "Datastore Small Operations"配额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16522427/