Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase(NoSQL):非规范化与索引_Firebase_Firebase Realtime Database_Nosql - Fatal编程技术网

Firebase(NoSQL):非规范化与索引

Firebase(NoSQL):非规范化与索引,firebase,firebase-realtime-database,nosql,Firebase,Firebase Realtime Database,Nosql,假设我想写一个博客应用程序。我应该选择以下两个选项之一吗?我更希望有尽可能多的“单一真相来源”,但我仍然不确定这种偏好是否来自我的SQL背景 选项1(非规范化): Posts: { post_1: { title: "hello", body: "hi there!", uid: "user_1", comments: { comment_1: { body: "hi I commented", uid: "user

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

选项1(非规范化):

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",
        },
      }
    }
  }
}
备选案文2(索引编制):

我想我更喜欢选择2

我在选项1中看到的主要问题是一个数据源太多。假设我想扩展应用程序,使每个帖子都属于某个类别或标签。然后,除了
/posts
/users/uid
之外,我还必须在
/categories/category\u id
下编写一个
post
对象。更新帖子时,我必须记住在三个不同的位置修改
post
对象。如果我选择选项2,我不会有这个问题,因为只有一个数据源

我遗漏了什么吗

参考资料:


  • 如果您喜欢第二个选项,那么为什么要使用NoSQL呢?只要使用SQL…我想尝试firebase作为我的后端。此外,我还想了解在NoSQL中,我是否更喜欢选项1而不是选项2。这个()说选项2更好,但很多帖子也建议使用选项1。好吧,免责声明方面,我没有使用NoSQL作为主要后端的实际经验。我可能很守旧,但如果可能的话,我喜欢标准化数据。为了获得可观的性能或简单性收益,可以改变规则并进行一些重复,但在大多数情况下,我认为对于复杂的关系数据,最好的选择是关系数据库(可能使用NoSQL作为缓存层)。我知道我没有回答你的问题,但这就是为什么我写评论而不是回答:)最好的解决方案是适用于所有用例的解决方案。因为我们不可能知道您的所有用例(很可能您也不知道),所以没有人有机会推荐最佳解决方案。一般来说,NoSQL数据库中的重复数据比SQL数据库中的重复数据更为常见(在SQL数据库中也不例外)。为了更好地理解这个话题,我强烈推荐阅读这篇文章。我也推荐阅读我的。这篇文章和我的回答都不是很害怕重复数据。当您来自最常见的下意识反应之一的SQL背景时,您可以/必须放手:数据复制在NoSQL中是正常的,通常需要实现NoSQL解决方案所需的可伸缩性。
    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",
      },
    }