由于数据存储对引用的实体进行额外查询,我的应用程序出现延迟问题。我收到了关于如何使用 get_value_for_datastore() 函数处理单值属性的好建议。但是我的应用程序也有一对多的关系,如下面的代码所示,我还没有找到预取这些实体的方法。结果是在尝试显示包含 200 个文档及其相关文档文件的表格时出现不可接受的延迟 (>6000ms)。
(可能永远不会超过 10.000 个文档或 DocumentFiles)
有没有办法解决这个问题?
模型.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)
...
class DocumentFile(db.Model):
description= db.StringProperty()
blob = blobstore.BlobReferenceProperty()
created = db.DateTimeProperty() # needs to be stored here in relation to upload / download of everything
document = db.ReferenceProperty(Document, collection_name = 'files')
@property
def link(self):
return '<a href="/file/serve/%s">%s</a>' % (self.key().id(),self.blob.filename)
...
主文件
docUsers = DocUser.all()
docUsersNameDict = dict([(i.key(), i.name) for i in docUsers])
documents = Document.all()
for d idocuments:
out += '<td>%s</td>' % d.title
docUserKey = Document.lastEditedBy.get_value_for_datastore(d)
out +='<td>%s</td>' % docUsersNameDict.get(docUserKey)
out += '<td>'
# Creates a new query for each document, resulting in unacceptable latency
for file in d.files:
out += file.link + '<br>'
out += '</td>'