如何对firebase实时数据库的数据结构进行非规范化/规范化?

时间:2021-12-25 04:16:24

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:

我试图围绕如何构建firebase实时数据库的数据。我在以下建议中阅读了有关SO发现的文档和其他一些问题:

  • data should be as flat as possible
  • 数据应尽可能平坦
  • be expensive on writes to have cheap reads
  • 在写入便宜的读取是昂贵的
  • avoid nesting data
  • 避免嵌套数据
  • duplication of data may be okay
  • 重复数据可能没问题

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

牢记这一点让我描述我的具体用例。首先,用户具有以下属性:

  • Firstname
  • 名字
  • Lastname
  • Profile picture (big)
  • 个人资料图片(大)
  • Profile picture (small)
  • 个人资料图片(小)

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

用户可以创建包含以下属性的故事:

  • User
  • 用户
  • Text
  • 文本
  • Timestamp
  • 时间戳

The visual representation of a story may look like this:

故事的视觉表现可能如下所示:

如何对firebase实时数据库的数据结构进行非规范化/规范化?

My question is, how would you associate user information (firstname, lastname, small profile picture) with a story?

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

What I thought about:

我的想法:

  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放入包含特定用户的外部ID的故事中。要加载故事,我们必须向数据库发出两个请求,一个用于获取故事,另一个用于用户。

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

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

  2. 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', firstname: 'sandra', lastname: 'adams', smallProfilePicutre: '...', text: 'foobar', timestamp: ... }

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

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.

因此,当创建的故事很少并且大部分时间只有读取时,方法1.将是昂贵的,因为我们支付两次读取来显示故事。方法2.将更具成本效益。

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

我想在此就你的想法和想法。

1 个解决方案

#1


10  

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.

我和杰伊在这里:你已经在你的问题中得到了所有这些。使用Firebase数据库时我们建议的做法的完整摘要。

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

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

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.

我看到的大多数开发人员都会将配置文件信息分开,只是将用户UID作为非托管外键保留在帖子中。这具有以下优点:需要在其改变时仅在一个地方更新用户简档。阅读单个故事的性能也不算太差:两个读取速度相对较快,因为它们通过相同的连接。当您显示故事列表时,由于Firebase通过其单一连接对请求进行管道传输,因此速度出乎意料地快。

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:

最后,这一切都取决于您的用例。您需要回答以下问题:

  • Do you need the historical information for each user?
  • 您需要每个用户的历史信息吗?
  • Is it crucial that you show the up-to-date information for a user in older posts?
  • 在较旧的帖子中显示用户的最新信息是否至关重要?
  • Can you come up with a good caching strategy for the user profiles in your client-side code?
  • 你能为客户端代码中的用户配置文件提出一个好的缓存策略吗?

#1


10  

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.

我和杰伊在这里:你已经在你的问题中得到了所有这些。使用Firebase数据库时我们建议的做法的完整摘要。

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

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

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.

我看到的大多数开发人员都会将配置文件信息分开,只是将用户UID作为非托管外键保留在帖子中。这具有以下优点:需要在其改变时仅在一个地方更新用户简档。阅读单个故事的性能也不算太差:两个读取速度相对较快,因为它们通过相同的连接。当您显示故事列表时,由于Firebase通过其单一连接对请求进行管道传输,因此速度出乎意料地快。

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:

最后,这一切都取决于您的用例。您需要回答以下问题:

  • Do you need the historical information for each user?
  • 您需要每个用户的历史信息吗?
  • Is it crucial that you show the up-to-date information for a user in older posts?
  • 在较旧的帖子中显示用户的最新信息是否至关重要?
  • Can you come up with a good caching strategy for the user profiles in your client-side code?
  • 你能为客户端代码中的用户配置文件提出一个好的缓存策略吗?