0

我正在Python中使用Google App Engine構建網站,但這個問題應該適用於任何類似於這個特定框架的類似問題。我正試圖在兩種相關數據模型之間做出決定。相關數據條目的最有效途徑

它們涉及一個Location數據庫類,每個Location實體都有多個與其掛鉤的Experience數據庫實體。

現在我在他們自己的memcache中擁有Locations類,並且我引用了存儲在expRef IntegerProperty中的每個Experience。然後,我執行Experience.get_by_id(expRef)以獲取所有體驗並在「位置」頁面上顯示它們。

class Location(ndb.Model): 
    #a bunch of other properties 
    expRefs = ndb.IntegerProperty(repeated=True) 
class Experience(ndb.Model): 
      #a bunch of other properties with no ref to Location 

#here is the handler for the Location page 
location = individual_location_cache(keyid) 
exps= [] 
      if location.expRefs != []: 
       for ref in location.expRefs: 
        records.append(Record.get_by_id(ref)) 

我想知道如果這是最好的選擇,或者倒不如給每個體驗到的位置屬性的引用,然後存儲所有Memcached中的引用,使兩個調用memcached的,一個是地點,然後是所有在那裏的經驗。

class Location(ndb.Model): 
     #a bunch of other properties but no ref to experiences 
class Experience(ndb.Model): 
     #a bunch of other properties 
     locationRef = ndb.IntegerProperty() 

#the Location page handler starts here 
location = individual_location_cache(keyid) 
exps= exp_location_cache(keyid) 

有沒有很大的差異或任何選項我忘了?

+0

使用代碼來定義問題總比使用自然語言更容易,您能提供一些您寫的內容的代碼以及您正在考慮的內容嗎? – bigblind

+0

好吧我認爲這應該有所幫助 – clifgray

回答

0

將你存儲它看起來不錯的方式 - 一個很明顯的事情,你會想,雖然優化,是作爲一個批次來獲取經驗:

exps= [] 
     if location.expRefs != []: 
      for future in [Record.get_by_id_async(ref) for ref in location.expRefs]: 
       records.append(future.get_result()) 

這樣NDB將嘗試獲取所有通過一個查詢獲得的體驗 - 並自動將它們緩存在memcache中。