简体   繁体   English

为什么 mongodb 查询执行时间比使用 node 的实际结果低得多?

[英]Why is mongodb query execution time much lower than actual result using node?

I have an API endpoint in node that uses a simple query to retrieve about 1.8MB of data from mongodb (size shown in mongodb Compass).我在节点中有一个 API 端点,它使用一个简单的查询从 mongodb 检索大约 1.8MB 的数据(大小在 mongodb Compass 中显示)。 The query uses an index and the explain plan shows the query execution time is almost 0 ms.查询使用索引,解释计划显示查询执行时间几乎为 0 毫秒。 But, when I console log the time to retrieve the data in node, it takes about 200 ms.但是,当我控制台记录在节点中检索数据的时间时,大约需要 200 毫秒。 What is happening between mongodb finding the results and having the results available in node?在 mongodb 查找结果和在 node 中提供结果之间发生了什么? Is there a way to speed this up?有没有办法加快这个速度? The mongodb server is on the same server as the node app. mongodb 服务器与节点应用程序在同一台服务器上。 Here is how I'm timing the retrieval from mongodb in my node/express route:以下是我如何在我的节点/快递路线中对从 mongodb 的检索计时:

app.get('/myRoute', function(req, res) {

    //get mongodb connection client
    const db = app.locals.db

    //start timer
    let start = new Date()

    //query DB
    db.collection("my_collection").find({year: 2019}).toArray()
    .then((data)=> {

        //log timer shows about 200 ms
        console.log(new Date() - start)

        res.writeHead(200, {'Content-Type': 'application/json'});
        res.end(JSON.stringify(data))
    })
})

My guess is since the amount of data is of decent size that it takes a while to convert from BSON to a JS object.我的猜测是,由于数据量相当大,因此从 BSON 转换为 JS 对象需要一段时间。 If so, anyway to speed this up?如果是这样,无论如何要加快速度? I send this to the client as JSON but preferably as gzip JSON (to speed up the download).我将此作为 JSON 发送给客户端,但最好作为 gzip JSON(以加快下载速度)。

The data are for reporting purposes but it just seems inefficient to query mongodb -> mongodb then converts to JS object -> stringify to JSON -> potentially gzip the JSON -> then send to client.数据用于报告目的,但查询 mongodb -> mongodb 然后转换为 JS 对象 -> 字符串化为 JSON -> 可能对 JSON 进行 gzip -> 然后发送到客户端似乎效率低下。

In production I use a file system with many files and folders with pre gzipped JSON that the API finds and sends to client.在生产中,我使用一个包含许多文件和文件夹的文件系统,其中包含 API 找到并发送到客户端的预压缩 JSON。 This is very fast but just hard to maintain.这非常快,但很难维护。 Any thoughts?有什么想法吗? Thanks!谢谢!

I guess you will always have the overhead if you will compare the time it takes to run query on mongo vs via node.我想如果您比较在 mongo 和通过节点上运行查询所需的时间,您将始终有开销。 You can try streaming the data instead of loading data first, stringifying it and then sending it over.您可以尝试流式传输数据,而不是先加载数据,将其字符串化然后再发送。 Something like this might help.像这样的事情可能会有所帮助。

app.get('/myRoute', function(req, res) {

  //get mongodb connection client
  const db = app.locals.db

  //start timer
  console.time("start");

  //query DB
  const query = db.collection("my_collection").find({year: 2019});
  query.stream().on("data", function (d) {
    res.json(d);
  });
  query.stream().on("end", function () {
    console.log("done"); res.end();
    console.timeEnd("start");
  });
})

The bson-ext module is an alternative BSON parser that is written in C++ . bson-ext模块是一个用C++编写的替代 BSON 解析器。 It delivers better deserialization performance and similar or somewhat better serialization performance to the pure javascript parser.它提供了更好的反序列化性能和与纯 javascript 解析器相似或更好的序列化性能。

https://mongodb.github.io/node-mongodb-native/3.4/installation-guide/installation-guide/#troubleshooting https://mongodb.github.io/node-mongodb-native/3.4/installation-guide/installation-guide/#troubleshooting

Try to install bson-ext and check again.尝试安装bson-ext并再次检查。

Benchmarking JSON vs BSON 基准 JSON 与 BSON

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

相关问题 为什么 console.time 显示的时间比实际时间短得多? - Why does console.time show much lower time than the actual time? 为什么Node JS中的Mysql查询执行比直接的Mysql查询执行要慢得多? - Why is a Mysql query execution in Node JS so much slower than a direct Mysql query execution? 使用node.js执行查询后返回MySQL结果 - Return MySQL result after query execution using node.js 为什么 EventMachine 比 Node 慢这么多? - Why is EventMachine so much slower than Node? 使用node在mongodb上执行查找查询时获得正确的结果 - Get the correct result while executing a find query on mongodb using node 为什么 Node.js 中的某些 TCP 套接字比大多数其他套接字花费更多的时间来连接? - Why some TCP sockets in Node.js take much more time to connect than most others? 为什么MongoDB比插入(使用唯一索引)要快得多? - Why are MongoDB upserts so much slower than inserts (with a unique index)? mongodb查询返回结果,并用节点导出 - Returning result of mongodb query, and exporting with node 为什么节点(+ Cheerio)使用这么多RAM? - Why is Node (+Cheerio) using so much RAM? 为什么针对node.js的Mysql Native驱动程序的查询执行时间如此之长? 还有其他选择吗? - Why is query execution time so high for Mysql Native driver for node.js? Any alternatives?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM