你如何在 NoSQL 中跟踪记录关系? [英] How do you track record relations in NoSQL?

查看:8
本文介绍了你如何在 NoSQL 中跟踪记录关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出 NoSQL KVP 或文档数据库中外键和索引的等价物.由于没有关键表(用于添加标记两个对象之间关系的键),我真的很难理解如何以对普通网页有用的方式检索数据.

I am trying to figure out the equivalent of foreign keys and indexes in NoSQL KVP or Document databases. Since there are no pivotal tables (to add keys marking a relation between two objects) I am really stumped as to how you would be able to retrieve data in a way that would be useful for normal web pages.

假设我有一个用户,这个用户在整个网站上留下了很多评论.我能想到的跟踪用户评论的唯一方法是

Say I have a user, and this user leaves many comments all over the site. The only way I can think of to keep track of that users comments is to

  1. 将它们嵌入到用户对象中(这似乎没什么用)
  2. 创建并维护一个 user_id:comments 值,其中包含每个评论的键 [comment:34、comment:197 等...] 的列表,以便我可以根据需要获取它们.
  1. Embed them in the user object (which seems quite useless)
  2. Create and maintain a user_id:comments value that contains a list of each comment's key [comment:34, comment:197, etc...] so that that I can fetch them as needed.

但是,以第二个示例为例,当您使用它来跟踪其他内容时,您很快就会碰壁> 查询每个页面只是为了了解一些最近的活跃评论.它也很容易出现竞争条件,因为许多页面可能会同时尝试更新它.

However, taking the second example you will soon hit a brick wall when you use it for tracking other things like a key called "active_comments" which might contain 30 million ids in it making it cost a TON to query each page just to know some recent active comments. It also would be very prone to race-conditions as many pages might try to update it at the same time.

如何在 NoSQL 数据库中跟踪如下关系?

  • 用户的所有评论
  • 所有活跃评论
  • 所有带有 [keyword] 标记的帖子
  • 俱乐部中的所有学生 - 或学生所在的所有俱乐部

还是我想错了?

推荐答案

关于如何以NoSQL 方式"存储多对多关联的所有答案都归结为同一件事:冗余存储数据.

All the answers for how to store many-to-many associations in the "NoSQL way" reduce to the same thing: storing data redundantly.

在 NoSQL 中,您不会根据数据实体之间的关系来设计数据库.您根据将针对它运行的查询来设计数据库.使用与非规范化关系数据库相同的标准:如果数据具有凝聚力更重要(考虑以逗号分隔的列表而不是规范化表中的值),那么就这样做.

In NoSQL, you don't design your database based on the relationships between data entities. You design your database based on the queries you will run against it. Use the same criteria you would use to denormalize a relational database: if it's more important for data to have cohesion (think of values in a comma-separated list instead of a normalized table), then do it that way.

但这不可避免地会优化一种类型的查询(例如任何用户对给定文章的评论),而牺牲其他类型的查询(给定用户对任何文章的评论).如果您的应用程序需要对两种类型的查询进行同等优化,则不应进行非规范化.同样,如果您需要以关系方式使用数据,则不应使用 NoSQL 解决方案.

But this inevitably optimizes for one type of query (e.g. comments by any user for a given article) at the expense of other types of queries (comments for any article by a given user). If your application has the need for both types of queries to be equally optimized, you should not denormalize. And likewise, you should not use a NoSQL solution if you need to use the data in a relational way.

非规范化和冗余存在冗余数据集彼此不同步的风险.这称为异常.当您使用规范化的关系数据库时,RDBMS 可以防止异常.在非规范化数据库或 NoSQL 中,编写应用程序代码以防止异常成为您的责任.

There is a risk with denormalization and redundancy that redundant sets of data will get out of sync with one another. This is called an anomaly. When you use a normalized relational database, the RDBMS can prevent anomalies. In a denormalized database or in NoSQL, it becomes your responsibility to write application code to prevent anomalies.

有人可能会认为,让 NoSQL 数据库为您完成防止异常的艰苦工作会很棒.有一种范式可以做到这一点——关系范式.

One might think that it'd be great for a NoSQL database to do the hard work of preventing anomalies for you. There is a paradigm that can do this -- the relational paradigm.

这篇关于你如何在 NoSQL 中跟踪记录关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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