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

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

问题描述

我试图找出NoSQL KVP或Document数据库中的外键和索引的等效项。由于没有枢轴表格(用于添加标记两个对象之间关系的键),我真的很困惑如何以对普通网页有用的方式检索数据。

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.

但是,以第二个示例为例,您很快就会碰壁当您使用它来跟踪其他内容(例如称为 active_comments的键)时,该键中可能包含3000万个ID,从而花费TON 来查询每个页面只是为了了解一些最近的活动注释。 竞赛条件也很容易出现,因为许多页面可能会尝试同时更新它。

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数据库中跟踪如下关系吗?


  • 所有用户评论

  • 所有活动评论

  • 所有带有[关键字]标签的帖子

  • 俱乐部中的所有学生-或学生所在的所有俱乐部

  • All of a user's comments
  • All active comments
  • All posts tagged with [keyword]
  • All students in a club - or all clubs a student is in

还是我不正确地考虑了这个问题?

推荐答案

所有有关如何以 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天全站免登陆