// 索引在 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 全索引扫描加全文档检查。耗时非常长
/* 1 */
[
{
“v” : 2,
“key” : {
“_id” : 1
},
“name” : “_id_”,
“ns” : “collection”
},
{
“v” : 2,
“key” : {
“comId” : 1
},
“name” : “comId_1”,
“ns” : “collection”
},
{
“v” : 2,
“key” : {
“loginTime” : 1
},
“name” : “loginTime”,
“ns” : “collection”
}
]
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
}
db.colloction.aggregate( [{ $match: { comId: “734f496571394931696b”, abnormal: 1} }, { $sort: { loginTime: -1 } }, { $skip: 0 }, { $limit: 10 }] , {explain: true})
/* 1 */
{
“stages” : [
{
“$cursor” : {
“query” : {
“comId” : “734f496571394931696b”,
“abnormal” : 1.0
},
“sort” : {
“loginTime” : -1
},
“limit” : NumberLong(1910),
“queryPlanner” : {
“plannerVersion” : 1,
“namespace” : “cmcc_20210729.cmcc_detect_abnormallogin”,
“indexFilterSet” : false,
“parsedQuery” : {
“$and” : [
{
“abnormal” : {
“$eq” : 1.0
}
},
{
“comId” : {
“$eq” : “734f496571394931696b”
}
}
]
},
“queryHash” : “E4B351FC”,
“planCacheKey” : “59752D0F”,
“winningPlan” : {
“stage” : “FETCH”,
“filter” : {
“$and” : [
{
“abnormal” : {
“$eq” : 1.0
}
},
{
“comId” : {
“$eq” : “734f496571394931696b”
}
}
]
},
“inputStage” : {
“stage” : “IXSCAN”,
“keyPattern” : {
“loginTime” : 1
},
“indexName” : “loginTime”,
“isMultiKey” : false,
“multiKeyPaths” : {
“loginTime” : []
},
“isUnique” : false,
“isSparse” : false,
“isPartial” : false,
“indexVersion” : 2,
“direction” : “backward”,
“indexBounds” : {
“loginTime” : [
“[MaxKey, MinKey]”
]
}
}
},
“rejectedPlans” : []
}
}
},
{
“$skip” : NumberLong(1900)
}
],
“serverInfo” : {
“host” : “wenqiang.wang-suyan-lts.novalocal”,
“port” : 27017,
“version” : “4.2.3”,
“gitVersion” : “6874650b362138df74be53d366bbefc321ea32d4”
},
“ok” : 1.0
}
根据你这个执行计划,skip不同,采用索引不同。你find里面对不同skip看下执行计划
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不同,执行计划不同。