Javascript mongodb查询速度超慢,可查询10K-20K以下的文档
我已经用Node/Express构建了一个API,它可以从一个免费的层mongo atlas数据库获取文档。对于GET请求,my Vue JS Frontend中记录的响应时间在3到15秒之间,总收集量通常在10-20K文档左右 我还尝试升级到付费集群,这对响应时间也没有任何积极影响,所以我的代码中可能有一些地方需要改进 这是我的问题Javascript mongodb查询速度超慢,可查询10K-20K以下的文档,javascript,node.js,mongodb,mongoose,axios,Javascript,Node.js,Mongodb,Mongoose,Axios,我已经用Node/Express构建了一个API,它可以从一个免费的层mongo atlas数据库获取文档。对于GET请求,my Vue JS Frontend中记录的响应时间在3到15秒之间,总收集量通常在10-20K文档左右 我还尝试升级到付费集群,这对响应时间也没有任何积极影响,所以我的代码中可能有一些地方需要改进 这是我的问题 const MINUTE = 1000 * 60; const HOUR = 1000 * 60 * 60; const DAY = 1000 * 60 * 60
const MINUTE = 1000 * 60;
const HOUR = 1000 * 60 * 60;
const DAY = 1000 * 60 * 60 * 24;
const INTERVALS = {
'1m' : MINUTE,
'5m' : MINUTE * 5,
'15m' : MINUTE * 15,
'30m' : MINUTE * 30,
'1h' : HOUR,
'2h' : HOUR * 2,
'4h' : HOUR * 4,
'12h' : HOUR * 12,
'1d' : DAY,
'1w' : DAY * 7,
'4w' : DAY * 7 * 4
};
result = await TransactionEvent.find({
timestamp : { $gte: Date.now() - INTERVALS[time] }
})
.lean()
.limit(Number(limit));
这是我的模型
const Schema = mongoose.Schema({
timestamp : { type: Date, default: Date.now, index: true },
direction : {
type : String,
required : true
},
contract : {
type : String,
required : true
},
symbol : {
type : String,
required : true
},
decimals : {
type : Number,
required : true
},
nametag : {
type : String,
required : false
},
balance : {
type : Number,
required : true
},
to : {
type : String,
required : true
},
from : {
type : String,
required : true
},
amount : {
type : Number,
required : true
},
value : {
type : Number,
required : true
},
txhash : {
type : String,
required : true
}
});
一旦收集超过10K的文档,响应时间就会变得无法使用。有时5秒,有时最多20秒+您可以对查找查询运行
解释,以生成查询计划。该计划可以深入了解查询的情况及其索引的使用情况。运行解释时使用“executionStats”模式。