如何对Firebase实时数据库的数据结构进行归一化/归一化? [英] How to denormalize/normalize data structure for firebase realtime database?

查看:97
本文介绍了如何对Firebase实时数据库的数据结构进行归一化/归一化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决如何为Firebase实时数据库构造数据的问题.我阅读了文档以及关于SO的其他一些问题,以寻求以下建议:

I am trying to wrap my head around how to structure my data for firebase realtime database. I read the docs and some other questions on SO finding following advices:

  • 数据应尽可能平坦
  • 写入昂贵而读取便宜
  • 避免嵌套数据
  • 重复数据可能没事

牢记这一点,让我描述我的特定用例.第一,有用户,具有以下属性:

Keeping this in mind let me describe my specific use case. Frist there is user with following attributes:

  • 名字
  • 姓氏
  • 头像(大)
  • 头像(小)

用户可以创建一个故事,该故事包含以下属性:

A user may create a story, that consist of following attributes:

  • 用户
  • 文字
  • 时间戳

故事的视觉呈现可能是这样的:

The visual representation of a story may look like this:

我的问题是,您如何将用户信息(名字,姓氏,小图片)与故事相关联?

我的想法:

  1. 在包含外部ID的故事中输入一个user_id给特定用户.要加载故事,我们必须向数据库发出两个请求,一个请求获取故事,另一个请求给用户.

  1. put a user_id in the story that contains the foreign id to the specific user. To load the story we would have to make two request to the database, one to get the story and one for the user.

{ user_id:"XYZ", 文字:"foobar", 时间戳:... }

{ user_id : 'XYZ', text: 'foobar', timestamp: ... }

在故事中输入名字,姓氏和小图片.只需一个请求即可显示该故事.但是,例如,当我们需要更新每个用户的故事时,个人资料图片发生变化.

put firstname, lastname and small profile picture in the story. Only one request would be necessary to display the story. But we would have to update each user's story, when e.g. the profile picture changes.

{ user_id:"XYZ", 名:"sandra", 姓氏:"adams", smallProfilePicutre:'...', 文字:"foobar", 时间戳:... }

{ user_id : 'XYZ', firstname: 'sandra', lastname: 'adams', smallProfilePicutre: '...', text: 'foobar', timestamp: ... }

因此,当创建的故事很少,而在大多数情况下只有阅读时,方法1会很昂贵,因为我们需要两次阅读才能显示一个故事.方法2.更具成本效益.

So when there are few stories created and most of the time there are just reads, approach 1. would be expensive, because we pay for two reads to display a story. Approach 2. would be more cost efficient.

我想在这里发表您的想法和想法.

I would like to here your thoughts and ideas on this.

推荐答案

我在这里与Jay在一起:您的问题中已经包含了所有这些内容.我们建议您在使用Firebase数据库时推荐的做法的摘要.

I'm with Jay here: you pretty much got all of it in your question already. Great summary of the practices we recommend when using Firebase Database.

您的问题可以归结为:我应该将我的用户个人资料信息复制到每个故事中吗?不幸的是,没有唯一的答案.

Your questions boils down to: should I duplicate my user profile information into each story? Unfortunately there's no single answer for that.

我看到的大多数开发人员都会将个人资料信息分开,只是将用户UID作为非托管外键保留在帖子中.这样做的好处是,当用户配置文件更改时,仅需要在一个位置更新即可.读取单个故事的性能还不错:两次读取相对较快,因为它们经过相同的连接.当您显示故事列表时,由于

Most developers I see will keep the profile information separate and just keep the user UID in the post as a unmanaged foreign key. This has the advantage of needing to update the user profile in only one place when it changes. The performance to read a single story is not too bad: the two reads are relatively fast, since they go over the same connection. When you're showing a list of stories, it is unexpectedly fast since Firebase pipelines the requests over its single connection.

但是我帮助实现的第一个更大的实现之一实际上是在故事中复制用户数据.正如您所说:在这种情况下,阅读故事或故事列表的速度是最快的.当被问及如何处理故事中的用户信息时(请参见策略这里),他们承认没有.实际上:他们争辩说他们为什么需要每个故事的历史用户信息有很多充分的理由.

But one of the first bigger implementation I helped with actually duplicated the user data over the stories. As you said: reading a story or list of stories is as fast as it can be in that case. When asked how they dealt with keeping the user information up to date in the stories (see strategies here), they admitted they didn't. In fact: they argued many good reasons why they needed the historical user information for each story.

最后,这完全取决于您的用例.您需要回答以下问题:

In the end, it all depends on your use-case. You'll need to answer questions such as:

  • 您需要每个用户的历史信息吗?
  • 在较早的帖子中显示用户的最新信息是否至关重要?
  • 您能为客户端代码中的用户配置文件提出一个好的缓存策略吗?

这篇关于如何对Firebase实时数据库的数据结构进行归一化/归一化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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