关系数据库不适合 Node.js 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18376042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:22:48  来源:igfitidea点击:

Are relational databases a poor fit for Node.js?

node.jsrelational-database

提问by Matthew Daly

Recently I've been playing around with Node.js a little bit. In my particular case I wound up using MongoDB, partly because it made sense for that project because it was very simple, and partly because Mongoose seemed to be an extremely simple way to get started with it.

最近我一直在玩 Node.js。在我的特殊情况下,我最终使用了 MongoDB,部分原因是它对那个项目有意义,因为它非常简单,部分原因是 Mongoose 似乎是一种非常简单的入门方式。

I've noticed that there seems to be a degree of antipathy towards relational databases when using Node.js. They seem to be poorly supported compared to non-relational databases within the Node.js ecosystem, but I can't seem to find a concise reason for this.

我注意到在使用 Node.js 时似乎对关系数据库有一定程度的反感。与 Node.js 生态系统中的非关系数据库相比,它们似乎没有得到很好的支持,但我似乎找不到一个简洁的原因。

So, my question is, is there a solid technical reason why relational databases are a poorer fit for working with Node.js than alternatives such as MongoDB?

所以,我的问题是,为什么关系数据库比 MongoDB 等替代方案更不适合使用 Node.js是否有可靠的技术原因?

EDIT: Just want to clarify a few things:

编辑:只是想澄清一些事情:

  • I'm specifically not looking for details relating to a specific application I'm building
  • Nor am I looking for non-technical reasons (for example, I'm not after answers like "Node and MongoDB are both new so developers use them together")
  • 我不是在寻找与我正在构建的特定应用程序相关的详细信息
  • 我也不是在寻找非技术原因(例如,我不是在寻找诸如“Node 和 MongoDB 都是新的,因此开发人员将它们一起使用”之类的答案)

What I am looking for is entirely technical reasons, ONLY. For instance, if there were a technical reason why relational databases performed unusually poorly when used with Node.js, then that would be the kind of thing I'm looking for (note that from the answers so far it doesn't appear that is the case)

我正在寻找的完全是技术原因。例如,如果在与 Node.js 一起使用时关系数据库表现异常糟糕是有技术原因的,那么这就是我正在寻找的东西(请注意,从目前的答案来看,似乎并不是案子)

采纳答案by Jonathan Lonowski

No, there isn't a technical reason. It's mostly just opinion and using NoSQL with Node.js is currently a popular choice.

不,没有技术原因。这主要只是意见,将 NoSQL 与 Node.js 结合使用是目前流行的选择。

Granted, Node's ecosystem is largely community-driven. Everything beyond Node's core APIrequires community involvement. And, certainly, people will be more likely to support what aligns with their personal preferences.

诚然,Node 的生态系统很大程度上是由社区驱动的。Node核心 API之外的一切都需要社区参与。而且,当然,人们将更有可能支持符合他们个人喜好的内容。

But, many still use and support relational databases with Node.js. Some notable projects include:

但是,许多人仍然使用和支持 Node.js 的关系数据库。一些值得注意的项目包括:

回答by Alexander Mills

I love Node.js, but with Node it actually makes more senseto use a RDBMs, as opposed to a non-relational DB. With a noSQL/non-relational solution you often need to do manual joins in your Node.js code and sometimes work with a lack of transactions, a technical feature of RDBMs that have commit/rollback features. Here are some potential problems with using Non-Relational DBs + Node.js servers:

我喜欢 Node.js,但是对于 Node,实际上使用 RDBM 比使用非关系数据库更有意义。使用 noSQL/非关系解决方案,您通常需要在 Node.js 代码中进行手动连接,有时还需要处理缺少事务的情况,这是具有提交/回滚功能的 RDBM 的技术特性。以下是使用非关系数据库 + Node.js 服务器的一些潜在问题:

(a) the joins are slower and responses are slower, because Node is not C/C++

(b) the expensive joins block your event loop, because the join is happening in your Node.js code not on some database server

(c) manually writing joins is often difficult and error-prone; your noSQL queries could easily be incorrect or your join code might be incorrect or suboptimal; optimized joins have been done before by the masters of RDBMs, and joins in RDBMs are proven to be correct, mathematically in most cases.

(d) Some non-relational databases, like MongoDB, do notsupport transactions - in my team's case, that means we have to use an external distributed lock so that multiple queries can be grouped together into an atomic transaction. It would be somewhat easier if we could just use transactions and avoid application level locks.

