- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在调查 Firebase 数据库 sample对于安卓并意识到它以下列方式存储其数据:
我不太熟悉 NoSQL 技术并试图理解为什么我们必须坚持每个 post
实体两次 - 在 posts
和 user_posts
相应地。文档说这种方法称为“扇出”,我完全同意通过像 databaseReference.child("user-posts").child("<user_uid>")
这样的简单构造来访问用户的帖子可能很有用。 .但是为什么我们需要 posts
节点呢?如果我们需要更新一些帖子怎么办 - 我们必须更新两次吗?
// [START write_fan_out]
private void writeNewPost(String userId, String username, String title, String body) {
// Create new post at /user-posts/$userid/$postid and at
// /posts/$postid simultaneously
String key = mDatabase.child("posts").push().getKey();
Post post = new Post(userId, username, title, body);
Map<String, Object> postValues = post.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/posts/" + key, postValues);
childUpdates.put("/user-posts/" + userId + "/" + key, postValues);
mDatabase.updateChildren(childUpdates);
}
// [END write_fan_out]
所以我想知道...这种方法何时有用,何时无用? Firebase SDK 是否提供任何工具来在更新或删除数据时使所有重复项保持同步?
更新:这是解释 received来自 Firebase 团队:
the reason the posts are duplicated is because we want to be able to quickly get all the posts belonging to a user (as you suggested) and filtering from the list of all posts ever to get the posts by one user can get pretty expensive as the number of posts expands.
This does mean that we have to update the post in two locations whenever we update it. It makes the code a little uglier but since queries are more common than writes it's better to optimize for reading the data.
我怀疑这种方法可能看起来不太优雅,但它可能是大型数据集的最快选择,只要您执行 SELECT 的频率高于 UPDATE。但是,在某些情况下,我宁愿坚持使用此处推荐的其他解决方案。
最佳答案
数据扇出是一种管理大量数据的好方法。如果不使用此模式,将来可能会遇到严重的缩放问题。
我从您的数据库结构中看到,您将整个帖子信息存储两次,这不是一个好的做法。您希望在另一个节点下存储只是对帖子的引用。因此,您将有一个名为 users-posts
的节点,它将包含用户键,并且每个键都有一组值为 true
的帖子键。为了更清楚:
这样,您就可以跟踪用户在 users-posts
节点下写了哪些帖子;以及在 posts
节点下撰写每篇文章的用户。现在,您可能需要获取所有用户帖子的列表。您需要做的是在 users-posts/USER_KEY/
节点上同步以获取用户已写的所有帖子的 key ,然后获取更多发布信息使用您刚刚获得的发布 key 。
为什么推荐这种数据库设计?因为您每次同步获得的信息少得多(对于 Firebase,我们本身不发出请求,因此我将读取称为同步)。在您的示例中,如果您将监听器附加到 user-posts/USER_KEY/
以获取所有帖子的列表,您还将要求ALL 的信息>每一个他们写的帖子。使用数据扇出方法,您可以只询问您需要的帖子信息,因为您已经拥有帖子的 key 。
关于java - Firebase 数据库 - "Fan Out"技术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38181973/
比如说,我们有三种方法来实现“扇入”行为 func MakeChannel(tries int) chan int { ch := make(chan int) go func() {
我已经安装了 NDepend(14 天试用版)作为 Visual Studio 2015 扩展,现在可以使用了。 我想在我的解决方案中获得某些类的一些指标: 标识符的长度 扇入/扇出 类的加权方法 类
我计划将 Elasticsearch 用于一种社交网络平台,用户可以在其中发布“更新”,与其他用户成为 friend 并关注他们 friend 的提要。基本且可能是最常见的查询将是 “获取我关注的 f
进程文件: fan or fan.exe 进程名称: Fan 进程类别:存在安全风险的进程 英文描述: fan.exe is a process from Dell. It allows t
Argo 允许基于先前步骤的输出动态生成并行工作流步骤。 此处提供了此动态工作流生成的示例:https://github.com/argoproj/argo-workflows/blob/master
一直在尝试解决这个问题,但被难住了! 我想用 Jquery/CSS 创建一个动画,其中一堆图像旋转并创建一个圆弧。我认为最简单的方法是堆叠它们,然后从中心旋转它们,如下图所示。 我希望所有四张图片(只
我需要用户能够成为其他用户的粉丝。我应该如何设计/设置它? 我需要能够查看用户粉丝的详细信息。 例如。我有用户:Foo。 Foo 有 3 个粉丝。我希望能够找到 Foo 粉丝的名字。因此: foo =
我正在使用 facebook 的 javascript sdk 构建扇门。我想确定用户是否“喜欢”我的页面,以便我可以显示/Conceal 内容。目前,如果我使用 javascript sdk,系统会
我正在使用 Cesium 并希望在视觉上表示相同的两个实体之间的多条折线。例如,从实体 A 到实体 B 的绿色多段线,以及从实体 A 到实体 B 的蓝色多段线。我希望它们不要重叠或混合,所以我想象随着
我使用 js sdk 毫无问题地获取我的 friend 列表。我得到的列表来 self 的 facebook 个人页面 (Tony Easterbird) 我还有一个扇形页面,叫做 Katahdin
我正在调查 Firebase 数据库 sample对于安卓并意识到它以下列方式存储其数据: 我不太熟悉 NoSQL 技术并试图理解为什么我们必须坚持每个 post实体两次 - 在 posts和 use
我们是 Azure 消息服务和功能的新手,我们了解了不同消息服务的基本概念,例如存储队列、服务总线队列、服务总线主题和事件中心,我们还阅读了 fan-out pattern由微软开发,但其工作方式更像
我必须对项目列表运行一个函数。我正在使用 Azure Durable Functions,并且可以使用它们的 fan out/fan in 并行运行这些项目。策略。 但是,我想知道这样做与在单个 Ac
我们是 Azure 消息服务和功能的新手,我们了解了不同消息服务的基本概念,例如存储队列、服务总线队列、服务总线主题和事件中心,我们还阅读了 fan-out pattern由微软开发,但其工作方式更像
我必须对项目列表运行一个函数。我正在使用 Azure Durable Functions,并且可以使用它们的 fan out/fan in 并行运行这些项目。策略。 但是,我想知道这样做与在单个 Ac
我试图实现一个示例 Go 代码,用于使用从 go 例程返回的 channel ,而 main 函数中没有任何“读取 block ”。在这里,一个 fanIn 函数接受来自其他两个例程的 channel
嗨,我正在为我的新树莓派 4 戴上 POE-FAN 帽子而苦苦挣扎。 使用官方 POE-HAT 安装了几个 Pi:https://www.raspberrypi.org/products/poe-ha
我正在使用顶点数组制作一些相对简单的形状,并且取得了一些不错的进展,但现在我想绘制 2 个(或更多)三角形扇形对象。有没有办法只打一个电话到gl.glDrawArrays(GL.GL_TRIANGLE
我想获得喜欢某个页面或它的粉丝的用户列表。 FB API 文档指出,您只能使用社交图谱获取特定页面的粉丝数,而不能获取粉丝列表。 此处讨论Retrieve Facebook Fan Names建议可以
我的问题是:如何使用 D3DPT_TRIANGLEFAN 绘制正确的金字塔(三角形四边形金字塔)? 我用作积分: CUSTOMVERTEX vertices[] = { { 0.0f, 3.
我是一名优秀的程序员,十分优秀!