我收集了大约 75,000 个文档。
数据库总大小约为45GB。
在 75,000 个文档中,大约 45,000 个文档每个大小为 900 KB(约 42 GB),其余文档每个大小约为 120 KB。
每个文档都映射到其他集合中的 custId
ObjectId,并具有一个时间戳
,两者均已索引。
现在我需要获取上个月特定 custId
的文档。
计数约为 5500 个文档。此 custId
包含每个大小约为 120 KB 的小文档。
以下是我的查询:
db.mycollection.find(
{
custId:ObjectId("CUST_OBJECT_ID_HERE"),
timestamp:{$gte:one_month_ago_date, $lt:current_date}
}).sort({timestamp:-1})
查询仍然需要 2 分钟才能获取所有记录。是因为文档的数量还是较大文档的大小?有什么办法可以解决这个问题吗?
注意:
从nodejs 触发查询需要2 分钟。如果我在 mongo shell 上触发它,它会很快返回,但这可能是因为它只获取前 50 条记录。当我将 .count()
附加到 mongo shell 上的查询时,花了 2 分钟才返回计数。
更新:
索引详细信息:
"wiredTiger" : {
"nindexes" : 3,
"totalIndexSize" : 2396160,
"indexSizes" : {
"_id_" : 1138688,
"custId_1" : 598016,
"timestamp_1" : 659456
}
}
解释输出:(带排序)
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "mydb.mycollection",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"custId" : {
"$eq" : ObjectId("CUST_OBJECT_ID_HERE")
}
},
{
"timestamp" : {
"$lt" : ISODate("2017-05-15T14:20:04.393Z")
}
},
{
"timestamp" : {
"$gte" : ISODate("2017-04-15T14:20:04.393Z")
}
}
]
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"custId" : {
"$eq" : ObjectId("CUST_OBJECT_ID_HERE")
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"timestamp" : 1
},
"indexName" : "timestamp_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "backward",
"indexBounds" : {
"timestamp" : [
"(new Date(1494858004393), new Date(1492266004393)]"
]
}
}
},
"rejectedPlans" : [
{
"stage" : "SORT",
"sortPattern" : {
"timestamp" : -1
},
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"timestamp" : {
"$lt" : ISODate("2017-05-15T14:20:04.393Z")
}
},
{
"timestamp" : {
"$gte" : ISODate("2017-04-15T14:20:04.393Z")
}
}
]
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"custId" : 1
},
"indexName" : "custId_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"custId" : [
"[ObjectId('CUST_OBJECT_ID_HERE'), ObjectId('CUST_OBJECT_ID_HERE')]"
]
}
}
}
}
}
]
},
"serverInfo" : {
"host" : "test-machine",
"port" : 27017,
"version" : "3.2.12",
"gitVersion" : "REMOVED_BY_OP"
},
"ok" : 1
}
最佳答案
这就是索引的用途!
为时间戳和 custId 创建索引(两者的复合索引将是最有效的),就可以了。由于按时间戳排序,在复合索引中,将时间戳放在第一个(顺序很重要)
<小时/>这是在 mongo 中创建复合索引的代码:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema({
//...
});
userSchema.index({timestamp: 1, custId: 1});
mongoose.model('User', userSchema);
module.exports = userSchema;
关于node.js - Mongo find查询需要2分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43996532/