服务器端JavaScript函数在mongoDB中有性能问题吗? [英] Does server-side javascript function have performance issues in mongoDB?

查看:142
本文介绍了服务器端JavaScript函数在mongoDB中有性能问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行服务器端JavaScript是否在MongoDB中存在性能问题? V8是否解决了所有的性能问题?



为什么这个 MongoDB文档建议不要使用服务器端函数? 解决方案

当你问服务器端JavaScript的可行性时,你首先必须弄清楚你正在谈论什么样的服务器端JavaScript。根据文档,有四种不同类型的服务器端代码执行。不幸的是,它们对于本地API不足的情况都是一种骇人听闻的解决方法:

eval 命令



eval的缺点是只能在一个节点上运行。这大大降低了它在集群环境中的实用性。使用分片集合,它根本不起作用!

默认情况下,它还会创建一个全局锁,它使数据库完全无法使用,直到脚本运行。这可以通过 nolock 参数来防止(除非脚本本身做了一些创建全局锁的东西)。



Sammaye的回答也解释了一些严重的安全问题。



这实际上更像是一种测试和管理工具,而不是您应该用于任何常规操作的东西。



通过mongo shell运行.js文件服务器上的实例



在这种情况下,代码不会在数据库,而是在其中一台服务器上的另一个独立进程。这意味着所有来自其他分片的必需数据都必须传输到运行javascript代码的服务器,无论脚本实际返回的是什么。



它看起来像另一个应用程序到MongoDB服务器,所以它不能做任何你在常规应用程序中无法做到的事情。这是另一个测试和管理工具,您不应在正常操作中使用。



使用$where -operator



find命令中的$ where操作符允许传递javascript用于过滤值的函数。对于大多数微不足道的情况,这比find查询的其他工具所提供的性能低得多,尤其是因为它无法使用任何索引。



当使用$ where无法避免时,至少尝试使用find-query的一些普通工具来减少需要传递给$ where函数的文档集。



MapReduce



MapReduce使用两个javascript函数来创建聚合数据。它曾经是MongoDB的主要数据挖掘工具,但其大部分通常的用例现在都可以通过更加用户友好的方式来实现

它也有与$相同的缺点:其中:除非你过滤,否则你将不得不运行一个,但至少每个文档都有两个javascript函数。

但MapReduce至少可以运行分布式,并且可以并行化。



tl; dr:



使用Javascript是非常不寻常的查询的最后一种措施,不能用普通的查询语言进行查询,而且需要访问太多的数据在应用程序中执行。如果可能,请执行您想要使用的专用工具,或在应用程序层实现您的逻辑。


Does running server-side JavaScript have performance issues in MongoDB? Does V8 solve said performance issues?

Why does the MongoDB documentation recommended not using server-side functions?

解决方案

When you ask about the viability of server-sided javascript, you first have to make clear what kind of server-sided javascript you are talking about. According to the documentation, there are four different kinds of server-sided code execution. Unfortunately they all are kind of hackish workarounds for situations where the native API is insufficient:

The eval command

eval has the drawback that it only runs on one node. This greatly reduces its usefulness in a clustered environment. With sharded collections, it doesn't work at all!

By default, it also creates a global lock which makes the database completely unusable until the script has run. This can be prevented with the nolock argument (unless the script itself does something which creates a global lock).

The answer by Sammaye also explains some serious security concerns.

It's really more of a test- and administration tool than something you should use for any regular operation.

Running .js files via a mongo shell Instance on the Server

In this case, the code isn't executed on the database, but rather on another independent process on one of the servers. This means that all the required data from other shards must be transfered to the server which runs the javascript code, no matter what's actually returned by the script.

It appears as another application to the mongodb server, so it can't do anything you couldn't also do from within your regular application. It's another test- and administration tool you shouldn't use in regular operation.

Find with the $where-operator

The $where operator in a find-command allows to pass a javascript function which is used to filter values. For most trivial cases, this is a lot less performant than what the other tools of the find query have to offer, especially because it can't make use of any indices.

When usage of $where can't be avoided, at least try to use some of the normal tools of the find-query to reduce the set of documents which need to be passed to the $where function.

MapReduce

MapReduce uses two javascript functions to create aggregated data. It used to be the primary data-mining tool for MongoDB, but most of its usual use-cases are now fulfilled by the much more user-friendly aggregation framework.

It also has the same drawback as $where: Unless you filter, you will have to run not one but at least two javascript functions for each document.

But MapReduce can at least run distributed and it can be parallelized.

tl;dr:

Using Javascript is a last resort measure for very unusual queries which can not be made with the normal query language and which require access to too much data to be implemented in the application. When possible, do what you want to do with the specialized tools you have available or implement your logic on the application layer.

这篇关于服务器端JavaScript函数在mongoDB中有性能问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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