我有一个带有索引的ISODate()类型字段的数据库(我也尝试了使用字符串字段进行此实验-结果相同)。我使用的是MongoDB(4.x)的开源版本,当我执行查询/排序以查找最大_finish_time时,除非指定提示,否则不使用索引。
我的查询是:
db.getcollection("test").find({},{ _finish_time: 1}).sort({_finish_time: -1}).limit(1)
解释为:
{
"queryPlanner" : {
"plannerVersion" : 1.0,"namespace" : "vdm-service-ts-staging.test","indexFilterSet" : false,"parsedQuery" : {
},"winningPlan" : {
"stage" : "PROJECTION","transformBy" : {
"_finish_time" : 1.0
},"inputStage" : {
"stage" : "SORT","sortPattern" : {
"_finish_time" : -1.0
},"limitAmount" : 1.0,"inputStage" : {
"stage" : "SORT_KEY_GENERATOR","inputStage" : {
"stage" : "COLLSCAN","direction" : "forward"
}
}
}
},"rejectedPlans" : [
]
},"serverInfo" : {
"host" : "ip-10-82-245-45.us-west-2.compute.internal","port" : 27017.0,"version" : "4.0.1","gitVersion" : "54f1582fc6eb01de4d4c42f26fc133e623f065fb"
},"ok" : 1.0,"operationTime" : Timestamp(1573220526,1),"$clusterTime" : {
"clusterTime" : Timestamp(1573220526,"signature" : {
"hash" : BinData(0,"blIkiGcam87SDdbKeZKex/9JXBU="),"keyId" : NumberLong(6715502669504446467)
}
}
}
扫描整个集合。当我为可用索引指定提示时,如:
db.getcollection("test").find({},{ _finish_time: 1}).sort({_finish_time: -1}).limit(1).hint("_finish_time")
我得到了查询计划:
{
"queryPlanner" : {
"plannerVersion" : 1.0,"winningPlan" : {
"stage" : "LIMIT","inputStage" : {
"stage" : "PROJECTION","transformBy" : {
"_finish_time" : 1.0
},"inputStage" : {
"stage" : "FETCH","inputStage" : {
"stage" : "IXSCAN","keyPattern" : {
"_finish_time" : -1.0
},"indexName" : "_finish_time","isMultiKey" : false,"multiKeyPaths" : {
"_finish_time" : [
]
},"isUnique" : false,"issparse" : true,"isPartial" : false,"indexVersion" : 2.0,"direction" : "forward","indexBounds" : {
"_finish_time" : [
"[MaxKey,MinKey]"
]
}
}
}
}
},"operationTime" : Timestamp(1573220603,3),"$clusterTime" : {
"clusterTime" : Timestamp(1573220603,"qsGhD1DpI306XbqtNZDYVINPid8="),"keyId" : NumberLong(6715502669504446467)
}
}
}
使用索引。我希望不必在我的查询中添加hint(),我感到困惑,为什么它拒绝使用索引。
我的索引稀疏且不唯一。
我尝试了其他索引并进行了搜索,但是在堆栈溢出或其他地方找不到任何对此问题的引用。