- I have the following query
db.MDocumentContent.find({
"updatedTime": {
"$lte": ISODate('2023-08-20T16:00:00.000Z')
},
"type": {
"$nin": [
"default",
"customFigureSample"
]
},
"isClean": {
"$in": [
null,
false
]
},
"_id": {
"$gt": ObjectId("611a18d87c62f5002fb0809c")
}
}).sort({ _id: 1 }).limit(10);
Analyzing the query with explain reveals that the index used is id instead of _id_1_updatedTime_1_type_1_isClean_1.
{
"queryPlanner" : {
"plannerVersion" : 1.0,
"namespace" : "mtv.MDocumentContent",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"updatedTime" : {
"$lte" : ISODate("2023-08-20T16:00:00.000+0000")
}
},
{
"_id" : {
"$gt" : ObjectId("611a18d87c62f5002fb0809c")
}
},
{
"isClean" : {
"$in" : [
null,
false
]
}
},
{
"type" : {
"$not" : {
"$in" : [
"customFigureSample",
"default"
]
}
}
}
]
},
"winningPlan" : {
"stage" : "LIMIT",
"limitAmount" : 10.0,
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"updatedTime" : {
"$lte" : ISODate("2023-08-20T16:00:00.000+0000")
}
},
{
"isClean" : {
"$in" : [
null,
false
]
}
},
{
"type" : {
"$not" : {
"$in" : [
"customFigureSample",
"default"
]
}
}
}
]
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"_id" : 1.0
},
"indexName" : "_id_",
"isMultiKey" : false,
"multiKeyPaths" : {
"_id" : [
]
},
"isUnique" : true,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2.0,
"direction" : "forward",
"indexBounds" : {
"_id" : [
"(ObjectId('611a18d87c62f5002fb0809c'), ObjectId('ffffffffffffffffffffffff')]"
]
}
}
}
},
"rejectedPlans" : [
{
"stage" : "LIMIT",
"limitAmount" : 10.0,
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"isClean" : {
"$in" : [
null,
false
]
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"_id" : 1.0,
"updatedTime" : 1.0,
"type" : 1.0,
"isClean" : 1.0
},
"indexName" : "_id_1_updatedTime_1_type_1_isClean_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"_id" : [
],
"updatedTime" : [
],
"type" : [
],
"isClean" : [
]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2.0,
"direction" : "forward",
"indexBounds" : {
"_id" : [
"(ObjectId('611a18d87c62f5002fb0809c'), ObjectId('ffffffffffffffffffffffff')]"
],
"updatedTime" : [
"(true, new Date(1692547200000)]"
],
"type" : [
"[MinKey, \"customFigureSample\")",
"(\"customFigureSample\", \"default\")",
"(\"default\", MaxKey]"
],
"isClean" : [
"[undefined, undefined]",
"[null, null]",
"[false, false]"
]
}
}
}
}
]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 10.0,
"executionTimeMillis" : 12.0,
"totalKeysExamined" : 2256.0,
"totalDocsExamined" : 2256.0,
"totalKeysExaminedBySizeInBytes" : 42864.0,
"totalDocsExaminedBySizeInBytes" : 3409047.0,
"executionStages" : {
"stage" : "LIMIT",
"nReturned" : 10.0,
"executionTimeMillisEstimate" : 1.0,
"works" : 2257.0,
"advanced" : 10.0,
"needTime" : 2246.0,
"needYield" : 0.0,
"saveState" : 35.0,
"restoreState" : 35.0,
"isEOF" : 1.0,
"limitAmount" : 10.0,
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"updatedTime" : {
"$lte" : ISODate("2023-08-20T16:00:00.000+0000")
}
},
{
"isClean" : {
"$in" : [
null,
false
]
}
},
{
"type" : {
"$not" : {
"$in" : [
"customFigureSample",
"default"
]
}
}
}
]
},
"nReturned" : 10.0,
"executionTimeMillisEstimate" : 1.0,
"works" : 2256.0,
"advanced" : 10.0,
"needTime" : 2246.0,
"needYield" : 0.0,
"saveState" : 35.0,
"restoreState" : 35.0,
"isEOF" : 0.0,
"docsExamined" : 2256.0,
"docsExaminedBySizeInBytes" : 3409047.0,
"alreadyHasObj" : 0.0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 2256.0,
"executionTimeMillisEstimate" : 0.0,
"works" : 2256.0,
"advanced" : 2256.0,
"needTime" : 0.0,
"needYield" : 0.0,
"saveState" : 35.0,
"restoreState" : 35.0,
"isEOF" : 0.0,
"keyPattern" : {
"_id" : 1.0
},
"indexName" : "_id_",
"isMultiKey" : false,
"multiKeyPaths" : {
"_id" : [
]
},
"isUnique" : true,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2.0,
"direction" : "forward",
"indexBounds" : {
"_id" : [
"(ObjectId('611a18d87c62f5002fb0809c'), ObjectId('ffffffffffffffffffffffff')]"
]
},
"keysExamined" : 2256.0,
"keysExaminedBySizeInBytes" : 42864.0,
"seeks" : 1.0,
"dupsTested" : 0.0,
"dupsDropped" : 0.0
}
}
},
"allPlansExecution" : [
{
"nReturned" : 10.0,
"executionTimeMillisEstimate" : 1.0,
"totalKeysExamined" : 2256.0,
"totalDocsExamined" : 2256.0,
"totalKeysExaminedBySizeInBytes" : 42864.0,
"totalDocsExaminedBySizeInBytes" : 3409047.0,
"executionStages" : {
"stage" : "LIMIT",
"nReturned" : 10.0,
"executionTimeMillisEstimate" : 1.0,
"works" : 2256.0,
"advanced" : 10.0,
"needTime" : 2246.0,
"needYield" : 0.0,
"saveState" : 35.0,
"restoreState" : 35.0,
"isEOF" : 1.0,
"limitAmount" : 10.0,
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"updatedTime" : {
"$lte" : ISODate("2023-08-20T16:00:00.000+0000")
}
},
{
"isClean" : {
"$in" : [
null,
false
]
}
},
{
"type" : {
"$not" : {
"$in" : [
"customFigureSample",
"default"
]
}
}
}
]
},
"nReturned" : 10.0,
"executionTimeMillisEstimate" : 1.0,
"works" : 2256.0,
"advanced" : 10.0,
"needTime" : 2246.0,
"needYield" : 0.0,
"saveState" : 35.0,
"restoreState" : 35.0,
"isEOF" : 0.0,
"docsExamined" : 2256.0,
"docsExaminedBySizeInBytes" : 3409047.0,
"alreadyHasObj" : 0.0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 2256.0,
"executionTimeMillisEstimate" : 0.0,
"works" : 2256.0,
"advanced" : 2256.0,
"needTime" : 0.0,
"needYield" : 0.0,
"saveState" : 35.0,
"restoreState" : 35.0,
"isEOF" : 0.0,
"keyPattern" : {
"_id" : 1.0
},
"indexName" : "_id_",
"isMultiKey" : false,
"multiKeyPaths" : {
"_id" : [
]
},
"isUnique" : true,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2.0,
"direction" : "forward",
"indexBounds" : {
"_id" : [
"(ObjectId('611a18d87c62f5002fb0809c'), ObjectId('ffffffffffffffffffffffff')]"
]
},
"keysExamined" : 2256.0,
"keysExaminedBySizeInBytes" : 42864.0,
"seeks" : 1.0,
"dupsTested" : 0.0,
"dupsDropped" : 0.0
}
}
}
},
{
"nReturned" : 10.0,
"executionTimeMillisEstimate" : 0.0,
"totalKeysExamined" : 2256.0,
"totalDocsExamined" : 10.0,
"totalKeysExaminedBySizeInBytes" : 76694.0,
"totalDocsExaminedBySizeInBytes" : 45016.0,
"executionStages" : {
"stage" : "LIMIT",
"nReturned" : 10.0,
"executionTimeMillisEstimate" : 0.0,
"works" : 2256.0,
"advanced" : 10.0,
"needTime" : 2246.0,
"needYield" : 0.0,
"saveState" : 35.0,
"restoreState" : 35.0,
"isEOF" : 1.0,
"limitAmount" : 10.0,
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"isClean" : {
"$in" : [
null,
false
]
}
},
"nReturned" : 10.0,
"executionTimeMillisEstimate" : 0.0,
"works" : 2256.0,
"advanced" : 10.0,
"needTime" : 2246.0,
"needYield" : 0.0,
"saveState" : 35.0,
"restoreState" : 35.0,
"isEOF" : 0.0,
"docsExamined" : 10.0,
"docsExaminedBySizeInBytes" : 45016.0,
"alreadyHasObj" : 0.0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 10.0,
"executionTimeMillisEstimate" : 0.0,
"works" : 2256.0,
"advanced" : 10.0,
"needTime" : 2246.0,
"needYield" : 0.0,
"saveState" : 35.0,
"restoreState" : 35.0,
"isEOF" : 0.0,
"keyPattern" : {
"_id" : 1.0,
"updatedTime" : 1.0,
"type" : 1.0,
"isClean" : 1.0
},
"indexName" : "_id_1_updatedTime_1_type_1_isClean_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"_id" : [
],
"updatedTime" : [
],
"type" : [
],
"isClean" : [
]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2.0,
"direction" : "forward",
"indexBounds" : {
"_id" : [
"(ObjectId('611a18d87c62f5002fb0809c'), ObjectId('ffffffffffffffffffffffff')]"
],
"updatedTime" : [
"(true, new Date(1692547200000)]"
],
"type" : [
"[MinKey, \"customFigureSample\")",
"(\"customFigureSample\", \"default\")",
"(\"default\", MaxKey]"
],
"isClean" : [
"[undefined, undefined]",
"[null, null]",
"[false, false]"
]
},
"keysExamined" : 2256.0,
"keysExaminedBySizeInBytes" : 76694.0,
"seeks" : 2247.0,
"dupsTested" : 0.0,
"dupsDropped" : 0.0
}
}
}
}
]
},
"ok" : 1.0,
"$clusterTime" : {
"clusterTime" : Timestamp(1708585127, 18),
"signature" : {
"hash" : BinData(0, "0YV3kUHkU1JPVWVekohkmjPCkdE="),
"keyId" : NumberLong(7285838794148806712)
}
},
"operationTime" : Timestamp(1708585127, 18)
}
Personally, I think _id_1_updatedTime_1_type_1_isClean_1 is the best index.
Does anyone know why this is