I have a collection that contains:
{ _id: 1, reportId: "a", accountId: "1" },
{ _id: 2, reportId: "b", accountId: "1" }
I have created compound Index:
db.collection.createIndex({ reportId: 1, accountId: 1 });
Executing query:
db.collection.find({ reportId: "a", accountId: "1" }).sort({ _id: 1 });
This query uses _id
index instead of compound index, and compound index in in rejected plain,
FETCH Details
{
"stage": "FETCH",
"filter": {
"$and": [
{
"accountId": {
"$eq": "1"
}
},
{
"reportId": {
"$eq": "a"
}
}
]
},
"nReturned": 1,
"executionTimeMillisEstimate": 0,
"works": 4,
"advanced": 1,
"needTime": 1,
"needYield": 0,
"saveState": 0,
"restoreState": 0,
"isEOF": 1,
"docsExamined": 2,
"alreadyHasObj": 0
}
IXSCAN Details
{
"stage": "IXSCAN",
"nReturned": 2,
"executionTimeMillisEstimate": 0,
"works": 3,
"advanced": 2,
"needTime": 0,
"needYield": 0,
"saveState": 0,
"restoreState": 0,
"isEOF": 1,
"keyPattern": {
"_id": 1
},
"indexName": "_id_",
"isMultiKey": false,
"multiKeyPaths": {
"_id": []
},
"isUnique": true,
"isSparse": false,
"isPartial": false,
"indexVersion": 2,
"direction": "forward",
"indexBounds": {
"_id": [
"[MinKey, MaxKey]"
]
},
"keysExamined": 2,
"seeks": 1,
"dupsTested": 0,
"dupsDropped": 0,
"parentName": "FETCH"
}
Raw JSON of explain
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "sample.index1",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"accountId" : {
"$eq" : "1"
}
},
{
"reportId" : {
"$eq" : "a"
}
}
]
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"accountId" : {
"$eq" : "1"
}
},
{
"reportId" : {
"$eq" : "a"
}
}
]
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"_id" : 1
},
"indexName" : "_id_",
"isMultiKey" : false,
"multiKeyPaths" : {
"_id" : []
},
"isUnique" : true,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"_id" : [
"[MinKey, MaxKey]"
]
}
}
},
"rejectedPlans" : [
{
"stage" : "SORT",
"sortPattern" : {
"_id" : 1
},
"memLimit" : 104857600,
"type" : "simple",
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"reportId" : 1.0,
"accountId" : 1.0
},
"indexName" : "reportId_1_accountId_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"reportId" : [],
"accountId" : []
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"reportId" : [
"[\"a\", \"a\"]"
],
"accountId" : [
"[\"1\", \"1\"]"
]
}
}
}
}
]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 0,
"totalKeysExamined" : 2,
"totalDocsExamined" : 2,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"accountId" : {
"$eq" : "1"
}
},
{
"reportId" : {
"$eq" : "a"
}
}
]
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 4,
"advanced" : 1,
"needTime" : 1,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"docsExamined" : 2,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"works" : 3,
"advanced" : 2,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"keyPattern" : {
"_id" : 1
},
"indexName" : "_id_",
"isMultiKey" : false,
"multiKeyPaths" : {
"_id" : []
},
"isUnique" : true,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"_id" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 2,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0
}
}
},
"serverInfo" : {
"host" : "vt",
"port" : 27017,
"version" : "4.4.5",
"gitVersion" : "ff5cb77101b052fa02da43b8538093486cf9b3f7"
},
"ok" : 1.0
}
Its really required to add _id
field in compound index?