Firebase(NoSQL):非规范化与索引 [英] Firebase (NoSQL): Denormalization vs Indexing

查看:163
本文介绍了Firebase(NoSQL):非规范化与索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想写一个博客应用程序。我应该更喜欢以下两种选择之一吗?我宁愿拥有尽可能多的单一真相源,但我仍然不确定这种偏好是否来自我在SQL中的背景。



选项1(反规范化):

 文章:{
post_1:{
title: hello,
body:hi there!,
uid:user_1,
评论:{
comment_1:{
body:hi I am comment ,
uid:user_2,
},
comment_2:{
body:bye I commented,
uid:user_2,




$ b用户:{
user_1:{
uid:user_1,
post_1:{
title:hello,
body:hi there!,
uid:user_1,
评论:{
comment_1:{
body:hi I commented,
uid:user_2,
},
comment_2:{
body:bye I commented,
uid:user_2,
},
}
}
}
}

选项2(索引):

$ p $ 文章:{
post_1:{
title:hello ,
body:hi there!,
uid:user_1,
authorName:Richard,
评论:{
comment_1:true,
comment_2:true
}
}
}

用户:{
user_1:{
uid:user_1,
displayName:Richard,
email:richard@gmail.com,
文章:{
post_1:true
},
评论:{
comment_1:true,
comment_2:true
}
}
}

评论:{
comment_1:{
body:hi我评论过,
uid:user_1,
},
comment_2:{
body:bye I commented,
uid: user_1,
},
}

我想我应该选择2。

我在选项1中看到的主要问题是:一个数据来源太多。比方说,我想扩展应用程序,使每个帖子属于某个类别或标签。然后,我将不得不在 / categories / category_id 下面写 post / posts / users / uid 。当帖子更新后,我必须记得在三个不同的地方修改帖子对象。如果我去选择2,我没有这个问题,因为只有一个来源的数据。



我缺少什么?



参考文献:


  1. Firebase数据结构和网址
  2. https://firebase.google.com/docs/database/web/structure-data


解决方案

第二个选项是更好的,否则你会迫使用户下载所有的评论和帖子(可以很多)。 >

您可以查看文档这里



您可以处理重复操作在多个位置进行原子写入。


Assume that I want to write a blogging app. Should I prefer one of the following two options? I would prefer to have as much "single source of truth" as possible, but I am still not sure whether that preference comes from my background in SQL.

Option 1 (Denormalization):

Posts: {
  post_1: {
    title: "hello",
    body: "hi there!",
    uid: "user_1",
    comments: {
      comment_1: {
        body: "hi I commented",
        uid: "user_2",
      },
      comment_2: {
        body: "bye I commented",
        uid: "user_2",
      },
    }
  }
}

Users: {
  user_1: {
    uid: "user_1",
    post_1: {
      title: "hello",
      body: "hi there!",
      uid: "user_1",
      comments: {
        comment_1: {
          body: "hi I commented",
          uid: "user_2",
        },
        comment_2: {
          body: "bye I commented",
          uid: "user_2",
        },
      }
    }
  }
}

Option 2 (Indexing):

Posts: {
  post_1: {
    title: "hello",
    body: "hi there!",
    uid: "user_1",
    authorName: "Richard",
    comments: {
      comment_1: true,
      comment_2: true
    }
  }
}

Users: {
  user_1: {
    uid: "user_1",
    displayName: "Richard",
    email: "richard@gmail.com",
    posts: {
      post_1: true
    },
    comments: {
      comment_1: true,
      comment_2: true
    }
  }
}

Comments: {
  comment_1: {
    body: "hi I commented",
    uid: "user_1",
  },
  comment_2: {
    body: "bye I commented",
    uid: "user_1",
  },
}

I think I should prefer option 2.

The main problem that I see with option 1 is that there are too many sources for one data. Let's say I want to extend the app so each post belongs to a certain category or tag. Then, I will have to write a post object under /categories/category_id in addition to /posts and /users/uid. When the post gets updated, I have to remember to modify the post object in three different places. If I go with option 2, I don't have this problem because there's only one source for data.

Am I missing anything?

References:

  1. Firebase data structure and url
  2. https://firebase.google.com/docs/database/web/structure-data

解决方案

The second option is better because otherwise you would be forcing the user to download all the comments and posts (that can be a lot).

You can check in the documentation here.

And you can handle the duplication doing atomic writes across multiple locations.

这篇关于Firebase(NoSQL):非规范化与索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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