问题描述
限时送ChatGPT账号..考虑博客数据建模的经典示例,其中我们有一个具有许多属性的 Blog
实体,我们希望列出页面中的最新博客.
Consider the classic example of blog data modelling, where we have a Blog
entity with many properties, and we want to list the latest blogs in a page.
将 BlogPost
实体非规范化为一个 BlogPostSummary
实体是有意义的,该实体将显示在列表视图中,避免获取和反序列化许多不需要的属性.
It makes sense to denormalize the BlogPost
entity into a BlogPostSummary
entity which will be shown in the list view, avoiding fetching and deserializing many unwanted properties.
class BlogPost(db.Model):
title = db.StringProperty()
content = db.TextProperty()
created = db.DateProperty()
...
class BlogPostSummary(db.Model):
title = db.StringProperty()
content_excerpt = db.TextProperty()
问题是:哪个实体应该拥有索引属性?有 3 个选项:
The question is: which entity should hold the indexed properties? There are 3 options:
优点:轻松查询两个实体. 维护非规范化索引的成本很高. 优点:主实体中的索引属性更安全,因为非规范化实体被视为冗余. 查询列表视图需要对数据存储进行两次往返:一次是对BlogPost
实体的仅键查询,然后是对 BlogPostSummary
的批量获取.
Querying the list view will need a double roundtrip to datastore: One to key-only query for BlogPost
entities, followed by a batch get for BlogPostSummary
.
优点:可以通过单个查询轻松构建列表视图.
不能再通过这些属性查询主实体.当非规范化实体是主实体的子实体时,索引占用更多空间.
哪个选项会更好?还有其他选择吗?
Which option would work better? Are there other options?
选项 2 中到数据存储的双程往返会不会有问题?
Would the double round trip to datastore in option 2 be a problem?
推荐答案
这是一个没有正确"答案的抽象问题.数据模型的选择取决于项目的具体要求,包括:
This is an abstract question that does not have a "correct" answer. The choice of a data model depends on specific requirements of a project, including:
使用模式(您需要访问不同数据的频率)更新模式(例如,将频繁更新的属性与稳定的属性分开以降低写入成本)平均性能和极端情况下的性能要求(例如,一个普通博客可能有 10 个帖子,一个非常受欢迎的博客可能有 10,000 个帖子)能够使用 memcache 来减少数据存储访问并提高性能数据复杂性(即有多少不同的实体孩子取决于这种特定的实体类型)交易要求安全和访问角色注意事项(例如,不要错误地暴露私人数据)顺便说一下,还有另一种方法可以在 Datastore 中对数据进行建模 - 使用子实体.例如,博客帖子可能是博客实体的子实体.通过这种方式,您可以通过提供父键通过单个查询检索所有博客帖子 - 无需在博客实体中存储帖子 ID 或键,或者在帖子实体中存储博客 ID/键.
By the way, there is another way to model data in the Datastore - using child entities. For example, blog posts may be child entities of a blog entity. This way you can retrieve all blog posts with a single query by providing a parent key - without storing post IDs or keys in the blog entity or blog ID/key in the post entities.
这篇关于App Engine 数据存储非规范化:主实体或非规范化实体中的索引属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论