【发布时间】:2012-04-28 23:02:38
【问题描述】:
根据 AppStats,当使用 200 个文档和 1 个 DocUser 运行以下脚本时,脚本大约需要 5000 毫秒。罪魁祸首是 lastEditedBy (datastore_v3.Get) 的每次锁定都会向数据存储发出请求,每次锁定需要 6-51 毫秒。
我正在尝试做的事情是使显示具有多个属性的许多实体成为可能,其中一些属性是从其他实体派生的。永远不会有大量实体(
我尝试通过缓存 DocUser 实体来进行优化,但如果不对数据存储区发出新请求,我无法从上面的查询中获取 DocUser 键。
1) 这有意义吗 - 我遇到的延迟正常吗?
2) 有没有办法在不向数据存储区发出额外请求的情况下完成这项工作?
models.py
class Document(db.Expando):
title = db.StringProperty()
lastEditedBy = db.ReferenceProperty(DocUser, collection_name = 'documentLastEditedBy')
...
class DocUser(db.Model):
user = db.UserProperty()
name = db.StringProperty()
hasWriteAccess= db.BooleanProperty(default = False)
isAdmin = db.BooleanProperty(default = False)
accessGroups = db.ListProperty(db.Key)
...
main.py
$out = '<table>'
documents = Document.all()
for i,d in enumerate(documents):
out += '<tr><td>%s</td><td>%s</td></tr>' % (d.title, d.lastEditedBy.name)
$out = '</table>'
【问题讨论】:
-
你为什么要循环访问文档,然后每次都在循环中获取所有文档?您的 main.py 有问题。
-
对不起,我的示例代码中有错误。现已修复。
标签: python google-app-engine google-cloud-datastore