简体   繁体   English

Meteor.js-是否应该对数据进行非规范化?

[英]Meteor.js - Should you denormalize data?

This question has been driving me crazy and I can't get my head around it. 这个问题一直让我发疯,我无法解决。 I come from a MySQL relational background and have been using Meteorjs and Mongo. 我来自MySQL关系背景,一直在使用Meteorjs和Mongo。 For the purposes of this question take the example of posts and authors. 为了这个问题的目的,以帖子和作者为例。 One Author to Many Posts. 多篇文章的一位作者。 I have come up with two ways in which to do this: 我想出了两种方法来做到这一点:

  1. Have a single collection of posts - Each post has the author information embedded into the document. 具有单个帖子集-每个帖子都将作者信息嵌入文档中。 This of course leads to denormalization and issues such as if the author name changes how do you keep the data correct. 当然,这会导致非规范化和问题,例如作者姓名是否更改,如何保持数据正确。

  2. Have two collections: posts and authors - Each post has an author ID which references the authors collection. 有两个集合:帖子和作者-每个帖子都有一个引用该作者集合的作者ID。 I then attempt to do a "join" on a non relational database while trying to maintain reactivity. 然后,我尝试在非关系数据库上进行“联接”,同时尝试保持反应性。

It seems to me with MongoDB degrees of denormalization is acceptable and I am tempted to embed as implementing joins really does feel like going against the ideals of Mongo. 在我看来,MongoDB的非规范化程度是可以接受的,并且由于实施联接确实确实违反了Mongo的理想,因此我很容易嵌入。

Can anyone shed any light on what is the right approach especially in terms of wanting my app data to scale well and be manageable? 谁能说出什么是正确的方法,尤其是在希望我的应用程序数据能够很好地扩展和可管理的方面?

Thanks 谢谢

Denormalisation is useful when you're scaling your application and you notice that some queries are taking too much time to complete. 当您扩展应用程序并且注意到某些查询花费太多时间才能完成时,非规范化很有用。 I also noticed that most Mongodb developers tend to forget about data normalisation but that's another topic. 我还注意到,大多数Mongodb开发人员倾向于忘记数据标准化,但这是另一个主题。

Some developers say things like: "Don't use observe and observeChanges because it's slow". 一些开发人员说这样的事情:“不要使用observe和observeChanges,因为它很慢”。 We're building real-time applications so that a normal thing to happen, it's a CPU intensive app design. 我们正在构建实时应用程序,以便正常发生,这是CPU密集型应用程序设计。

In my opinion, you should always aim for a normalised database design and then you have to decide, try and test which fields, that duplicated/denormalised, could improve your app's performance. 我认为,您应该始终以规范化的数据库设计为目标,然后必须决定,尝试和测试重复/非规范化的字段可以提高应用程序的性能。 Example: You remove 1 query per user. 示例:您删除每个用户1个查询。 The UI need an extra field and it's fast to duplicated it, etc. UI需要一个额外的字段,可以快速复制它,等等。

With the denormalisation you've an extra price to pay. 通过非规范化,您需要支付额外的费用。 You've to update the denormalised fields according to the main collection. 您必须根据主集合更新非规范化字段。

Example: Let's say that you Authors and Articles collections. 示例:假设您是作者和文章集合。 On each article you have the author name. 在每篇文章上,您都有作者姓名。 The author might change his name. 作者可能会更改他的名字。 With a normalised scenario, it works fine. 使用标准化方案,它可以正常工作。 With a denormalised scenario you have to update the Author document name AND every single article, owned by this author, with the new name. 在非规范化的情况下,您必须使用新名称更新作者文档名称以及该作者拥有的每篇文章。

Keeping a normalised design makes you life easier but denormalisation, eventually, becomes necessary. 保持规范化的设计会使您的生活更轻松,但是最终需要非规范化。

From a MeteorJs perspective: With the normalised scenario you're sending data from 2 Collections to the client. 从MeteorJs的角度来看:使用规范化的方案,您正在将数据从2个Collections发送到客户端。 With the denormalised scenario, you only send 1 collection. 在非规范化方案中,您仅发送1个集合。 You can also reactively join on the server and send 1 collection to the client, although it increases the RAM usage because of MergeBox on the server. 您还可以在服务器上进行反应性连接并向客户端发送1个集合,尽管由于服务器上的MergeBox会增加RAM使用率。

Denormalisation is something that it's very specify for you application needs. 非规范化非常适合您的应用程序需求。 You can use Kadira to find ways of making your application faster. 您可以使用Kadira找到使您的应用程序更快的方法。 The database design is only 1 factor out of many that you play with when trying to improve performance. 在尝试提高性能时,数据库设计只是众多因素中的1个因素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM