Google App Engine Datastore 中最有效的一对多关系? [英] Most Efficient One-To-Many Relationships in Google App Engine Datastore?

查看:32
本文介绍了Google App Engine Datastore 中最有效的一对多关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果这个问题太简单了;我才刚上九年级.

Sorry if this question is too simple; I'm only entering 9th grade.

我正在尝试了解 NoSQL 数据库设计.我想设计一个谷歌数据存储模型,最大限度地减少读/写次数.

I'm trying to learn about NoSQL database design. I want to design a Google Datastore model that minimizes the number of read/writes.

这是一个关于一对多关系中的博文和评论的玩具示例.哪个更有效 - 将所有评论存储在 StructuredProperty 中还是在 Comment 模型中使用 KeyProperty?

Here is a toy example for a blog post and comments in a one-to-many relationship. Which is more efficient - storing all of the comments in a StructuredProperty or using a KeyProperty in the Comment model?

同样,目标是最大限度地减少对数据存储的读/写次数.您可以做出以下假设:

Again, the objective is to minimize the number of read/writes to the datastore. You may make the following assumptions:

  • 不会独立于各自的博客文章检索评论.(我怀疑这使 StructuredProperty 成为最可取的.)
  • 评论需要按日期、评级、作者等进行排序.(数据存储中的子属性无法编入索引,所以这可能会影响性能?)
  • 博客帖子和评论在创建后都可能被编辑(甚至删除).

使用结构化属性:

from google.appengine.ext import ndb

class Comment(ndb.Model):
    various properties...

class BlogPost(ndb.Model):
    comments = ndb.StructuredProperty(Comment, repeated=True)
    various other properties...

使用 KeyProperty:

Using KeyProperty:

from google.appengine.ext import ndb

class BlogPost(ndb.Model):
    various properties...

class Comment(ndb.Model):
    blogPost = ndb.KeyProperty(kind=BlogPost)
    various other properties...

请随意提出与有效表示一对多关系相关的任何其他注意事项,以最大限度地减少对数据存储的读/写次数.

Feel free to bring up any other considerations that relate to efficiently representing a one-to-many relationship with regards to minimizing the number of read/writes to the datastore.

谢谢.

推荐答案

我可能是错的,但据我所知,StructuredProperty 只是实体中的一个属性,但带有子属性.

I could be wrong, but from what I understand, a StructuredProperty is just a property within an entity, but with sub-properties.

这意味着阅读博客帖子及其所有评论只需阅读一次.因此,当您呈现页面时,您只需对整个页面进行一次阅读操作.

This means reading a BlogPost and all its comments would only cost one read. So when you render your page, you only need one read op for your entire page.

写入也会更便宜.您需要一个读取操作来获取 BlogPost,并且只要您不更新任何索引属性,它就只是一个写入操作.

Writes would be cheaper each too. You'll need one read op to get the BlogPost, and as long as you don't update any indexed properties, it'll just be one write op.

您可以在从数据存储区读取实体后自行处理评论排序.

You can handle the comment sorting on your own after you read the entity out of the datastore.

您必须将您的评论更新/编辑与事务同步,以确保一个评论不会覆盖另一个评论,因为它们都在修改同一个实体.如果每个人同时评论和编辑同一篇博文,您可能会遇到无法解决的问题.

You'll have to synchronize your comment updates/edits with transactions, to make sure one comment doesn't overwrite another, since they are both modifying the same entity. You may run into unsolveable problems if everyone is commenting and editing the same blog post at the same time.

不过,在优化成本时,您会遇到最大实体大小为 1MB 的情况.这将限制您可以存储每篇博文的评论数量.

In optimizing for cost though, you'll hit a wall with the maximum entity size of 1MB. This will limit the number of comments you can store per blog post.

使用 KeyProperty 会贵很多.

Going with the KeyProperty would be quite a bit more expensive.

您需要阅读一次才能获取博文,外加 1 次查询和每条评论的 1 次小阅读操作.

You'll need one read to get the blog post, plus 1 query plus 1 small read op for each comment.

每条评论都是一个新实体,因此至少需要 4 次写入操作.您可能希望为排序顺序建立索引,这样最终会花费更多的写入操作.

Every comment is a new entity, so it'll be at least 4 write ops. You may want to index for sort order, so that'll end up costing even more write ops.

从好的方面来说,您可以在每篇博文中获得无限评论,您不必担心同步新评论.您可能需要担心编辑评论的同步,但如果您将编辑限制为创建者,那应该不是问题.您也不必自己整理.

On the plus side, you'll have unlimited comments per blog post, you don't have to worry about synchronizing new comments. You might need to worry about synchronization for editing comments, but if you limit the edit to the creator, that shouldn't really be a problem. You don't have to do sorting yourself either.

这是成本与功​​能的权衡.

It's a cost vs features tradeoff.

这篇关于Google App Engine Datastore 中最有效的一对多关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