我有一个集合,我有一个多语言形式的数据,如下所示:
> db.names.findOne();
{
"_id" : ObjectId("591dbcd877d4ede22d765247"),
"name" : [
{
"locale" : "de",
"text" : "Brunhilde"
},
{
"locale" : "sk",
"text" : "Šimon"
}
]
}
我已为每种语言定义了索引:
db.names.createIndex({ 'name.locale': 1, 'name.text': 1 }, { name: 'name_de', partialFilterExpression: { 'name.locale': 'de' }, collation: { locale: 'de' } });
db.names.createIndex({ 'name.locale': 1, 'name.text': 1 }, { name: 'name_sk', partialFilterExpression: { 'name.locale': 'sk' }, collation: { locale: 'sk' } });
当我将该集合查询为:
> db.names.find({ 'name.locale': 'de', 'name.text': 'Rüdi' }).collation({ locale: 'de' });
查询使用索引,但解释计划看起来有点奇怪:
...
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"name.text" : {
"$eq" : "Rüdi"
}
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 10,
"advanced" : 1,
"needTime" : 8,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 9,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 9,
"executionTimeMillisEstimate" : 0,
"works" : 10,
"advanced" : 9,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"name.locale" : 1,
"name.text" : 1
},
"indexName" : "name_de",
"collation" : {
"locale" : "de",
"caseLevel" : false,
"caseFirst" : "off",
"strength" : 3,
"numericOrdering" : false,
"alternate" : "non-ignorable",
"maxVariable" : "punct",
"normalization" : false,
"backwards" : false,
"version" : "57.1"
},
"isMultiKey" : true,
"multiKeyPaths" : {
"name.locale" : [
"name"
],
"name.text" : [
"name"
]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : true,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"name.locale" : [
"[\"/1\u0001\u0006\u0001\u0006\", \"/1\u0001\u0006\u0001\u0006\"]"
],
"name.text" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 9,
"seeks" : 1,
"dupsTested" : 9,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
它创建了一个IXSCAN,但是在FETCH阶段,MongoDB通过name.text
进行过滤。
另一方面,当我进行查询时,name.text
用于将结果排序为:
> db.names.find({ 'name.locale': 'de' }).sort({ 'name.text': 1 }).collation({ locale: "de" });
解释计划如下:
...
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 9,
"executionTimeMillis" : 1,
"totalKeysExamined" : 9,
"totalDocsExamined" : 9,
"executionStages" : {
"stage" : "FETCH",
"nReturned" : 9,
"executionTimeMillisEstimate" : 0,
"works" : 10,
"advanced" : 9,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 9,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 9,
"executionTimeMillisEstimate" : 0,
"works" : 10,
"advanced" : 9,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"name.locale" : 1,
"name.text" : 1
},
"indexName" : "name_de",
"collation" : {
"locale" : "de",
"caseLevel" : false,
"caseFirst" : "off",
"strength" : 3,
"numericOrdering" : false,
"alternate" : "non-ignorable",
"maxVariable" : "punct",
"normalization" : false,
"backwards" : false,
"version" : "57.1"
},
"isMultiKey" : true,
"multiKeyPaths" : {
"name.locale" : [
"name"
],
"name.text" : [
"name"
]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : true,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"name.locale" : [
"[\"/1\u0001\u0006\u0001\u0006\", \"/1\u0001\u0006\u0001\u0006\"]"
],
"name.text" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 9,
"seeks" : 1,
"dupsTested" : 9,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
现在,IXSCAN之后是没有过滤器的FETCH。
问题是:为什么在第一种情况下使用FETCH中的过滤器并且仅仅使用索引是不够的?
在这两种情况下,解释计划中的indexBounds
都是相同的,这可以解释为什么在第二个查询中不需要过滤器。我没有在MongoDB文档中找到任何可以解释这种行为的约束。
其他信息 当我尝试使用我的索引的替换时:
> db.names.createIndex({ 'name.text': 1 }, { name: 'name_de', partialFilterExpression: { 'name.locale': 'de' }, collation: { locale: 'de' } });
即。从索引中省略字段name.locale
。它给出了有趣的结果,令人遗憾的是仍然是错误的。第一个查询解释计划是:
...
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 0,
"totalKeysExamined" : 1,
"totalDocsExamined" : 1,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"name.locale" : {
"$eq" : "de"
}
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 2,
"advanced" : 1,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 1,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 2,
"advanced" : 1,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"name.text" : 1
},
"indexName" : "name_de",
"collation" : {
"locale" : "de",
"caseLevel" : false,
"caseFirst" : "off",
"strength" : 3,
"numericOrdering" : false,
"alternate" : "non-ignorable",
"maxVariable" : "punct",
"normalization" : false,
"backwards" : false,
"version" : "57.1"
},
"isMultiKey" : true,
"multiKeyPaths" : {
"name.text" : [
"name"
]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : true,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"name.text" : [
"[\"KQ/9\u0001D�\u0006\u0001�\b\", \"KQ/9\u0001D�\u0006\u0001�\b\"]"
]
},
"keysExamined" : 1,
"seeks" : 1,
"dupsTested" : 1,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
...
它仍在FETCH中进行过滤,但是通过name.locale
进行过滤,而不是通过name.text
进行过滤。 indexBounds
也发生了变化,并包含name.text
模式。
第二个查询解释计划是:
...
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 9,
"executionTimeMillis" : 0,
"totalKeysExamined" : 18,
"totalDocsExamined" : 9,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"name.locale" : {
"$eq" : "de"
}
},
"nReturned" : 9,
"executionTimeMillisEstimate" : 0,
"works" : 19,
"advanced" : 9,
"needTime" : 9,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 9,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 9,
"executionTimeMillisEstimate" : 0,
"works" : 19,
"advanced" : 9,
"needTime" : 9,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"name.text" : 1
},
"indexName" : "name_de",
"collation" : {
"locale" : "de",
"caseLevel" : false,
"caseFirst" : "off",
"strength" : 3,
"numericOrdering" : false,
"alternate" : "non-ignorable",
"maxVariable" : "punct",
"normalization" : false,
"backwards" : false,
"version" : "57.1"
},
"isMultiKey" : true,
"multiKeyPaths" : {
"name.text" : [
"name"
]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : true,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"name.text" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 18,
"seeks" : 1,
"dupsTested" : 18,
"dupsDropped" : 9,
"seenInvalidated" : 0
}
}
}
...
indexBounds
模式包含name.text
的最小/最大范围(在排序中使用时很清楚),并且FETCH包含过滤器。但是......结果是错误的,文档不再像以前那样按德语翻译排序:
...
{ "_id" : ObjectId("591dbcd877d4ede22d76524f"), "name" : [ { "locale" : "de", "text" : "Mathias" }, { "locale" : "sk", "text" : "Abel" } ] }
{ "_id" : ObjectId("591dbcd877d4ede22d76524e"), "name" : [ { "locale" : "de", "text" : "Rudi" }, { "locale" : "sk", "text" : "Ábel" } ] }
{ "_id" : ObjectId("591dbcd877d4ede22d765248"), "name" : [ { "locale" : "de", "text" : "Brünhild" }, { "locale" : "sk", "text" : "Žofia" } ] }
{ "_id" : ObjectId("591dbcd877d4ede22d765247"), "name" : [ { "locale" : "de", "text" : "Brunhilde" }, { "locale" : "sk", "text" : "Šimon" } ] }
{ "_id" : ObjectId("591dbcd877d4ede22d765249"), "name" : [ { "locale" : "de", "text" : "Cäcilia" }, { "locale" : "sk", "text" : "Chruňo" } ] }
{ "_id" : ObjectId("591dbcd877d4ede22d76524a"), "name" : [ { "locale" : "de", "text" : "Caecilia" }, { "locale" : "sk", "text" : "Hana" } ] }
{ "_id" : ObjectId("591dbcd877d4ede22d76524b"), "name" : [ { "locale" : "de", "text" : "Günther" }, { "locale" : "sk", "text" : "Cecília" } ] }
{ "_id" : ObjectId("591dbcd877d4ede22d76524c"), "name" : [ { "locale" : "de", "text" : "Gunther" }, { "locale" : "sk", "text" : "Zina" } ] }
{ "_id" : ObjectId("591dbcd877d4ede22d76524d"), "name" : [ { "locale" : "de", "text" : "Rüdi" }, { "locale" : "sk", "text" : "Stano" } ] }
...
恕我直言,在MongoDB中使用这个索引确实有些奇怪。