Hi,
Regarding the indexing rule of equality/sort/range, I am using this tutorial as knowledge base: Chapter_4_CRUD_Optimization
I need help understanding why the rule of equality/sort/range is not applying to my example (which is a simplified reproducer of my real issue):
// ----- INIT DATA -----
use test;
db.test.drop();
db.test.insertMany([ // 10 items
{ a: 1, b: 101, c:0, d:1 },
{ a: 1, b: 102, c:0, d:1 },
{ a: 1, b: 103, c:0, d:1 },
{ a: 1, b: 104, c:0, d:1 },
{ a: 1, b: 105, c:0, d:1 },
{ a: 2, b: 106, c:0, d:1 },
{ a: 2, b: 107, c:0, d:1 },
{ a: 2, b: 108, c:0, d:1 },
{ a: 2, b: 109, c:0, d:1 },
{ a: 2, b: 110, c:0, d:1 },
]);
// ----- INIT INDEX -----
db.test.dropIndexes();
db.test.createIndex({b : 1, a : 1}); // index b (sort) + a (range)
// ----- QUERY -----
db.test.find({a:{$lt:2}}, {a:1, _id:0}).sort({b:1}).explain("executionStats");
I am trying to use the compound index to sort data by “b” using index key “b : 1” and then to filter data by “a < 2” using index key “a : 1”.
==> It doesn’t work. It sorts data using the index but it doesn’t filter using the index. It filters using FETCH with the filter “a < 2”.
{
"explainVersion" : "1",
"queryPlanner" : {
"namespace" : "test.test",
"indexFilterSet" : false,
"parsedQuery" : {
"a" : {
"$lt" : 2
}
},
"maxIndexedOrSolutionsReached" : false,
"maxIndexedAndSolutionsReached" : false,
"maxScansToExplodeReached" : false,
"winningPlan" : {
"stage" : "PROJECTION_SIMPLE",
"transformBy" : {
"a" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"a" : {
"$lt" : 2
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"b" : 1,
"a" : 1
},
"indexName" : "b_1_a_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"b" : [ ],
"a" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"b" : [
"[MinKey, MaxKey]"
],
"a" : [
"[MinKey, MaxKey]"
]
}
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 5,
"executionTimeMillis" : 1,
"totalKeysExamined" : 10,
"totalDocsExamined" : 10,
"executionStages" : {
"stage" : "PROJECTION_SIMPLE",
"nReturned" : 5,
"executionTimeMillisEstimate" : 0,
"works" : 11,
"advanced" : 5,
"needTime" : 5,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"transformBy" : {
"a" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"a" : {
"$lt" : 2
}
},
"nReturned" : 5,
"executionTimeMillisEstimate" : 0,
"works" : 11,
"advanced" : 5,
"needTime" : 5,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"docsExamined" : 10,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 10,
"executionTimeMillisEstimate" : 0,
"works" : 11,
"advanced" : 10,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"keyPattern" : {
"b" : 1,
"a" : 1
},
"indexName" : "b_1_a_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"b" : [ ],
"a" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"b" : [
"[MinKey, MaxKey]"
],
"a" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 10,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0
}
}
}
},
"command" : {
"find" : "test",
"filter" : {
"a" : {
"$lt" : 2
}
},
"sort" : {
"b" : 1
},
"projection" : {
"a" : 1,
"_id" : 0
},
"$db" : "test"
},
"serverInfo" : {
"host" : "LAPTOP-8552N07T",
"port" : 27017,
"version" : "5.0.1",
"gitVersion" : "318fd9cabc59dc9651f3189b622af6e06ab6cd33"
},
"serverParameters" : {
"internalQueryFacetBufferSizeBytes" : 104857600,
"internalQueryFacetMaxOutputDocSizeBytes" : 104857600,
"internalLookupStageIntermediateDocumentMaxSizeBytes" : 104857600,
"internalDocumentSourceGroupMaxMemoryBytes" : 104857600,
"internalQueryMaxBlockingSortMemoryUsageBytes" : 104857600,
"internalQueryProhibitBlockingMergeOnMongoS" : 0,
"internalQueryMaxAddToSetBytes" : 104857600,
"internalDocumentSourceSetWindowFieldsMaxMemoryBytes" : 104857600
},
"ok" : 1
}