由於數據存儲區對引用的實體執行附加查詢,因此我的應用程序中存在延遲問題。我通過使用get_value_for_datastore()函數收到了關於如何處理單值屬性的good advice。然而,我的應用程序也有一對多的關係,如下面的代碼所示,我還沒有找到預取這些實體的方法。嘗試顯示200個文檔及其相關文檔文件(> 6000毫秒)的表時,結果是不可接受的延遲。如何優化數據存儲區中的一對多查詢
(有可能永遠不會超過10000個文檔或DocumentFiles)
有沒有辦法解決這個問題的方法嗎?
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)
...
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)
...
main.py
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>'
謝謝。非正規化它是。這是一種士氣低落,不能使用文檔中承諾的乾淨設計,但這就是生活有時是如此。 –