簡體   English   中英

parent-> appengine python中的子關系(bigtable)

[英]parent->child relationships in appengine python (bigtable)

我還在學習bigtable / nosql中關於數據建模的課程,並希望得到一些反饋。 如果我經常需要與父母一起處理這些孩子,那么我的數據建模中是否應該避免父母與子女的關系,這是否公平?

舉個例子,假設我正在建立一個博客,該博客將由許多作者提供,彼此有帖子,每個帖子都有標簽。 所以我可能會設置這樣的東西:

class Author(db.Model): 
  owner = db.UserProperty()

class Post(db.Model): 
  owner = db.ReferenceProperty(Author, 
    collection_name='posts') 
  tags = db.StringListProperty() 

據我了解,這將創建一個基於作者父級的實體組。 如果我主要需要通過標簽查詢Posts,這會導致效率低下嗎?

我理解對列表屬性進行查詢可能效率低下。 假設每個帖子平均有大約3個標簽,但可以一直到7個。我希望我的可能標簽集合可以達到數百個。 將模型改為這樣的東西有什么好處嗎?

class Author(db.Model): 
  owner = db.UserProperty()

class Post(db.Model): 
  owner = db.ReferenceProperty(Author, 
    collection_name='posts') 
  tags = db.ListProperty(db.Key)

class Tag(db.Model): 
  name = db.StringProperty() 

或者我會做這樣的事情會更好嗎?

class Author(db.Model): 
  owner = db.UserProperty()

class Post(db.Model): 
  owner = db.ReferenceProperty(Author, 
    collection_name='posts')

class Tag(db.Model): 
  name = db.StringProperty() 

class PostTag(db.Model): 
  post = db.ReferenceProperty(Post, 
    collection_name='posts') 
  tag = db.ReferenceProperty(Tag, 
    collection_name='tags') 

最后一個問題......如果我最常見的用例是通過多個標簽查詢帖子怎么辦? 例如,“在{'apples','oranges','cucumbers','bicycles'}中查找帶有標簽的所有帖子” 這些方法中的一種更適合查找具有任何標簽集合的帖子的查詢嗎?

謝謝,我知道那是滿口的。 :-)

像第一種或第二種方法的東西非常適合App Engine。 請考慮以下設置:

class Author(db.Model): 
  owner = db.UserProperty()

class Post(db.Model): 
  author = db.ReferenceProperty(Author, 
    collection_name='posts') 
  tags = db.StringListProperty()

class Tag(db.Model): 
  post_count = db.IntegerProperty()

如果使用字符串標記(case-normalized)作為Tag實體key_name,則可以有效地查詢具有特定標記的帖子,或列出帖子的標記或獲取標記統計信息:

post = Post(author=some_author, tags=['app-engine', 'google', 'python'])
post_key = post.put()
# call some method to increment post counts...
increment_tag_post_counts(post_key)

# get posts with a given tag:
matching_posts = Post.all().filter('tags =', 'google').fetch(100)
# or, two tags:
matching_posts = Post.all().filter('tags =', 'google').filter('tags =', 'python').fetch(100)

# get tag list from a post:
tag_stats = Tag.get_by_key_name(post.tags)

第三種方法需要對大多數基本操作進行額外的查詢或提取,如果要查詢多個標記則更加困難。

我會選擇最后一種方法,因為它允許直接檢索給定標簽的帖子列表。

第一種方法基本上使得不可能保留規范的標簽集。 換句話說,“系統中當前存在什么標簽”的問題是非常昂貴的。

第二種方法修復了這個問題,但正如我所提到的,它無法幫助您檢索給定標記的帖子。

實體組有點神秘,但只要說第一種方法不創建實體組就足夠了,它們只是事務數據庫操作所必需的,有時對優化數據讀取很有用,但可能不需要小應用。

應該提到的是,您采取的任何方法只能與智能緩存策略配合使用。 GAE應用程序喜歡緩存。 熟悉memcache api,了解memcache和數據存儲區的批量讀/寫操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM