我在我的收藏中创建了以下索引:
db.myCollection.createIndex({
user_id: 1,
name: 'text'
})
Run Code Online (Sandbox Code Playgroud)
如果我尝试查看包含两个字段的查询的执行计划,如下所示:
db.getCollection('campaigns').find({
user_id: ObjectId('xxx')
,$text: { $search: 'bla' }
}).explain('executionStats')
Run Code Online (Sandbox Code Playgroud)
我得到以下结果:
...
"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" : {}
}
}
}
}
...
Run Code Online (Sandbox Code Playgroud)
正如文档中所述,MongoDB 可以使用索引前缀来执行索引查询。
由于user_id是上述索引的前缀,我希望只有 by 的查询user_id会使用索引,但如果我尝试以下操作:
db.myCollection.find({
user_id: ObjectId('xxx')
}).explain('executionStats')
Run Code Online (Sandbox Code Playgroud)
我得到:
...
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"user_id" : {
"$eq" : ObjectId("xxx")
}
},
"direction" : "forward"
},
...
Run Code Online (Sandbox Code Playgroud)
因此,它根本不使用索引并执行完整的集合扫描。
一般来说,MongoDB 可以使用索引前缀来支持查询,但是包括地理空间或文本字段在内的复合索引是稀疏复合索引的特例。如果文档不包含复合索引中任何文本索引字段的值,则它将不会包含在索引中。
为了确保前缀搜索的正确结果,将在稀疏复合索引上选择替代查询计划:
如果稀疏索引会导致查询和排序操作的结果集不完整,MongoDB 将不会使用该索引,除非hint() 明确指定该索引。
在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 })
Run Code Online (Sandbox Code Playgroud)
然后,强制使用复合文本索引:
db.myCollection.find({user_id:123}).hint('myIndex')
Run Code Online (Sandbox Code Playgroud)
结果仅包括带有索引文本字段的单个文档name,而不是预期的三个文档:
{
"_id": ObjectId("595ab19e799060aee88cb035"),
"user_id": 123,
"name": "Banana"
}
Run Code Online (Sandbox Code Playgroud)
这个异常应该在 MongoDB 文档中更清楚地突出显示;在 MongoDB 问题跟踪器中观看/投票 DOCS-10322以获取更新。
| 归档时间: |
|
| 查看次数: |
1383 次 |
| 最近记录: |