// 索引在 comId, 和 loginTime 上都有但对的索引
find // db.collontion.find({comId: “734f496571394931696b”}).skip(1200).limit(10).sort({loginTime: 1 })
aggregate // db.collection.aggregate( [{ $match: { comId: “734f496571394931696b”} }, { $sort: { loginTime: 1 } }, { $skip: 1200}, { $limit: 10 }] )
- find查询走comId索引, 数据量少 ,耗时很小
- aggregate走loginTime 全索引扫描加全文档检查。耗时非常长
db.colloction.aggregate( [{ $match: { comId: “734f496571394931696b”, abnormal: 1} }, { $skip: 1900 }, { $limit: 10 },{ $sort: { loginTime: -1 } }] , {explain: true})
/* 1 */
{
“stages” : [
{
“$cursor” : {
“query” : {
“comId” : “734f496571394931696b”,
“abnormal” : 1.0
},
“queryPlanner” : {
“plannerVersion” : 1,
“namespace” : “collcltion”,
“indexFilterSet” : false,
“parsedQuery” : {
“$and” : [
{
“abnormal” : {
“$eq” : 1.0
}
},
{
“comId” : {
“$eq” : “734f496571394931696b”
}
}
]
},
“queryHash” : “008C97FE”,
“planCacheKey” : “34FC53A9”,
“winningPlan” : {
“stage” : “FETCH”,
“filter” : {
“abnormal” : {
“$eq” : 1.0
}
},
“inputStage” : {
“stage” : “IXSCAN”,
“keyPattern” : {
“comId” : 1
},
“indexName” : “comId_1”,
“isMultiKey” : false,
“multiKeyPaths” : {
“comId” : []
},
“isUnique” : false,
“isSparse” : false,
“isPartial” : false,
“indexVersion” : 2,
“direction” : “forward”,
“indexBounds” : {
“comId” : [
“[\”734f496571394931696b\”, \”734f496571394931696b\”]”
]
}
}
},
“rejectedPlans” : []
}
}
},
{
“$skip” : NumberLong(1900)
},
{
“$limit” : NumberLong(10)
},
{
“$sort” : {
“sortKey” : {
“loginTime” : -1
}
}
}
],
“serverInfo” : {
“host” : “wenqiang.wang-suyan-lts.novalocal”,
“port” : 27017,
“version” : “4.2.3”,
“gitVersion” : “6874650b362138df74be53d366bbefc321ea32d4”
},
“ok” : 1.0
}
根据你这个执行计划,skip不同,执行计划不同。