簡體   English   中英

Mongodb查詢不使用復合索引上的前綴和文本字段

[英]Mongodb query does not use prefix on compound index with text field

我在我的集​​合上創建了以下索引:

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" : {}
            }
        }
    }
}
...

文檔中所述,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"
},
...

因此,它根本不使用索引並執行完整的集合掃描。

通常,MongoDB可以使用索引前綴來支持查詢,但是復合索引(包括地理空間或文本字段)是稀疏復合索引的特例。 如果文檔不包含復合索引中任何文本索引字段的值,則它不會包含在索引中。

為了確保前綴搜索的正確結果 ,將在稀疏復合索引上選擇備用查詢計划:

如果稀疏索引會導致查詢和排序操作的結果集不完整,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文檔中更清楚地突出顯示此異常; 觀察/ upvote DOCS-10322在MongoDB問題跟蹤器中進行更新。

此行為是由於默認情況下文本索引是稀疏的

對於包含文本索引鍵和其他類型鍵的復合索引,只有文本索引字段確定索引是否引用文檔。 其他鍵不確定索引是否引用文檔。

查詢過濾器未引用文本索引字段,因此查詢規划器不會考慮此索引,因為無法確定索引將返回完整的文檔結果集。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM