对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

解释“解释计划结果”

您可以使用解释结果,确定有关查询的以下信息:

  • 完成查询所需的时间

  • 查询是否使用索引

  • 为完成查询而扫描的文档和索引键的数量

注意

查询的解释计划结果可能会在各个 MongoDB 版本之间发生变化。

cursor.explain("executionStats") 和 db.collection.explain("executionStats") 方法提供了有关查询性能的统计信息。这些统计信息可用于衡量查询是否索引以及是如何使用该索引的。有关详细信息,请参阅 db.collection.explain()。

本页上的示例使用来自 sample_mflix 数据集的数据。有关如何将此数据集加载到您的自管理 MongoDB 部署中的详细信息,请参阅 加载示例数据集。如果您对示例数据库进行了任何修改,您可能需要删除并重新创建数据库,才能运行此页面上的示例。

以下查询检索 year 字段的值在 2000 和 2005(含)之间的文档:

db.movies.find( { year: { $gte: 2000, $lte: 2005 } } )

此查询匹配集合中 21349 个文档中的 3809 个。

要查看选定的查询计划,请将 cursor.explain("executionStats") 游标方法链接到 find 命令的末尾:

db.movies.find( { year: { $gte: 2000, $lte: 2005 } }).explain("executionStats")
{ queryPlanner: { namespace: 'sample_mflix.movies', winningPlan: { stage: 'COLLSCAN', '...': '...' }, '...': '...' }, executionStats: { executionSuccess: true, nReturned: 3809, totalKeysExamined: 0, totalDocsExamined: 21349, executionStages: { stage: 'COLLSCAN', '...': '...' }, '...': '...' }, '...': '...'}

匹配文档数量和已检查文档数量之间的差异可能表明,使用索引可能有助于提高查询效率。

要支持对 year 字段的查询,请对 year 字段添加索引:

db.movies.createIndex( { year: 1 } )

要查看查询计划的统计信息,请使用 explain() 方法:

db.movies.find( { year: { $gte: 2000, $lte: 2005 } }).explain("executionStats")
{ queryPlanner: { namespace: 'sample_mflix.movies', winningPlan: { stage: 'FETCH', inputStage: { stage: 'IXSCAN', keyPattern: { year: 1 }, '...': '...' }, '...': '...' }, '...': '...' }, executionStats: { executionSuccess: true, nReturned: 3809, totalKeysExamined: 3809, totalDocsExamined: 3809, '...': '...' }, '...': '...'}

如果没有索引,查询会扫描整个集合以返回匹配的文档。此查询还须扫描每个文档的全部,因而可能会将其拉取到内存中。此举会导致查询操作成本高昂且可能缓慢。

使用索引运行时,查询仅扫描它返回的索引项和文档,从而提升查询效率。

比较索引性能

如要手动比较使用多个索引的查询性能,您可以结合使用 hint() 方法和 explain() 方法。

考虑以下查询:

db.movies.find( { year: { $gte: 2000, $lte: 2005 }, rated: "PG"} )

此查询匹配 235 个文档。

为支持查询,请添加复合索引。在复合索引中,字段的顺序很重要。

例如,添加以下两个复合索引。第一个索引首先按 year 字段排序,然后按 rated 字段排序。第二个索引首先按 rated 排序,然后按 year 字段排序。

db.movies.createIndex( { year: 1, rated: 1 } )db.movies.createIndex( { rated: 1, year: 1 } )

评估第一个索引对查询的影响:

db.movies.find( { year: { $gte: 2000, $lte: 2005 }, rated: "PG" }).hint( { year: 1, rated: 1 } ).explain("executionStats")
{ queryPlanner: { namespace: 'sample_mflix.movies', winningPlan: { stage: 'FETCH', inputStage: { stage: 'IXSCAN', keyPattern: { year: 1, rated: 1 }, '...': '...' }, '...': '...' }, '...': '...' }, executionStats: { executionSuccess: true, nReturned: 235, totalKeysExamined: 246, totalDocsExamined: 235, '...': '...' }, '...': '...'}

MongoDB扫描了246个索引键 (executionStats.totalKeysExamined) 以返回 235 个匹配的文档 (executionStats.nReturned)。

评估第二个索引对查询的影响:

db.movies.find( { year: { $gte: 2000, $lte: 2005 }, rated: "PG" }).hint( { rated: 1, year: 1 } ).explain("executionStats")
{ queryPlanner: { namespace: 'sample_mflix.movies', winningPlan: { stage: 'FETCH', inputStage: { stage: 'IXSCAN', keyPattern: { rated: 1, year: 1 }, '...': '...' }, '...': '...' }, '...': '...' }, executionStats: { executionSuccess: true, nReturned: 235, totalKeysExamined: 235, totalDocsExamined: 235, '...': '...' }, '...': '...'}

MongoDB扫描了235个索引键 (executionStats.totalKeysExamined) 以返回 235 个匹配的文档 (executionStats.nReturned)。

因此,第二个复合索引 { rated: 1, year: 1 } 是支持示例查询的更高效索引,因为 MongoDB 服务器只需扫描 235 个 index keys 来查找所有匹配文档,而使用复合索引 { year: 1, rated: 1 } 时需要扫描 246 个。

给本页内容打分

在此页面上