MongoDB

关注公众号 jb51net

关闭
首页 > 数据库 > MongoDB > MongoDB使用explain命令

MongoDB使用explain命令的步骤和代码示例

作者:Victor356

使用MongoDB的explain 命令可以深入了解查询的执行计划,从而帮助你优化和调试查询性能,下面是详细的步骤和代码示例,展示如何使用 explain 命令深入分析查询,感兴趣的朋友跟随小编一起看看吧

使用 MongoDB 的 explain 命令可以深入了解查询的执行计划,从而帮助你优化和调试查询性能。下面是详细的步骤和代码示例,展示如何使用 explain 命令深入分析查询。

1. 基本使用

最基本的 explain 命令可以提供查询的执行计划。

示例:基本explain使用

db.students.find({ studentId: 12345 }).explain();

2. explain() 的模式

explain 命令有三种模式:

示例:不同模式的explain

// queryPlanner 模式
db.students.find({ studentId: 12345 }).explain("queryPlanner");
// executionStats 模式
db.students.find({ studentId: 12345 }).explain("executionStats");
// allPlansExecution 模式
db.students.find({ studentId: 12345 }).explain("allPlansExecution");

3. explain() 输出解读

示例输出(executionStats 模式)

{
  "queryPlanner": {
    "plannerVersion": 1,
    "namespace": "school.students",
    "indexFilterSet": false,
    "parsedQuery": { "studentId": { "$eq": 12345 } },
    "winningPlan": {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": { "studentId": 1 },
        "indexName": "studentId_1",
        "direction": "forward",
        "indexBounds": { "studentId": [ "[12345, 12345]" ] }
      }
    },
    "rejectedPlans": []
  },
  "executionStats": {
    "executionSuccess": true,
    "nReturned": 1,
    "executionTimeMillis": 2,
    "totalKeysExamined": 1,
    "totalDocsExamined": 1,
    "executionStages": {
      "stage": "FETCH",
      "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": { "studentId": 1 },
        "indexName": "studentId_1",
        "isMultiKey": false,
        "multiKeyPaths": { "studentId": [] },
        "indexBounds": { "studentId": [ "[12345, 12345]" ] },
        "keysExamined": 1,
        "seeks": 1,
        "dupsTested": 0,
        "dupsDropped": 0
      }
    }
  },
  "serverInfo": {
    "host": "localhost",
    "port": 27017,
    "version": "4.4.6",
    "gitVersion": "22c124145fa3bfdaeafb3f6d1b5f3d4e8391fe86"
  }
}

关键字段解读

4. 示例:复合索引和多条件查询

假设有一个复合索引 { lastName: 1, firstName: 1 },并执行多条件查询。

创建复合索引

db.students.createIndex({ lastName: 1, firstName: 1 });

查询及执行计划分析

db.students.find({ lastName: "Smith", firstName: "John" }).explain("executionStats");

示例输出及解读

{
  "queryPlanner": {
    "plannerVersion": 1,
    "namespace": "school.students",
    "indexFilterSet": false,
    "parsedQuery": { "lastName": { "$eq": "Smith" }, "firstName": { "$eq": "John" } },
    "winningPlan": {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": { "lastName": 1, "firstName": 1 },
        "indexName": "lastName_1_firstName_1",
        "direction": "forward",
        "indexBounds": {
          "lastName": [ "[\"Smith\", \"Smith\"]" ],
          "firstName": [ "[\"John\", \"John\"]" ]
        }
      }
    },
    "rejectedPlans": []
  },
  "executionStats": {
    "executionSuccess": true,
    "nReturned": 1,
    "executionTimeMillis": 1,
    "totalKeysExamined": 1,
    "totalDocsExamined": 1,
    "executionStages": {
      "stage": "FETCH",
      "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": { "lastName": 1, "firstName": 1 },
        "indexName": "lastName_1_firstName_1",
        "isMultiKey": false,
        "multiKeyPaths": { "lastName": [], "firstName": [] },
        "indexBounds": {
          "lastName": [ "[\"Smith\", \"Smith\"]" ],
          "firstName": [ "[\"John\", \"John\"]" ]
        },
        "keysExamined": 1,
        "seeks": 1,
        "dupsTested": 0,
        "dupsDropped": 0
      }
    }
  }
}

通过以上示例和解读,您可以深入了解 MongoDB 查询的执行计划,并根据执行计划中的信息优化查询和索引设计。合理使用 explain 命令,可以显著提升查询性能,确保数据库的高效运行。

到此这篇关于MongoDB使用explain命令的文章就介绍到这了,更多相关MongoDB使用explain命令内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文