// 索引在 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.collction.find({comId: “734f496571394931696b”}).skip(1900).limit(10).sort({loginTime: 1 }).explain(“executionStats”)
/* 1 */
{
“queryPlanner” : {
“plannerVersion” : 1,
“namespace” : “collocltion”,
“indexFilterSet” : false,
“parsedQuery” : {
“comId” : {
“$eq” : “734f496571394931696b”
}
},
“winningPlan” : {
“stage” : “SKIP”,
“skipAmount” : 0,
“inputStage” : {
“stage” : “SORT”,
“sortPattern” : {
“loginTime” : 1.0
},
“limitAmount” : 1910,
“inputStage” : {
“stage” : “SORT_KEY_GENERATOR”,
“inputStage” : {
“stage” : “FETCH”,
“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” : [
{
“stage” : “LIMIT”,
“limitAmount” : 10,
“inputStage” : {
“stage” : “SKIP”,
“skipAmount” : 1897,
“inputStage” : {
“stage” : “FETCH”,
“filter” : {
“comId” : {
“$eq” : “734f496571394931696b”
}
},
“inputStage” : {
“stage” : “IXSCAN”,
“keyPattern” : {
“loginTime” : 1
},
“indexName” : “loginTime”,
“isMultiKey” : false,
“multiKeyPaths” : {
“loginTime” : []
},
“isUnique” : false,
“isSparse” : false,
“isPartial” : false,
“indexVersion” : 2,
“direction” : “forward”,
“indexBounds” : {
“loginTime” : [
“[MinKey, MaxKey]”
]
}
}
}
}
}
]
},
“executionStats” : {
“executionSuccess” : true,
“nReturned” : 10,
“executionTimeMillis” : 85,
“totalKeysExamined” : 2053,
“totalDocsExamined” : 2053,
“executionStages” : {
“stage” : “SKIP”,
“nReturned” : 10,
“executionTimeMillisEstimate” : 35,
“works” : 3966,
“advanced” : 10,
“needTime” : 3955,
“needYield” : 0,
“saveState” : 62,
“restoreState” : 62,
“isEOF” : 1,
“skipAmount” : 0,
“inputStage” : {
“stage” : “SORT”,
“nReturned” : 1910,
“executionTimeMillisEstimate” : 35,
“works” : 3966,
“advanced” : 1910,
“needTime” : 2055,
“needYield” : 0,
“saveState” : 62,
“restoreState” : 62,
“isEOF” : 1,
“sortPattern” : {
“loginTime” : 1.0
},
“memUsage” : 932857,
“memLimit” : 33554432,
“limitAmount” : 1910,
“inputStage” : {
“stage” : “SORT_KEY_GENERATOR”,
“nReturned” : 2053,
“executionTimeMillisEstimate” : 35,
“works” : 2055,
“advanced” : 2053,
“needTime” : 1,
“needYield” : 0,
“saveState” : 62,
“restoreState” : 62,
“isEOF” : 1,
“inputStage” : {
“stage” : “FETCH”,
“nReturned” : 2053,
“executionTimeMillisEstimate” : 35,
“works” : 2054,
“advanced” : 2053,
“needTime” : 0,
“needYield” : 0,
“saveState” : 62,
“restoreState” : 62,
“isEOF” : 1,
“docsExamined” : 2053,
“alreadyHasObj” : 0,
“inputStage” : {
“stage” : “IXSCAN”,
“nReturned” : 2053,
“executionTimeMillisEstimate” : 0,
“works” : 2054,
“advanced” : 2053,
“needTime” : 0,
“needYield” : 0,
“saveState” : 62,
“restoreState” : 62,
“isEOF” : 1,
“keyPattern” : {
“comId” : 1
},
“indexName” : “comId_1”,
“isMultiKey” : false,
“multiKeyPaths” : {
“comId” : []
},
“isUnique” : false,
“isSparse” : false,
“isPartial” : false,
“indexVersion” : 2,
“direction” : “forward”,
“indexBounds” : {
“comId” : [
“[\”734f496571394931696b\”, \”734f496571394931696b\”]”
]
},
“keysExamined” : 2053,
“seeks” : 1,
“dupsTested” : 0,
“dupsDropped” : 0
}
}
}
}
}
},
“serverInfo” : {
“host” : “wenqiang.wang-suyan-lts.novalocal”,
“port” : 27017,
“version” : “4.2.3”,
“gitVersion” : “6874650b362138df74be53d366bbefc321ea32d4”
},
“ok” : 1.0
}
根据你这个执行计划,skip不同,执行计划不同。