python - 意外用完 "Datastore Small Operations"配额

标签 python google-app-engine

所以这段代码:

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/

相关文章:

python - 如何使用 alpha channel 给 png 图像着色?

google-app-engine - 玩! 2.0 和谷歌应用引擎

java - GQL 仅适用于 Python 项目而不适用于 Java?

python - 无法在 Flask 单元测试中设置 session 变量

python - 用 numpy 拟合数据

python - 在引用 pandas DataFrame 的下一个元素时省略循环

php - Google App Engine 提供 Content-Length header

google-app-engine - App Engine 应用程序计划在 7 天内尽快删除

iphone - iOS 应用程序的文件服务器

python - 使用 numpy.savetxt() 将数据写入 csv 时缺少第一个条目