(a) 连接较慢,响应较慢,因为 Node 不是 C/C++

(b) 昂贵的连接会阻塞您的事件循环,因为连接发生在您的 Node.js 代码中,而不是在某个数据库服务器上

(c) 手动编写连接通常很困难且容易出错;您的 noSQL 查询很容易出错,或者您的连接代码可能不正确或不理想;优化连接之前已经由 RDBM 的大师完成,并且在大多数情况下,RDBM 中的连接在数学上被证明是正确的。

(d) 一些非关系型数据库,如 MongoDB,支持事务——就我的团队而言,这意味着我们必须使用外部分布式锁,以便可以将多个查询组合到一个原子事务中。如果我们可以只使用事务并避免应用程序级锁,那会容易一些。

with a more powerful relational database system that can do optimized joins in C/C++ on the database server rather than in your Node.js code, you let your Node.js server do what it's best at.

有了更强大的关系数据库系统,它可以在数据库服务器上而不是在您的 Node.js 代码中以 C/C++ 进行优化连接,您可以让 Node.js 服务器做它最擅长的事情。

With that being said, I think it's pretty f*ing stupid that many major noSQL vendors don't support joins (?) Complete de-normalization is only a dream as far as I can see it. And the lack of transactions can be a bit weird. Without transactions, only one query is atomic, you cannot make multiple queries atomic without an application level locking mechanism :/

话虽如此,我认为许多主要的 noSQL 供应商不支持联接 (?) 就我所见,完全反规范化只是一个梦想,这真是太愚蠢了。缺乏交易可能有点奇怪。没有事务,只有一个查询是原子的,如果没有应用程序级别的锁定机制,就不能使多个查询原子化:/

Take-aways:

外卖:

If you want non-relational persistence - why not simply de-normalize a relational database? There is nobody forcing you to use a traditional database in a relational manner.

如果您想要非关系持久性 - 为什么不简单地对关系数据库进行反规范化?没有人强迫您以关系方式使用传统数据库。

If you use a relational DB with Node.js I recommend this ORM: https://github.com/typeorm/typeorm

如果你使用 Node.js 的关系数据库,我推荐这个 ORM:https: //github.com/typeorm/typeorm

As an aside, I prefer the term "non-relational" as opposed to "noSQL".

顺便说一句,我更喜欢术语“非关系”而不是“noSQL”。

回答by Alex Savenok

In my experience node tends to be popular with databases that have a stateless API, this fits very nicely into nodes async nature. Most relational databases utilize stateful connections for transactions, this minimizes the primary advantages of async non-block i/o.

根据我的经验,节点往往受具有无状态 API 的数据库的欢迎,这非常适合节点异步性质。大多数关系数据库为事务使用有状态连接,这最大限度地减少了异步非块 i/o 的主要优势。

回答by Munim

Can you explain exactly what specific problems you are facing with your chosen database and node.js?

您能准确解释一下您选择的数据库和 node.js 面临哪些具体问题吗?

A few reasons why MongoDB could be more popular than relational databases:

MongoDB 比关系型数据库更受欢迎的几个原因:

  • MongoDB is essentially a JSON object store, so it translates very well for a javascript application. MongoDB functions are javascript functions.

  • I am just guessing here, but since NoSQL databases are newer and have more enthusiastic programmers experimenting with it, you probably have more involvement in those NPM modules.

  • MongoDB 本质上是一个 JSON 对象存储,因此它非常适合 javascript 应用程序。MongoDB 函数是 javascript 函数。

  • 我只是在这里猜测,但是由于 NoSQL 数据库更新并且有更多热情的程序员在试验它,因此您可能更多地参与这些 NPM 模块。

Apart from this, Node.js technically is a perfect choice for any sort of database application. I have personally worked on a small Node.js/MySQL application and I didn't face any hurdles.

除此之外,从技术上讲,Node.js 是任何类型的数据库应用程序的完美选择。我亲自开发了一个小型 Node.js/MySQL 应用程序,我没有遇到任何障碍。

But back to my main point, we could talk about this all day, and that is not what this forum is for. If you have any specific issues in any code with Node.js and your database of choice, please ask those questions instead.

但是回到我的主要观点,我们可以整天谈论这个,这不是这个论坛的目的。如果您在使用 Node.js 和您选择的数据库的任何代码中遇到任何特定问题,请改为提出这些问题。

Edit: Strictly technical reasons, apart from the JSON compatibility on both sides: There are none.

编辑:严格的技术原因,除了双方的 JSON 兼容性:没有。