Mongodb查询不使用复合索引上的前缀和文本字段

时间:2017-07-03 20:09:33

标签: mongodb indexing

我在我的收藏中创建了以下索引:

db.myCollection.createIndex({
  user_id: 1,
  name: 'text'
})

如果我试图查看包含这两个字段的查询的执行计划,请执行以下操作:

db.getCollection('campaigns').find({ 
    user_id: ObjectId('xxx')
   ,$text: { $search: 'bla' } 
}).explain('executionStats')

我得到以下结果:

...
"winningPlan" : {
    "stage" : "TEXT",
    "indexPrefix" : {
        "user_id" : ObjectId("xxx")
    },
    "indexName" : "user_id_1_name_text",
    "parsedTextQuery" : {
        "terms" : [ 
            "e"
        ],
        "negatedTerms" : [],
        "phrases" : [],
        "negatedPhrases" : []
    },
    "inputStage" : {
        "stage" : "TEXT_MATCH",
        "inputStage" : {
            "stage" : "TEXT_OR",
            "inputStage" : {
                "stage" : "IXSCAN",
                "keyPattern" : {
                    "user_id" : 1.0,
                    "_fts" : "text",
                    "_ftsx" : 1
                },
                "indexName" : "user_id_1_name_text",
                "isMultiKey" : true,
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 1,
                "direction" : "backward",
                "indexBounds" : {}
            }
        }
    }
}
...

documentation中所述,MongoDB可以使用索引前缀来执行索引查询。

由于user_id是上述索引的前缀,我希望只有user_id的查询会使用该索引,但如果我尝试以下内容:

db.myCollection.find({ 
    user_id: ObjectId('xxx')
}).explain('executionStats')

我明白了:

...
"winningPlan" : {
    "stage" : "COLLSCAN",
    "filter" : {
        "user_id" : {
            "$eq" : ObjectId("xxx")
        }
    },
    "direction" : "forward"
},
...

因此,它根本没有使用索引并执行完整的集合扫描。

2 个答案:

答案 0 :(得分:3)

一般情况下,MongoDB可以使用索引前缀来支持查询,但复合索引(包括地理空间或文本字段)是sparse compound indexes的特例。如果文档不包含复合索引中任何文本索引字段的值,则它不会包含在索引中。

为了确保前缀搜索correct results,将在稀疏复合索引上选择一个替代查询计划:

  

如果稀疏索引会导致查询和排序操作的结果集不完整,MongoDB将不会使用该索引,除非提示()显式指定索引。

在MongoDB 3.4.5中设置一些测试数据以演示潜在的问题:

db.myCollection.createIndex({ user_id:1, name: 'text' }, { name: 'myIndex'})

// `name` is a string; this document will be included in a text index
db.myCollection.insert({ user_id:123, name:'Banana' })

// `name` is a number; this document will NOT be included in a text index
db.myCollection.insert({ user_id:123, name: 456 })

// `name` is missing; this document will NOT be included in a text index
db.myCollection.insert({ user_id:123 })

然后,强制使用复合文本索引:

db.myCollection.find({user_id:123}).hint('myIndex')

结果只包含带有索引文本字段name的单个文档,而不是预期的三个文档:

{
  "_id": ObjectId("595ab19e799060aee88cb035"),
  "user_id": 123,
  "name": "Banana"
}

MongoDB文档中应该更清楚地突出显示此异常;在MongoDB问题跟踪器中观察/ upvote DOCS-10322以获取更新。

答案 1 :(得分:1)

此行为是由于文本索引为sparse by default

  

对于包含文本索引键和键的复合索引   其他类型,只有文本索引字段确定是否为索引   引用文件。其他键不确定是否   index是否引用文档。

查询过滤器未引用文本索引字段,因此查询规划器不会考虑此索引,因为它无法确定索引将返回完整的文档结果集。 / p>