Cor*_*len 9 mongodb node.js koa mongodb-atlas
我遇到MongoDB的主要性能问题.简单的find()查询有时需要2,000-3,000毫秒才能在少于100个文档的数据库中完成.
我在MongoDB Atlas M10实例以及我在具有4GB RAM的VM上在Digital Ocean上设置的集群中都看到了这一点.当我在Heroku上重新启动我的Node.js应用程序时,查询表现良好(小于100毫秒),持续10-15分钟,但随后它们变慢.
我是否错误地连接到MongoDB或从Node.js错误地查询?请参阅下面的应用程序代码.或者这是共享VM环境中缺少硬件资源?
任何帮助将不胜感激.我已经完成了解释查询和Mongo shell的所有故障排除.
var Koa = require('koa'); //v2.4.1
var Router = require('koa-router'); //v7.3.0
var MongoClient = require('mongodb').MongoClient; //v3.1.3
var app = new Koa();
var router = new Router();
app.use(router.routes());
//Connect to MongoDB
async function connect() {
try {
var client = await MongoClient.connect(process.env.MONGODB_URI, {
readConcern: { level: 'local' }
});
var db = client.db(process.env.MONGODB_DATABASE);
return db;
}
catch (error) {
console.log(error);
}
}
//Add MongoDB to Koa's ctx object
connect().then(db => {
app.context.db = db;
});
//Get company's collection in MongoDB
router.get('/documents/:collection', async (ctx) => {
try {
var query = { company_id: ctx.state.session.company_id };
var res = await ctx.db.collection(ctx.params.collection).find(query).toArray();
ctx.body = { ok: true, docs: res };
}
catch (error) {
ctx.status = 500;
ctx.body = { ok: false };
}
});
app.listen(process.env.PORT || 3000);
Run Code Online (Sandbox Code Playgroud)
UPDATE
我正在使用MongoDB Change Streams和标准的Server Sent Events来为应用程序UI提供实时更新.我关闭了这些,现在MongoDB似乎再次表现良好.
MongoDB Change Streams是否会影响读/写性能?
Change Streams indeed affect the performance of your server. As noted in this SO question.
As mentioned in the accepted answer there,
The default connection pool size in the Node.js client for MongoDB is 5. Since each change stream cursor opens a new connection, the connection pool needs to be at least as large as the number of cursors.
Run Code Online (Sandbox Code Playgroud)const mongoConnection = await MongoClient.connect(URL, {poolSize: 100});(Thanks to MongoDB Inc. for investigating this issue.)
您需要增加池大小以恢复正常性能。
| 归档时间: |
|
| 查看次数: |
861 次 |
| 最近记录: |