Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么MongoDB没有正确使用索引?_Mongodb - Fatal编程技术网

为什么MongoDB没有正确使用索引?

为什么MongoDB没有正确使用索引?,mongodb,Mongodb,我有一个集合,其中我有一个多语言形式的数据,如下所示: > db.names.findOne(); { "_id" : ObjectId("591dbcd877d4ede22d765247"), "name" : [ { "locale" : "de", "text" : "Brunhilde" }, { "locale" : "sk", "text" : "Šimon" } ] } 我已将每种语

我有一个集合,其中我有一个多语言形式的数据,如下所示:

> 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中的过滤器,而仅仅使用索引是不够的?

在这两种情况下,解释计划中的
索引边界
是相同的,这可以解释为什么在第二次查询中不需要过滤器。我在MongoDB文档中没有找到任何可以解释这种行为的约束

其他信息 当我尝试将我的索引替换为:

> db.names.createIndex({ 'name.text': 1 }, { name: 'name_de', partialFilterExpression: { 'name.locale': 'de' }, collation: { locale: 'de' } });
i、 e.从索引中省略字段
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
    }
  }
}
...
...
"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
    }
  }
}
...
它仍然在FETCH中进行过滤,但通过
name.locale
进行过滤,而不是通过
name.text
indexBounds
也已更改,并包含
name.text
模式。 第二个问题是:

...
"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
    }
  }
}
...
...
"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中使用这个索引确实有点奇怪。

第一个问题的解决方案是使用$elemMatch操作符,否则查询将匹配“name”字段的所有组合,因此不会同时匹配“locale”和“text”。这里和下面讨论了替代性的和整体的“部分过滤表达”问题。