我是 MongoDB 和 Node.js 的新手。
我编写了一个示例应用程序来测量使用 Mongoose 向 MongoDB 发出获取请求的速度。
我有一个包含大约 200000 条记录的集合。在我的代码中,我想通过查询获取前 100000 行:
var query = db.myCollection.find().limit(100000);
query.exec(function(err, data){
// ....
});
大约花了99秒,我觉得速度太慢了。有人知道如何加快查询速度吗?
非常感谢!
使用 mongodb,您可以对某些键建立索引,以提高查询时的性能。但在这种情况下,这是行不通的。
您的 99 可能是由您的 PC 造成的,它无法一次性处理这种繁重的数据负载(这是完全可以理解的)。
这绝对与 mongodb 无关,而是与您的测试机内存相关。
但是你也许可以通过管道传输结果来改进它:
// use our lame formatter
var format = new ArrayFormatter;
// first pipe the querystream to the formatter
myCollection.find().stream().pipe(format);
// then pipe the formatter to the response
// (node 0.4x style pipe non-chaining)
format.pipe(res);
// In node 0.6 we can P.find().stream().pipe(format).pipe(res);
与 this gist
我是一名优秀的程序员,十分优秀!