I have a collection faults
in my MongoDB
database which every document has these fields: rack_name
, timestamp
Just for sake of testing and comparing performances, I have created these two indexes:
rack -> {'rack_name': 1}
and
time -> {'timestamp': 1}
Now I executed the following query with explain():
db.faults.find({
'rack_name': {
$in: [ 'providence1', 'helena2' ]
},
'timestamp': {
$gt: 1501548359000
}
})
.explain('allPlansExecution')
and here is the result:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "quicktester_clone.faults",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"timestamp" : {
"$gt" : 1501548359000.0
}
},
{
"rack_name" : {
"$in" : [
"helena2",
"providence1"
]
}
}
]
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"timestamp" : {
"$gt" : 1501548359000.0
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"rack_name" : 1
},
"indexName" : "rack",
"isMultiKey" : false,
"multiKeyPaths" : {
"rack_name" : []
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"rack_name" : [
"[\"helena2\", \"helena2\"]",
"[\"providence1\", \"providence1\"]"
]
}
}
},
"rejectedPlans" : [
{
"stage" : "FETCH",
"filter" : {
"rack_name" : {
"$in" : [
"helena2",
"providence1"
]
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"timestamp" : 1
},
"indexName" : "time",
"isMultiKey" : false,
"multiKeyPaths" : {
"timestamp" : []
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"timestamp" : [
"(1501548359000.0, inf.0]"
]
}
}
}
]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 43,
"executionTimeMillis" : 1512,
"totalKeysExamined" : 221,
"totalDocsExamined" : 219,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"timestamp" : {
"$gt" : 1501548359000.0
}
},
"nReturned" : 43,
"executionTimeMillisEstimate" : 1431,
"works" : 222,
"advanced" : 43,
"needTime" : 177,
"needYield" : 0,
"saveState" : 64,
"restoreState" : 64,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 219,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 219,
"executionTimeMillisEstimate" : 71,
"works" : 221,
"advanced" : 219,
"needTime" : 1,
"needYield" : 0,
"saveState" : 64,
"restoreState" : 64,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"rack_name" : 1
},
"indexName" : "rack",
"isMultiKey" : false,
"multiKeyPaths" : {
"rack_name" : []
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"rack_name" : [
"[\"helena2\", \"helena2\"]",
"[\"providence1\", \"providence1\"]"
]
},
"keysExamined" : 221,
"seeks" : 2,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
},
"allPlansExecution" : [
{
"nReturned" : 2,
"executionTimeMillisEstimate" : 31,
"totalKeysExamined" : 221,
"totalDocsExamined" : 221,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"rack_name" : {
"$in" : [
"helena2",
"providence1"
]
}
},
"nReturned" : 2,
"executionTimeMillisEstimate" : 31,
"works" : 221,
"advanced" : 2,
"needTime" : 219,
"needYield" : 0,
"saveState" : 64,
"restoreState" : 64,
"isEOF" : 0,
"invalidates" : 0,
"docsExamined" : 221,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 221,
"executionTimeMillisEstimate" : 10,
"works" : 221,
"advanced" : 221,
"needTime" : 0,
"needYield" : 0,
"saveState" : 64,
"restoreState" : 64,
"isEOF" : 0,
"invalidates" : 0,
"keyPattern" : {
"timestamp" : 1
},
"indexName" : "time",
"isMultiKey" : false,
"multiKeyPaths" : {
"timestamp" : []
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"timestamp" : [
"(1501548359000.0, inf.0]"
]
},
"keysExamined" : 221,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
{
"nReturned" : 43,
"executionTimeMillisEstimate" : 1431,
"totalKeysExamined" : 221,
"totalDocsExamined" : 219,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"timestamp" : {
"$gt" : 1501548359000.0
}
},
"nReturned" : 43,
"executionTimeMillisEstimate" : 1431,
"works" : 221,
"advanced" : 43,
"needTime" : 177,
"needYield" : 0,
"saveState" : 64,
"restoreState" : 64,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 219,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 219,
"executionTimeMillisEstimate" : 71,
"works" : 221,
"advanced" : 219,
"needTime" : 1,
"needYield" : 0,
"saveState" : 64,
"restoreState" : 64,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"rack_name" : 1
},
"indexName" : "rack",
"isMultiKey" : false,
"multiKeyPaths" : {
"rack_name" : []
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"rack_name" : [
"[\"helena2\", \"helena2\"]",
"[\"providence1\", \"providence1\"]"
]
},
"keysExamined" : 221,
"seeks" : 2,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
]
},
"serverInfo" : {
"host" : "dtauto-sna01.mascorp.com",
"port" : 27017,
"version" : "3.4.4",
"gitVersion" : "888390515874a9debd1b6c5d36559ca86b44babd"
},
"ok" : 1.0
}
Two things I do not understand:
When you look at
AllPlansExecution
thenReturned
key has different value on each plan. Second plan (index: rack) is actually the winner plan and returns 43 results which is the actual return result of the whole query, but what are those 2nReturned
result from the firstMore challenge comes to this point that why the first plan (index: time which is the rejected plan) has been reported with less
executionTimeMillis
value 31 than the winner planexecutionTimeMillis
1431?
What's going on?
$in
to just "one" condition instead of "two" then what would the results look like then? Consider that "if you were a database, how would you determine which is best when you decide to fetch something?" Would you not try the "alternatives" before deciding which to choose? Does that "trying" possibly return a number of results?