[英]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 inC++
.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
并再次检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.