MongoDB 性能优化:分析执行计划

时间:2022-12-19 07:41:29

前言

cursor.explain("executionStats")db.collection.explain("executionStats") 方法提供一个关于查询的性能统计情况。这些数据输出在校验某个查询是否以及如何使用了索引的时候非常有用。
db.collection.explain() 提供该次执行的一些其他操作信息,比如 db.collection.update()

对于一次查询的性能评估

假设一个集合存储了以下文档:
{ "_id" : 1, "item" : "f1", type: "food", quantity: 500 }
{ "_id" : 2, "item" : "f2", type: "food", quantity: 100 }
{ "_id" : 3, "item" : "p1", type: "paper", quantity: 200 }
{ "_id" : 4, "item" : "p2", type: "paper", quantity: 150 }
{ "_id" : 5, "item" : "f3", type: "food", quantity: 300 }
{ "_id" : 6, "item" : "t1", type: "toys", quantity: 500 }
{ "_id" : 7, "item" : "a1", type: "apparel", quantity: 250 }
{ "_id" : 8, "item" : "a2", type: "apparel", quantity: 400 }
{ "_id" : 9, "item" : "t2", type: "toys", quantity: 50 }
{ "_id" : 10, "item" : "f4", type: "food", quantity: 75 }

没有索引下的查询

以下查询将对 quantity 字段的值介于 100200 的文档进行检索:
db.inventory.find( { quantity: { $gte: 100, $lte: 200 } } )

该查询返回的文档如下:
{ "_id" : 2, "item" : "f2", "type" : "food", "quantity" : 100 }
{ "_id" : 3, "item" : "p1", "type" : "paper", "quantity" : 200 }
{ "_id" : 4, "item" : "p2", "type" : "paper", "quantity" : 150 }

使用 explain("executionStats") 方法来查看其查询计划:
db.inventory.find(
   { quantity: { $gte: 100, $lte: 200 } }
).explain("executionStats")

explain() 方法返回结果如下:
{
   "queryPlanner" : {
         "plannerVersion" : 1,
         ...
         "winningPlan" : {
            "stage" : "COLLSCAN",
            ...
         }
   },
   "executionStats" : {
      "executionSuccess" : true,
      "nReturned" : 3,
      "executionTimeMillis" : 0,
      "totalKeysExamined" : 0,
      "totalDocsExamined" : 10,
      "executionStages" : {
         "stage" : "COLLSCAN",
         ...
      },
      ...
   },
   ...
}

  • queryPlanner.winningPlan.stage 显示为 COLLSCAN,表示进行了一次全集合扫描
  • executionStats.nReturned 显示为 3,表示匹配查询并返回的文档数目为 3
  • executionStats.totalDocsExamined 显示为 10,表示 MongoDB 必须扫描 10 个文档(也就是需要扫描该集合的所有文档)来找到这 3 个匹配的文档
匹配文档和被扫描文档数量之间的巨大差异意味着,要提高效率,可以使用索引对该查询进行优化。

使用索引的查询

quantity 字段上创建一个索引以支持该字段上的查询:
db.inventory.createIndex( { quantity: 1 } )

还是使用 explain("executionStats") 方法来查看其查询计划:
db.inventory.find(
   { quantity: { $gte: 100, $lte: 200 } }
).explain("executionStats")

explain() 方法返回结果如下:
{
   "queryPlanner" : {
         "plannerVersion" : 1,
         ...
         "winningPlan" : {
               "stage" : "FETCH",
               "inputStage" : {
                  "stage" : "IXSCAN",
                  "keyPattern" : {
                     "quantity" : 1
                  },
                  ...
               }
         },
         "rejectedPlans" : [ ]
   },
   "executionStats" : {
         "executionSuccess" : true,
         "nReturned" : 3,
         "executionTimeMillis" : 0,
         "totalKeysExamined" : 3,
         "totalDocsExamined" : 3,
         "executionStages" : {
            ...
         },
         ...
   },
   ...
}

  • queryPlanner.winningPlan.inputStage.stage 显示为 IXSCAN,表示使用了索引扫描
  • executionStats.nReturned 显示为 3,表示匹配查询并返回的文档数目为 3
  • executionStats.totalKeysExamined 显示为 3,表示 MongoDB 扫描了 3 个索引条目
  • executionStats.totalDocsExamined 显示为 3,表示 MongoDB 扫描了 3 个文档
在使用了索引的情况下,该查询总共扫描了 3 个索引条目以及 3 个文档以返回 3 个匹配的文档。在没有索引的情况下,要返回 3 个匹配的文档,MongoDB 必须去进行全集合扫描 - 对 10 个文档进行扫描。

索引性能的对比

你可以使用 hint() 方法结合 explain() 方法来对查询中的多个索引的性能进行手工对比。
考虑一下这个查询:
db.inventory.find( { quantity: { $gte: 100, $lte: 300 }, type: "food" } )

该查询返回以下文档:
{ "_id" : 2, "item" : "f2", "type" : "food", "quantity" : 100 }
{ "_id" : 5, "item" : "f3", "type" : "food", "quantity" : 300 }

创建一个复合索引以支持上述查询。 在使用复合索引的时候,字段的顺序很重要( 译者批:注意是索引中的顺序,而非查询中出现的顺序)。
比如,创建下面两个复合索引。第一个索引把 quantity 字段放在前面,而第二个字段把 type 字段放前:
db.inventory.createIndex( { quantity: 1, type: 1 } )
db.inventory.createIndex( { type: 1, quantity: 1 } )

先对第一个索引在该查询上起的效果进行评估:
db.inventory.find(
   { quantity: { $gte: 100, $lte: 300 }, type: "food" }
).hint({ quantity: 1, type: 1 }).explain("executionStats")

explain() 方法返回结果如下:
{
   "queryPlanner" : {
      ...
      "winningPlan" : {
         "stage" : "FETCH",
         "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
               "quantity" : 1,
               "type" : 1
            },
            ...
            }
         }
      },
      "rejectedPlans" : [ ]
   },
   "executionStats" : {
      "executionSuccess" : true,
      "nReturned" : 2,
      "executionTimeMillis" : 0,
      "totalKeysExamined" : 5,
      "totalDocsExamined" : 2,
      "executionStages" : {
      ...
      }
   },
   ...
}

MongoDB 扫描了 5 个索引键( executionStats.totalKeysExamined)以返回 2 个匹配的文档( executionStats.nReturned)。
再对第二个索引在该查询上起的效果进行评估:
db.inventory.find(
   { quantity: { $gte: 100, $lte: 300 }, type: "food" }
).hint({ type: 1, quantity: 1 }).explain("executionStats")

explain() 方法返回结果如下:
{
   "queryPlanner" : {
      ...
      "winningPlan" : {
         "stage" : "FETCH",
         "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
               "type" : 1,
               "quantity" : 1
            },
            ...
         }
      },
      "rejectedPlans" : [ ]
   },
   "executionStats" : {
      "executionSuccess" : true,
      "nReturned" : 2,
      "executionTimeMillis" : 0,
      "totalKeysExamined" : 2,
      "totalDocsExamined" : 2,
      "executionStages" : {
         ...
      }
   },
   ...
}

MongoDB 扫描了 2 个索引键( executionStats.totalKeysExamined)以返回 2 个匹配的文档( executionStats.nReturned)。
显而易见,对于上述查询,复合索引 { type: 1, quantity: 1 }{ quantity: 1, type: 1 } 更高效。
原文链接: https://docs.mongodb.com/manual/tutorial/analyze-query-plan/