如果我使用提示,为什么Mongodb仅使用我的索引

我有一个带有索引的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(),我感到困惑,为什么它拒绝使用索引。

我的索引稀疏且不唯一。

我尝试了其他索引并进行了搜索,但是在堆栈溢出或其他地方找不到任何对此问题的引用。

zhangfujun12545 回答:如果我使用提示,为什么Mongodb仅使用我的索引

根据MongoDB的文档

  

如果索引稀疏会导致查询结果集不完整   和排序操作,除非使用hint(),否则MongoDB不会使用该索引   明确指定索引。

要使用稀疏索引,请使用hint()显式指定索引

作为hint()强制查询优化器在执行查询时使用索引

本文链接:https://www.f2er.com/3137468.html

大家都在问